Skip to content

Query API Reference

function GraphQLProvider<TSchema extends SchemaDescriptor, TDefs extends AnyEntityDefs>(props: {
client: GraphQLClient<TSchema, TDefs>
children: ReactNode
}): JSX.Element

React context provider that makes the GraphQL client available to all hooks in the subtree. Must be nested inside a TanStack Query QueryClientProvider.

function useGraphQLClient<
TSchema extends SchemaDescriptor = SchemaDescriptor,
TDefs extends AnyEntityDefs = AnyEntityDefs,
>(): GraphQLClient<TSchema, TDefs>

Returns the GraphQLClient instance from the nearest GraphQLProvider. Throws an error if called outside the provider tree.

function useEntity<
TSchema extends SchemaDescriptor,
TDefs extends AnyEntityDefs,
TEntityName extends string & keyof TSchema & keyof TDefs,
>(entityName: TEntityName): EntityClient<EntityDefsRef<TDefs>, TEntityName>

Returns a memoized entity client for the given entity name. Convenience wrapper around useGraphQLClient().entity(name).

Parameters:

ParameterTypeDescription
entityNamestringName of the entity in the schema

Returns: EntityClient — typed entity client with query/mutation methods.

function useEntityQuery<TDefs, TEntityName, TSelect>(
entity: EntityClient<EntityDefsRef<TDefs>, TEntityName>,
params: { select: TSelect; where?: Filters; orderBy?: OrderBy; offset?: number },
options?: EntityQueryOptions,
): UseQueryResult<InferResult<...> | null>

Fetches a single entity matching the filter. Wraps TanStack Query’s useQuery.

Parameters:

ParameterTypeDescription
entityEntityClientEntity client from client.entity() or useEntity()
params.selectselect objectFields and relations to include
params.wherefilter objectOptional filter conditions
params.orderBy{ [column]: { direction, priority } }Optional ordering
params.offsetnumberOptional offset

Options:

OptionTypeDescription
enabledbooleanEnable/disable the query
gcTimenumberGarbage collection time (ms)
staleTimenumberStale time (ms)
refetchOnWindowFocusbooleanRefetch on window focus
queryKeyunknown[]Custom query key

Returns: UseQueryResult<T | null> from TanStack Query.

function useEntityList<TDefs, TEntityName, TSelect>(
entity: EntityClient<EntityDefsRef<TDefs>, TEntityName>,
params: { select: TSelect; where?: Filters; orderBy?: OrderBy; limit?: number; offset?: number },
options?: EntityListOptions,
): UseQueryResult<InferResult<...>[]>

Fetches a list of entities. Wraps TanStack Query’s useQuery.

Parameters:

ParameterTypeDescription
entityEntityClientEntity client
params.selectselect objectFields and relations to include
params.wherefilter objectOptional filter conditions
params.orderBy{ [column]: { direction, priority } }Optional ordering
params.limitnumberOptional row limit
params.offsetnumberOptional offset

Options: Same as useEntityQuery.

Returns: UseQueryResult<T[]> from TanStack Query.

function useEntityInfiniteQuery<TDefs, TEntityName, TSelect>(
entity: EntityClient<EntityDefsRef<TDefs>, TEntityName>,
params: { select: TSelect; pageSize: number; where?: Filters; orderBy?: OrderBy },
options?: EntityInfiniteOptions,
): UseInfiniteQueryResult<{ pages: Array<{ items: T[]; count: number }> }>

Infinite-scroll pagination. Wraps TanStack Query’s useInfiniteQuery. Each page contains items (the fetched rows) and count (total matching rows).

Parameters:

ParameterTypeDescription
entityEntityClientEntity client
params.selectselect objectFields and relations to include
params.pageSizenumberItems per page
params.wherefilter objectOptional filter conditions
params.orderBy{ [column]: { direction, priority } }Optional ordering

Options:

OptionTypeDescription
enabledbooleanEnable/disable the query
gcTimenumberGarbage collection time (ms)
staleTimenumberStale time (ms)
queryKeyunknown[]Custom query key

Returns: UseInfiniteQueryResult from TanStack Query.

function useEntityInsert<TDefs, TEntityName, TSelect>(
entity: EntityClient<EntityDefsRef<TDefs>, TEntityName>,
returning?: TSelect,
options?: InsertOptions<InferResult<...>[]>,
): UseMutationResult<InferResult<...>[], Error, { values: InsertInput[] }>

Insert mutation. The mutate function accepts { values: InsertInput[] }.

Parameters:

ParameterTypeDescription
entityEntityClientEntity client
returningselect objectOptional fields to return from inserted rows
optionsmutation optionsOptional callbacks and cache settings

Options:

OptionTypeDefaultDescription
invalidatebooleantrueInvalidate queries after success
invalidateKeyunknown[]['gql']Query key prefix to invalidate
onSuccess(data) => voidSuccess callback
onError(error) => voidError callback

Returns: UseMutationResult from TanStack Query.

function useEntityUpdate<TDefs, TEntityName, TSelect>(
entity: EntityClient<EntityDefsRef<TDefs>, TEntityName>,
returning?: TSelect,
options?: UpdateOptions<InferResult<...>[]>,
): UseMutationResult<InferResult<...>[], Error, { set: UpdateInput; where?: Filters }>

Update mutation. The mutate function accepts { set: UpdateInput, where?: FilterInput }.

Parameters, Options, Returns: Same structure as useEntityInsert.

function useEntityDelete<TDefs, TEntityName, TSelect>(
entity: EntityClient<EntityDefsRef<TDefs>, TEntityName>,
returning?: TSelect,
options?: DeleteOptions<InferResult<...>[]>,
): UseMutationResult<InferResult<...>[], Error, { where?: Filters }>

Delete mutation. The mutate function accepts { where?: FilterInput }.

Parameters, Options, Returns: Same structure as useEntityInsert.

type GraphQLClientContext<
TSchema extends SchemaDescriptor = SchemaDescriptor,
TDefs extends AnyEntityDefs = AnyEntityDefs,
> = {
client: GraphQLClient<TSchema, TDefs>
}