GraphQLQueryBuilder class
Fluent GraphQL query builder for constructing and executing complex queries.
Constructors
| Graph |
Creates a query builder for a single entity. |
Methods
| after(string) | Continue pagination after the given cursor. Example
|
| aggregate<TSpec>(TSpec) | Execute a grouped aggregation. The specification is keyed by Builder-chosen aliases; each entry value
is a single-operation object ( Example
|
| execute() | Execute the query and return a single page of matching records. Example
|
| execute |
Execute the query and return a paginated result with cursor metadata. Example
|
| find |
Execute the query and return only the first matching record. The temporary limit is applied to an independent builder. |
| first(number) | Set the page size for this query. Example
|
| group |
Start a grouped aggregation over the given scalar fields.
Follow with GroupedAggregationStage.aggregate to specify the
aggregation values, then call Example
|
| order |
Add an order-by clause. Repeated calls append additional sort keys. Sort directions must be lowercase ( Example
|
| select<TFields>(TFields) | Select the fields (and nested relationship fields) to return. Field paths use Example
|
| where(Filter |
Add filter conditions. Repeated calls are merged together. Successive calls combine via DAB's lowercase Example
|
Constructor Details
GraphQLQueryBuilder<TSchema, TEntity, TResult>(GraphQLClient, string)
Creates a query builder for a single entity.
new GraphQLQueryBuilder(graphqlClient: GraphQLClient, entityName: string)
Parameters
- graphqlClient
- GraphQLClient
The GraphQL client used to execute the built query.
- entityName
-
string
The entity name to query (pluralized internally for DAB).
Method Details
after(string)
Continue pagination after the given cursor.
Example
const page = await client.data.Post
.first(20)
.after(previousCursor)
.executePaginated();
function after(cursor: string): RowQueryBuilder<TSchema, TEntity, GraphQLQueryBuilder<TSchema, TEntity, TResult>, GraphQLResponseValue<TResult>>
Parameters
- cursor
-
string
The endCursor from a previous paginated result.
Returns
RowQueryBuilder<TSchema, TEntity, GraphQLQueryBuilder<TSchema, TEntity, TResult>, GraphQLResponseValue<TResult>>
This builder, for chaining.
aggregate<TSpec>(TSpec)
Execute a grouped aggregation.
The specification is keyed by Builder-chosen aliases; each entry value
is a single-operation object ({ sum | avg | min | max | count })
whose value is either a field-name shorthand or an
{ field, having?, distinct? } options object. Aliases and field
tokens are validated at runtime against the GraphQL Name grammar.
Example
const rows = await client.data.Order
.groupBy(['region'])
.aggregate({
revenue: { sum: 'amount' },
orders: { count: 'quantity' },
biggest: { max: { field: 'amount', having: { gt: 500 } } },
})
.execute();
function aggregate<TSpec>(spec: TSpec): GraphQLAggregationBuilder<TSchema, TEntity, readonly Object, TSpec>
Parameters
- spec
-
TSpec
The alias-keyed aggregation specification.
Returns
GraphQLAggregationBuilder<TSchema, TEntity, readonly Object, TSpec>
A GraphQLAggregationBuilder; call .execute() to run it.
Remarks
Because DAB rejects a query that combines groupBy and items,
calling aggregate after select, first, after,
or orderBy throws a runtime error.
execute()
Execute the query and return a single page of matching records.
Example
const todos = await client.data.Todo
.select(['id', 'title'])
.where({ isCompleted: { eq: false } })
.orderBy({ createdAt: 'desc' })
.execute();
for (const todo of todos) {
console.log(todo.title);
}
function execute(): Promise<GraphQLResponseValue<TResult>[]>
Returns
Promise<GraphQLResponseValue<TResult>[]>
A single page of matching records.
Remarks
The Data API returns only one page (100 records by default, or the size set via first) and does not signal whether more records exist, so lists longer than one page are silently truncated. For result sets that can exceed one page, use executePaginated with after to page through the full set.
executePaginated()
Execute the query and return a paginated result with cursor metadata.
Example
let cursor: string | undefined = undefined;
do {
let query = client.data.Post
.select(['id', 'title'])
.first(20)
.orderBy({ createdAt: 'desc' });
if (cursor) {
query = query.after(cursor);
}
const result = await query.executePaginated();
for (const post of result.items) {
console.log(post.title);
}
// Advance only when there is another page AND a cursor to resume from;
// otherwise stop so we never re-fetch the first page.
cursor = result.hasNextPage ? result.endCursor : undefined;
} while (cursor);
function executePaginated(): Promise<PagedResult<GraphQLResponseValue<TResult>>>
Returns
Promise<PagedResult<GraphQLResponseValue<TResult>>>
A PagedResult containing the items plus endCursor and hasNextPage.
Use this when you need to paginate forward via .after() on a follow-up call.
findFirst()
Execute the query and return only the first matching record. The temporary limit is applied to an independent builder.
function findFirst(): Promise<GraphQLResponseValue<TResult> | null>
Returns
Promise<GraphQLResponseValue<TResult> | null>
The first matching record, or null if none match.
first(number)
Set the page size for this query.
Example
const firstPage = await client.data.Post
.first(20)
.orderBy({ createdAt: 'desc' })
.executePaginated();
if (firstPage.endCursor) {
const nextPage = await client.data.Post
.first(20)
.after(firstPage.endCursor)
.orderBy({ createdAt: 'desc' })
.executePaginated();
console.log(nextPage.items.length);
}
function first(count: number): RowQueryBuilder<TSchema, TEntity, GraphQLQueryBuilder<TSchema, TEntity, TResult>, GraphQLResponseValue<TResult>>
Parameters
- count
-
number
The maximum number of records to return in one page.
Returns
RowQueryBuilder<TSchema, TEntity, GraphQLQueryBuilder<TSchema, TEntity, TResult>, GraphQLResponseValue<TResult>>
This builder, for chaining.
Remarks
count is bounded by DAB's maximum page size (100,000). Pass -1 to
request an unbounded page, which DAB still caps at that maximum - so it
works only when the full result set fits under that limit. execute
still returns just this single page; use executePaginated with
after to retrieve result sets that span multiple pages.
groupBy<TGroup>(TGroup)
Start a grouped aggregation over the given scalar fields.
groupBy is DAB-compliant: the fields are emitted as unquoted GraphQL
enum tokens and the fields sub-selection under groupBy is derived
from this argument, so the selection and the grouping arguments cannot
diverge. Grouping is mutually exclusive with row selection: the returned
GroupedAggregationStage deliberately omits select,
first, after, and orderBy, so combining them with
grouping is now a compile-time error. (A runtime guard in
aggregate still backs this up for callers that bypass the types.)
Follow with GroupedAggregationStage.aggregate to specify the
aggregation values, then call .execute() on the returned aggregation
builder.
Example
const rows = await client.data.Order
.where({ status: { eq: 'shipped' } })
.groupBy(['region'])
.aggregate({ revenue: { sum: 'amount' } })
.execute();
function groupBy<TGroup>(fields: TGroup): GroupedAggregationStage<TSchema, TEntity, TGroup>
Parameters
- fields
-
TGroup
The scalar entity fields to group by.
Returns
GroupedAggregationStage<TSchema, TEntity, TGroup>
A grouped-aggregation stage carrying the exact grouped keys
TGroup, ready for a subsequent .aggregate(...) call.
orderBy(OrderByInput<TSchema[TEntity]>)
Add an order-by clause. Repeated calls append additional sort keys.
Sort directions must be lowercase ('asc' or 'desc') per DAB.
Example
await client.data.Post
.orderBy({ priority: 'desc' })
.orderBy({ createdAt: 'desc' })
.execute();
function orderBy(order: OrderByInput<TSchema[TEntity]>): RowQueryBuilder<TSchema, TEntity, GraphQLQueryBuilder<TSchema, TEntity, TResult>, GraphQLResponseValue<TResult>>
Parameters
- order
-
OrderByInput<TSchema[TEntity]>
The order-by specification (field to 'asc'/'desc').
Returns
RowQueryBuilder<TSchema, TEntity, GraphQLQueryBuilder<TSchema, TEntity, TResult>, GraphQLResponseValue<TResult>>
This builder, for chaining.
select<TFields>(TFields)
Select the fields (and nested relationship fields) to return.
Field paths use 'customer.name' for objects and 'wheel[].name'
for collections. Each path selects one scalar leaf; bare relationship
roots and deeper paths are unsupported. For filtering on a related entity, use
the foreign key column on .where() instead of a dot path.
Example
await client.data.Todo
.select(['id', 'title', 'category.name'])
.execute();
function select<TFields>(fields: TFields): RowQueryBuilder<TSchema, TEntity, GraphQLQueryBuilder<TSchema, TEntity, TResult>, SelectionResult<TSchema[TEntity], TFields>>
Parameters
- fields
-
TFields
The field selection. Use dot notation (e.g. 'author.name') for nested fields.
Returns
RowQueryBuilder<TSchema, TEntity, GraphQLQueryBuilder<TSchema, TEntity, TResult>, SelectionResult<TSchema[TEntity], TFields>>
An independently selected builder that preserves the current filters, ordering, and pagination.
where(FilterInput<TSchema[TEntity]>)
Add filter conditions. Repeated calls are merged together.
Successive calls combine via DAB's lowercase and.
Example
await client.data.Order
.where({ status: { eq: 'open' } })
.where({ customer_id: { eq: currentUserId } })
.execute();
function where(conditions: FilterInput<TSchema[TEntity]>): this
Parameters
- conditions
-
FilterInput<TSchema[TEntity]>
The filter conditions to apply.
Returns
this
This builder, for chaining.