Anfra docs

Queries

Describe the data each view needs

A query object describes the data a view needs. Your frontend reads its result and passes that data to a chart or other UI.

Creating a query

Here, the page redraws a revenue chart whenever the query updates:

const trend = app.createQuery('trend', {
  dataset: 'sales',
  aql: `explore {
    dimensions { month: date_trunc(orders.created_at, "month") }
    measures { revenue: revenue }
  }`,
})

app.subscribe(() => renderRevenueChart(document.querySelector('#chart'), trend.result))
await app.execute()

AMQL or structured fields

Queries can be written in two forms:

  • AMQL, passed as the aql string, as in the example above.
  • Structured fields, passed as dimensions and measures objects:
const byRegion = app.createQuery('byRegion', {
  dataset: 'sales',
  dimensions: { region: { field: 'users.region' } },
  measures: { revenue: { field: 'revenue' } },
})

Running queries

app.execute() runs every query that needs updating. app.subscribe() registers a callback that runs when results change, so one subscription can redraw all views on the page.

Queries respond to controls and to selections in other views through interaction mappings.

On this page