GraphQLEntityClient class

Type-safe fluent client for reading and mutating a single Data API Builder entity. Instances are typically obtained via the DataApi proxy (dataApi.<Entity>) rather than constructed directly.

Query methods (select, where, orderBy, first) return a chainable GraphQLQueryBuilder; call .execute() to run the query. Direct methods (findMany, findFirst, findById) and mutation methods (create, update, delete, upsert) execute immediately.

Example

const users = await dataApi.User
  .select(['id', 'name', 'email'])
  .where({ isActive: { eq: true } })
  .orderBy({ createdAt: 'desc' })
  .execute();

const created = await dataApi.User.create({ name: 'Jane', email: 'jane@example.com' });

Methods

aggregate<TSpec>(TSpec)

Execute a grand-total aggregation over the entire filtered set, with no grouping fields.

create<TInput>(ComputedMutationInput<TInput, CreateInput<TSchema[TEntity]>>)
create<TInput>(TInput, NoExtraKeys<TInput, CreateInput<TSchema[TEntity]>>)

Create a new record.

For relationship fields, pass { id: '...' } as the value - the client converts that into the corresponding foreign key column on the wire (e.g. category: { id: 'abc' } becomes category_id: 'abc').

Example

const created = await client.data.Todo.create({
  title: 'Write the proposal',
  isCompleted: false,
  category: { id: categoryId },
  user_id: claims.sub,
});
console.log('Created todo', created.id);
delete(WhereUniqueInput<TSchema[TEntity]>)

Delete a record identified by its primary key.

DAB returns the selected primary key from the row before deletion.

Example

await client.data.Todo.delete({ id: todoId });
findById(string)

Fetch a single record by its primary key.

Use this instead of findByPk (which does not exist on this client) and instead of .where({ id: { eq: x } }).execute() when you want the single-record convenience and null-on-missing semantics.

Example

const todo = await client.data.Todo.findById('11111111-2222-3333-4444-555555555555');
if (!todo) {
  console.log('Not found');
  return;
}
console.log(todo.id);
findFirst(FilterInput<TSchema[TEntity]>)

Fetch the first record matching an optional filter.

findMany(FilterInput<TSchema[TEntity]>)

Fetch all records matching an optional filter.

first(number)

Start a query limited to the first count results.

For cursor-based pagination, follow up with .executePaginated() instead of .execute() so you can pass endCursor to a later .after() call.

Example

const page = await client.data.Post
  .first(20)
  .orderBy({ createdAt: 'desc' })
  .executePaginated();
console.log(page.items.length, page.endCursor);
groupBy<TGroup>(TGroup)

Start a grouped aggregation query.

The generated GraphQL contains only a groupBy selection and never emits items, endCursor, or hasNextPage, because Data API Builder rejects a query that combines groupBy with items. Follow with .aggregate(...) and then .execute().

Example

const rows = await client.data.Order
  .groupBy(['region'])
  .aggregate({ revenue: { sum: 'amount' } })
  .execute();
orderBy(OrderByInput<TSchema[TEntity]>)

Start a query ordered by the given fields.

Sort directions must be lowercase ('asc' or 'desc') per DAB.

Example

const recent = await client.data.Post
  .orderBy({ publishedAt: 'desc' })
  .first(10)
  .execute();
select<TFields>(TFields)

Start a query selecting the given fields.

Field paths use 'category.name' for objects and 'wheel[].name' for collections, with one scalar leaf per path. Bare relationship roots and deeper paths are unsupported. Filtering on a related entity must use the foreign key column (e.g. category_id), not the dot path.

Example

const todos = await client.data.Todo
  .select(['id', 'title', 'isCompleted', 'category.name'])
  .where({ isCompleted: { eq: false } })
  .orderBy({ createdAt: 'desc' })
  .first(20)
  .execute();
update<TInput>(WhereUniqueInput<TSchema[TEntity]>, ComputedMutationInput<TInput, Partial<MutationInput<TSchema[TEntity]>>>)
update<TInput>(WhereUniqueInput<TSchema[TEntity]>, TInput, NoExtraKeys<TInput, Partial<MutationInput<TSchema[TEntity]>>>)

Update an existing record identified by its primary key.

Only the fields present in data are sent; omitted fields are untouched.

Example

const updated = await client.data.Todo.update(
  { id: todoId },
  { isCompleted: true }
);
console.log('Updated', updated.id, updated.isCompleted);
upsert<TCreate, TUpdate>(WhereUniqueInput<TSchema[TEntity]>, ComputedMutationInput<TCreate, CreateInput<TSchema[TEntity]>>, ComputedMutationInput<TUpdate, Partial<MutationInput<TSchema[TEntity]>>>)
upsert<TCreate, TUpdate>(WhereUniqueInput<TSchema[TEntity]>, ComputedMutationInput<TCreate, CreateInput<TSchema[TEntity]>>, TUpdate, NoExtraKeys<TUpdate, Partial<MutationInput<TSchema[TEntity]>>>)
upsert<TCreate, TUpdate>(WhereUniqueInput<TSchema[TEntity]>, TCreate, ComputedMutationInput<TUpdate, Partial<MutationInput<TSchema[TEntity]>>>, NoExtraKeys<TCreate, CreateInput<TSchema[TEntity]>>)
upsert<TCreate, TUpdate>(WhereUniqueInput<TSchema[TEntity]>, TCreate, TUpdate, [Object, Object])

Update a record if it exists, otherwise create it.

where(FilterInput<TSchema[TEntity]>)

Start a query filtered by the given conditions.

Filter operators are object-shaped per DAB convention ({ field: { eq: value } }, { field: { in: [...] } }, etc.) and combine via lowercase and / or. Filter on foreign-key columns (e.g. customer_id) rather than the dot path of the relationship.

Example

const open = await client.data.Order
  .where({
    and: [
      { status: { eq: 'open' } },
      { customer_id: { eq: currentUserId } },
    ],
  })
  .orderBy({ createdAt: 'desc' })
  .execute();

Constructor Details

GraphQLEntityClient<TSchema, TEntity>(GraphQLClient, TEntity)

new GraphQLEntityClient(graphqlClient: GraphQLClient, entityName: TEntity)

Parameters

graphqlClient
GraphQLClient

The underlying GraphQLClient used to send requests.

entityName

TEntity

The entity name this client operates on.

Method Details

aggregate<TSpec>(TSpec)

Execute a grand-total aggregation over the entire filtered set, with no grouping fields.

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.

create<TInput>(ComputedMutationInput<TInput, CreateInput<TSchema[TEntity]>>)

function create<TInput>(input: ComputedMutationInput<TInput, CreateInput<TSchema[TEntity]>>): Promise<MutationResult<TSchema[TEntity], CreateInput<TSchema[TEntity]>>>

Parameters

input

ComputedMutationInput<TInput, CreateInput<TSchema[TEntity]>>

Returns

Promise<MutationResult<TSchema[TEntity], CreateInput<TSchema[TEntity]>>>

create<TInput>(TInput, NoExtraKeys<TInput, CreateInput<TSchema[TEntity]>>)

Create a new record.

For relationship fields, pass { id: '...' } as the value - the client converts that into the corresponding foreign key column on the wire (e.g. category: { id: 'abc' } becomes category_id: 'abc').

Example

const created = await client.data.Todo.create({
  title: 'Write the proposal',
  isCompleted: false,
  category: { id: categoryId },
  user_id: claims.sub,
});
console.log('Created todo', created.id);
function create<TInput>(input: TInput, _invalidKeys: NoExtraKeys<TInput, CreateInput<TSchema[TEntity]>>): Promise<MutationResult<TSchema[TEntity], TInput>>

Parameters

input

TInput

The fields for the new record. Relationship objects are converted to their foreign-key values.

_invalidKeys

NoExtraKeys<TInput, CreateInput<TSchema[TEntity]>>

Returns

Promise<MutationResult<TSchema[TEntity], TInput>>

The created record.

delete(WhereUniqueInput<TSchema[TEntity]>)

Delete a record identified by its primary key.

DAB returns the selected primary key from the row before deletion.

Example

await client.data.Todo.delete({ id: todoId });
function delete(where: WhereUniqueInput<TSchema[TEntity]>): Promise<GraphQLResponseValue<PrimaryKeyOnly<TSchema[TEntity]>>>

Parameters

where

WhereUniqueInput<TSchema[TEntity]>

The unique key identifying the record to delete.

Returns

Promise<GraphQLResponseValue<PrimaryKeyOnly<TSchema[TEntity]>>>

The deleted record's primary key.

findById(string)

Fetch a single record by its primary key.

Use this instead of findByPk (which does not exist on this client) and instead of .where({ id: { eq: x } }).execute() when you want the single-record convenience and null-on-missing semantics.

Example

const todo = await client.data.Todo.findById('11111111-2222-3333-4444-555555555555');
if (!todo) {
  console.log('Not found');
  return;
}
console.log(todo.id);
function findById(id: string): Promise<GraphQLResponseValue<PrimaryKeyOnly<TSchema[TEntity]>> | null>

Parameters

id

string

The primary key value to look up.

Returns

Promise<GraphQLResponseValue<PrimaryKeyOnly<TSchema[TEntity]>> | null>

The matching record, or null if not found.

findFirst(FilterInput<TSchema[TEntity]>)

Fetch the first record matching an optional filter.

function findFirst(filter?: FilterInput<TSchema[TEntity]>): Promise<GraphQLResponseValue<PrimaryKeyOnly<TSchema[TEntity]>> | null>

Parameters

filter

FilterInput<TSchema[TEntity]>

Optional filter conditions.

Returns

Promise<GraphQLResponseValue<PrimaryKeyOnly<TSchema[TEntity]>> | null>

The first matching record, or null if none match.

findMany(FilterInput<TSchema[TEntity]>)

Fetch all records matching an optional filter.

function findMany(filter?: FilterInput<TSchema[TEntity]>): Promise<GraphQLResponseValue<PrimaryKeyOnly<TSchema[TEntity]>>[]>

Parameters

filter

FilterInput<TSchema[TEntity]>

Optional filter conditions. Omit to fetch all records.

Returns

Promise<GraphQLResponseValue<PrimaryKeyOnly<TSchema[TEntity]>>[]>

The matching records.

first(number)

Start a query limited to the first count results.

For cursor-based pagination, follow up with .executePaginated() instead of .execute() so you can pass endCursor to a later .after() call.

Example

const page = await client.data.Post
  .first(20)
  .orderBy({ createdAt: 'desc' })
  .executePaginated();
console.log(page.items.length, page.endCursor);
function first(count: number): RowQueryBuilder<TSchema, TEntity>

Parameters

count

number

The maximum number of records to return.

Returns

RowQueryBuilder<TSchema, TEntity>

A chainable GraphQLQueryBuilder; call .execute() to run it.

groupBy<TGroup>(TGroup)

Start a grouped aggregation query.

The generated GraphQL contains only a groupBy selection and never emits items, endCursor, or hasNextPage, because Data API Builder rejects a query that combines groupBy with items. Follow with .aggregate(...) and then .execute().

Example

const rows = await client.data.Order
  .groupBy(['region'])
  .aggregate({ revenue: { sum: 'amount' } })
  .execute();
function groupBy<TGroup>(fields: TGroup): GroupedAggregationStage<TSchema, TEntity, TGroup>

Parameters

fields

TGroup

The scalar fields to group by. Emitted as unquoted GraphQL enum tokens and mirrored into the fields sub-selection.

Returns

GroupedAggregationStage<TSchema, TEntity, TGroup>

A GroupedAggregationStage carrying the grouped keys; call .aggregate(...) next.

orderBy(OrderByInput<TSchema[TEntity]>)

Start a query ordered by the given fields.

Sort directions must be lowercase ('asc' or 'desc') per DAB.

Example

const recent = await client.data.Post
  .orderBy({ publishedAt: 'desc' })
  .first(10)
  .execute();
function orderBy(order: OrderByInput<TSchema[TEntity]>): RowQueryBuilder<TSchema, TEntity>

Parameters

order

OrderByInput<TSchema[TEntity]>

The order-by specification (field to 'asc'/'desc').

Returns

RowQueryBuilder<TSchema, TEntity>

A chainable GraphQLQueryBuilder; call .execute() to run it.

select<TFields>(TFields)

Start a query selecting the given fields.

Field paths use 'category.name' for objects and 'wheel[].name' for collections, with one scalar leaf per path. Bare relationship roots and deeper paths are unsupported. Filtering on a related entity must use the foreign key column (e.g. category_id), not the dot path.

Example

const todos = await client.data.Todo
  .select(['id', 'title', 'isCompleted', 'category.name'])
  .where({ isCompleted: { eq: false } })
  .orderBy({ createdAt: 'desc' })
  .first(20)
  .execute();
function select<TFields>(fields: TFields): RowQueryBuilder<TSchema, TEntity, GraphQLQueryBuilder<TSchema, TEntity, PrimaryKeyOnly<TSchema[TEntity]>>, SelectionResult<TSchema[TEntity], TFields>>

Parameters

fields

TFields

The fields (and nested selections) to return.

Returns

RowQueryBuilder<TSchema, TEntity, GraphQLQueryBuilder<TSchema, TEntity, PrimaryKeyOnly<TSchema[TEntity]>>, SelectionResult<TSchema[TEntity], TFields>>

A chainable GraphQLQueryBuilder; call .execute() to run it.

update<TInput>(WhereUniqueInput<TSchema[TEntity]>, ComputedMutationInput<TInput, Partial<MutationInput<TSchema[TEntity]>>>)

function update<TInput>(where: WhereUniqueInput<TSchema[TEntity]>, data: ComputedMutationInput<TInput, Partial<MutationInput<TSchema[TEntity]>>>): Promise<MutationResult<TSchema[TEntity], Partial<MutationInput<TSchema[TEntity]>>>>

Parameters

where

WhereUniqueInput<TSchema[TEntity]>

data

ComputedMutationInput<TInput, Partial<MutationInput<TSchema[TEntity]>>>

Returns

Promise<MutationResult<TSchema[TEntity], Partial<MutationInput<TSchema[TEntity]>>>>

update<TInput>(WhereUniqueInput<TSchema[TEntity]>, TInput, NoExtraKeys<TInput, Partial<MutationInput<TSchema[TEntity]>>>)

Update an existing record identified by its primary key.

Only the fields present in data are sent; omitted fields are untouched.

Example

const updated = await client.data.Todo.update(
  { id: todoId },
  { isCompleted: true }
);
console.log('Updated', updated.id, updated.isCompleted);
function update<TInput>(where: WhereUniqueInput<TSchema[TEntity]>, data: TInput, _invalidKeys: NoExtraKeys<TInput, Partial<MutationInput<TSchema[TEntity]>>>): Promise<MutationResult<TSchema[TEntity], TInput>>

Parameters

where

WhereUniqueInput<TSchema[TEntity]>

The unique key identifying the record to update.

data

TInput

The fields to change.

_invalidKeys

NoExtraKeys<TInput, Partial<MutationInput<TSchema[TEntity]>>>

Returns

Promise<MutationResult<TSchema[TEntity], TInput>>

The updated record.

upsert<TCreate, TUpdate>(WhereUniqueInput<TSchema[TEntity]>, ComputedMutationInput<TCreate, CreateInput<TSchema[TEntity]>>, ComputedMutationInput<TUpdate, Partial<MutationInput<TSchema[TEntity]>>>)

function upsert<TCreate, TUpdate>(where: WhereUniqueInput<TSchema[TEntity]>, create: ComputedMutationInput<TCreate, CreateInput<TSchema[TEntity]>>, update: ComputedMutationInput<TUpdate, Partial<MutationInput<TSchema[TEntity]>>>): Promise<MutationResult<TSchema[TEntity], CreateInput<TSchema[TEntity]>> | MutationResult<TSchema[TEntity], Partial<MutationInput<TSchema[TEntity]>>>>

Parameters

where

WhereUniqueInput<TSchema[TEntity]>

create

ComputedMutationInput<TCreate, CreateInput<TSchema[TEntity]>>

update

ComputedMutationInput<TUpdate, Partial<MutationInput<TSchema[TEntity]>>>

Returns

Promise<MutationResult<TSchema[TEntity], CreateInput<TSchema[TEntity]>> | MutationResult<TSchema[TEntity], Partial<MutationInput<TSchema[TEntity]>>>>

upsert<TCreate, TUpdate>(WhereUniqueInput<TSchema[TEntity]>, ComputedMutationInput<TCreate, CreateInput<TSchema[TEntity]>>, TUpdate, NoExtraKeys<TUpdate, Partial<MutationInput<TSchema[TEntity]>>>)

function upsert<TCreate, TUpdate>(where: WhereUniqueInput<TSchema[TEntity]>, create: ComputedMutationInput<TCreate, CreateInput<TSchema[TEntity]>>, update: TUpdate, _invalidKeys: NoExtraKeys<TUpdate, Partial<MutationInput<TSchema[TEntity]>>>): Promise<MutationResult<TSchema[TEntity], CreateInput<TSchema[TEntity]>> | MutationResult<TSchema[TEntity], TUpdate>>

Parameters

where

WhereUniqueInput<TSchema[TEntity]>

create

ComputedMutationInput<TCreate, CreateInput<TSchema[TEntity]>>

update

TUpdate

_invalidKeys

NoExtraKeys<TUpdate, Partial<MutationInput<TSchema[TEntity]>>>

Returns

Promise<MutationResult<TSchema[TEntity], CreateInput<TSchema[TEntity]>> | MutationResult<TSchema[TEntity], TUpdate>>

upsert<TCreate, TUpdate>(WhereUniqueInput<TSchema[TEntity]>, TCreate, ComputedMutationInput<TUpdate, Partial<MutationInput<TSchema[TEntity]>>>, NoExtraKeys<TCreate, CreateInput<TSchema[TEntity]>>)

function upsert<TCreate, TUpdate>(where: WhereUniqueInput<TSchema[TEntity]>, create: TCreate, update: ComputedMutationInput<TUpdate, Partial<MutationInput<TSchema[TEntity]>>>, _invalidKeys: NoExtraKeys<TCreate, CreateInput<TSchema[TEntity]>>): Promise<MutationResult<TSchema[TEntity], Partial<MutationInput<TSchema[TEntity]>>> | MutationResult<TSchema[TEntity], TCreate>>

Parameters

where

WhereUniqueInput<TSchema[TEntity]>

create

TCreate

update

ComputedMutationInput<TUpdate, Partial<MutationInput<TSchema[TEntity]>>>

_invalidKeys

NoExtraKeys<TCreate, CreateInput<TSchema[TEntity]>>

Returns

Promise<MutationResult<TSchema[TEntity], Partial<MutationInput<TSchema[TEntity]>>> | MutationResult<TSchema[TEntity], TCreate>>

upsert<TCreate, TUpdate>(WhereUniqueInput<TSchema[TEntity]>, TCreate, TUpdate, [Object, Object])

Update a record if it exists, otherwise create it.

function upsert<TCreate, TUpdate>(where: WhereUniqueInput<TSchema[TEntity]>, create: TCreate, update: TUpdate, _invalidKeys: [Object, Object]): Promise<MutationResult<TSchema[TEntity], TCreate> | MutationResult<TSchema[TEntity], TUpdate>>

Parameters

where

WhereUniqueInput<TSchema[TEntity]>

The unique key used to check for an existing record.

create

TCreate

The fields to use when creating a new record.

update

TUpdate

The fields to apply when updating an existing record.

_invalidKeys

[Object, Object]

Returns

Promise<MutationResult<TSchema[TEntity], TCreate> | MutationResult<TSchema[TEntity], TUpdate>>

The created or updated record.

where(FilterInput<TSchema[TEntity]>)

Start a query filtered by the given conditions.

Filter operators are object-shaped per DAB convention ({ field: { eq: value } }, { field: { in: [...] } }, etc.) and combine via lowercase and / or. Filter on foreign-key columns (e.g. customer_id) rather than the dot path of the relationship.

Example

const open = await client.data.Order
  .where({
    and: [
      { status: { eq: 'open' } },
      { customer_id: { eq: currentUserId } },
    ],
  })
  .orderBy({ createdAt: 'desc' })
  .execute();
function where(conditions: FilterInput<TSchema[TEntity]>): GraphQLQueryBuilder<TSchema, TEntity>

Parameters

conditions

FilterInput<TSchema[TEntity]>

The filter conditions to apply.

Returns

GraphQLQueryBuilder<TSchema, TEntity>

A chainable GraphQLQueryBuilder; call .execute() to run it.