Rayfin

@microsoft/rayfin-data

The fluent GraphQL query and mutation API behind client.data — dataService, select/where/orderBy, pagination, projected result types, and CRUD methods with exact signatures.

@microsoft/rayfin-data is the Data API Builder–compliant data client behind client.data. Register it on a RayfinClient with dataService<AppSchema>(). It is also a standalone package if you already own an ApiClient and want only the query builder.

Installation

npm install @microsoft/rayfin-data

The 2.0 client package no longer depends on @microsoft/rayfin-data, so install it in every app that uses client.data. The package now also publishes an exports map: import from @microsoft/rayfin-data, because deep imports into dist/ no longer resolve. Changed in 2.0

Register the data service

New in 2.0

function dataService<TSchema extends EntitySchema>(): ClientServiceFactory<
  DataApi<TSchema> & TypedDataClients<TSchema>
>;

dataService<TSchema>() registers the typed data client on RayfinClient.create, using the client's shared authenticated transport. It keeps /graphql routing, typed selections, relationships, the id conveniences, and Data API Builder mutation behavior.

src/services/rayfinClient.ts
import { RayfinClient } from '@microsoft/rayfin-client';
import { browserSession } from '@microsoft/rayfin-auth';
import { dataService } from '@microsoft/rayfin-data';
import type { AppSchema } from '../../rayfin/data/schema';

export const rayfinClient = RayfinClient.create({
  baseUrl: import.meta.env.VITE_RAYFIN_API_URL,
  publishableKey: import.meta.env.VITE_RAYFIN_PUBLISHABLE_KEY,
  auth: browserSession({ storage: true }),
  services: {
    data: dataService<AppSchema>(),
  },
});

export type AppClient = typeof rayfinClient;

Import EntitySchema and TypedDataClients from @microsoft/rayfin-data — the 2.0 client package no longer re-exports them. Changed in 2.0

createDataApi

Use createDataApi directly when you already own an ApiClient:

function createDataApi<TSchema extends EntitySchema>(
  apiClient: ApiClient,
): DataApi<TSchema> & TypedDataClients<TSchema>;

type TypedDataClients<TSchema extends EntitySchema> = {
  [K in keyof TSchema]: GraphQLEntityClient<TSchema, K>;
};

Each property on the result — dataApi.Todo, dataApi.Note, and so on — is a GraphQLEntityClient scoped to that entity. Its methods are bound to the entity client, so a detached call such as const { findById } = client.data.Todo keeps working. Changed in 2.0

Query chain

Changed in 2.0

GraphQLEntityClient exposes direct query methods and the entry points of a fluent builder (GraphQLQueryBuilder) for composing select / where / orderBy / pagination:

class GraphQLEntityClient<TSchema extends EntitySchema, TEntity extends keyof TSchema> {
  select<const TFields extends FieldSelection<TSchema[TEntity]>>(
    fields: TFields,
  ): RowQueryBuilder<
    TSchema,
    TEntity,
    GraphQLQueryBuilder<TSchema, TEntity>,
    SelectionResult<TSchema[TEntity], TFields>
  >;
  where(conditions: FilterInput<TSchema[TEntity]>): GraphQLQueryBuilder<TSchema, TEntity>;
  orderBy(order: OrderByInput<TSchema[TEntity]>): RowQueryBuilder<TSchema, TEntity>;
  first(count: number): RowQueryBuilder<TSchema, TEntity>;
  findMany(
    filter?: FilterInput<TSchema[TEntity]>,
  ): Promise<GraphQLResponseValue<PrimaryKeyOnly<TSchema[TEntity]>>[]>;
  findFirst(
    filter?: FilterInput<TSchema[TEntity]>,
  ): Promise<GraphQLResponseValue<PrimaryKeyOnly<TSchema[TEntity]>> | null>;
  findById(id: string): Promise<GraphQLResponseValue<PrimaryKeyOnly<TSchema[TEntity]>> | null>;
}

type RowQueryBuilder<
  TSchema extends EntitySchema,
  TEntity extends keyof TSchema,
  TSelf = GraphQLQueryBuilder<TSchema, TEntity>,
  TResult = GraphQLResponseValue<PrimaryKeyOnly<TSchema[TEntity]>>,
> = ProjectionSafeMembers<TSelf> & {
  select<const TFields extends FieldSelection<TSchema[TEntity]>>(
    fields: TFields,
  ): RowQueryBuilder<TSchema, TEntity, TSelf, SelectionResult<TSchema[TEntity], TFields>>;
  where(conditions: FilterInput<TSchema[TEntity]>): RowQueryBuilder<TSchema, TEntity, TSelf, TResult>;
  orderBy(order: OrderByInput<TSchema[TEntity]>): RowQueryBuilder<TSchema, TEntity, TSelf, TResult>;
  first(count: number): RowQueryBuilder<TSchema, TEntity, TSelf, TResult>;
  after(cursor: string): RowQueryBuilder<TSchema, TEntity, TSelf, TResult>;
  execute(): Promise<TResult[]>;
  executePaginated(): Promise<PagedResult<TResult>>;
  findFirst(): Promise<TResult | null>;
};
  • select() returns a new, independently selected builder that copies the current filters, ordering, and pagination. Calling it again on that builder replaces the selection. Always use the builder it returns — ignoring the return value leaves the original builder's selection unchanged.
  • where(), orderBy(), first(), and after() update the builder they are called on and return it, so they chain in any order before a terminal execute(), executePaginated(), or findFirst().
  • A RowQueryBuilder has no groupBy() or aggregate(). Once a query is shaped for rows, it cannot turn into an aggregation — see Aggregation.

Projected result types

Changed in 2.0

GraphQL responses contain only the fields a query requests, and the inferred TypeScript result matches that projection:

const note = await client.data.Note.select(['id', 'title']).findFirst();
// Type: { id: string; title: string } | null

note?.title; // valid
note?.content; // type error: content was not selected
Query shapeResult type
.select(fields).where(...).orderBy(...).first(...).after(...).execute()SelectionResult[]
.where(...).select(fields).execute()SelectionResult[]
.orderBy(...).select(fields).execute()SelectionResult[]
.first(...).select(fields).findFirst()SelectionResult | null
.after(...).select(fields).executePaginated()PagedResult<SelectionResult>
No .select()Primary-key-only result
.select([])Primary-key-only result
Widened or dynamic field arrayPossible selected fields are optional
  • A literal field array produces exact types. If the array is widened or built dynamically, every field it could contain is optional, because the compiler cannot know which entries are present at runtime. Alternatives inside a fixed tuple, such as [condition ? 'email' : 'displayName'], are optional for the same reason.
  • Selected Date fields are typed Date | string, and boolean fields boolean | 'true' | 'false', because responses can carry wire strings. The client does not infer a field's type from a string's contents — 1.x converted ISO-looking strings and "true"/"false" automatically, 2.0 does not — so convert explicitly before using date methods or boolean truthiness.
  • The client preserves the nullability declared in the schema type. An optional ? models undefined, not a returned null. Required @text() fields return an empty string for empty text; optional @text({ optional: true }) fields preserve null.
  • Queries without .select() request only the entity's primary key, so findMany(), findFirst(), and findById() return primary-key-only results. In 1.x these methods were typed as returning the full entity, but the runtime already requested only id.

Complete API result contract

APIResult typing
select(fields)Starts or replaces an exact projection; a widened field array produces optional possible fields
where(filter)Preserves the active projection; primary-key-only when no selection exists
orderBy(order)Preserves the active projection; primary-key-only when no selection exists
first(count)Preserves the active projection; primary-key-only when no selection exists
after(cursor)Preserves the active projection; primary-key-only when no selection exists
execute()ActiveResult[]
executePaginated()PagedResult<ActiveResult>
Fluent findFirst()ActiveResult | null
Entity findMany(filter?)PrimaryKeyOnly<Entity>[]
Entity findFirst(filter?)PrimaryKeyOnly<Entity> | null
Entity findById(id)PrimaryKeyOnly<Entity> | null
create(input)Primary key plus eligible input fields
update(where, input)Primary key plus eligible input fields
delete(where)PrimaryKeyOnly<Entity>
upsert(where, create, update)Union of create and update result shapes
groupBy(fields).aggregate(spec)Grouped aggregation result; cannot be combined with row selection
aggregate(spec)Ungrouped aggregation result; cannot be combined with row selection

ActiveResult is the projection of the most recent select(), or the primary key when there is none. To read a full row by id, select the fields and filter on the key:

const todo = await client.data.Todo.select(['id', 'title', 'isCompleted', 'createdAt'])
  .where({ id: { eq: todoId } })
  .findFirst();

Reading records

const notes = await client.data.Note.select([
  'id',
  'title',
  'createdAt',
  'notebook.id',      // relationship path — only valid inside select(), not where()
  'notebook.name',
])
  .where({ notebook_id: { eq: notebookId } })
  .orderBy({ createdAt: 'desc' })
  .execute();

.execute() returns a single page — the Data API caps a response at its default page size (100 records) even when the underlying table has more rows, and gives no signal that more records exist. Use it only for queries you know are bounded (a .where() filter that can match at most a handful of rows, or a small lookup table); for anything that can grow unbounded, use pagination instead.

Relationship paths

Changed in 2.0

type FieldSelection<T> = readonly NestedFieldPath<T>[];
// NestedFieldPath<T>: 'field' | 'relationship.field' | 'collection[].field'

select() accepts scalar field names and one level of relationship path:

  • An object (@one()) relationship uses a plain dot: 'notebook.name' returns a notebook object that contains only name.
  • An array (@many()) relationship needs the [] marker: 'notes[].title' emits notes { items { title } } and returns a notes array whose elements contain only title, preserving the array's readonly-ness and nullability.
  • Cardinality comes from the marker, not the name. An object relationship keeps the unmarked form even when its name looks plural.
  • Each path is one relationship followed by one scalar leaf. Bare relationship roots, object-valued leaves, and deeper paths are not selectable; Date and Buffer fields are scalar leaves.
  • Malformed paths and conflicting selections — one relationship selected both with and without [], or both bare and with a leaf — throw before the request is sent.

To migrate 1.x code, add [] to every array-relationship path ('notes.title' becomes 'notes[].title') and replace bare relationships with explicit scalar leaves. Object paths are unchanged. Connector entities keep their own selection syntax, whose cardinality comes from registered metadata.

Filtering — FilterInput

where() takes one entry per field, keyed to a type-specific filter shape, plus optional and / or arrays for boolean composition:

type FilterInput<T> = {
  [K in keyof T]?: FilterValue<T, K>;
} & {
  and?: FilterInput<T>[];
  or?: FilterInput<T>[];
};
Field typeFilter operators
string (StringFilterInput)eq, neq, gt, gte, lt, lte, contains, notContains, startsWith, endsWith, isNull, in
number (NumberFilterInput)eq, neq, gt, gte, lt, lte, isNull, in
boolean (BooleanFilterInput)eq, neq, isNull, in
Date (DateFilterInput)eq, neq, gt, gte, lt, lte, isNull, in
relationship field{ isNull: boolean } (only when the relationship itself is optional)

Filter by the foreign key column (customer_id), not a relationship dot-path (customer.id) — dot-paths are select-only. A relationship { isNull } filter is rewritten to its {property}_id foreign key only when the same query selects a field under that relationship; otherwise filter the declared foreign key directly. See Known limitations.

Sorting — OrderByInput

type OrderByInput<T> = { [K in keyof T]?: 'asc' | 'desc' };

Directions are lowercase strings, not an enum.

Pagination

interface PaginationConfig {
  first?: number;
  after?: string;
}

interface PagedResult<T> {
  items: T[];
  hasNextPage: boolean;
  endCursor?: string;
  totalCount?: number;
}

Data API Builder only supports forward pagination (first / after) — there is no before / last. Use .first(n) to set the page size and .executePaginated() to get a page plus cursor metadata; pass the previous page's endCursor to .after() for the next page:

const page = await client.data.Note.select(['id', 'title'])
  .orderBy({ createdAt: 'desc' })
  .first(25)
  .executePaginated();

// page.items, page.hasNextPage, page.endCursor

if (page.hasNextPage && page.endCursor) {
  const nextPage = await client.data.Note.select(['id', 'title'])
    .orderBy({ createdAt: 'desc' })
    .first(25)
    .after(page.endCursor)
    .executePaginated();
}

Keep select, where, and orderBy identical across pages — a stable sort order is required for the cursor to advance correctly. first(n) is bounded by DAB's maximum page size (100,000); totalCount is present on PagedResult but is not populated by DAB today. There is no count() method on the query chain. count exists as an aggregation operation over numeric fields — see Aggregation below. For a row count, select minimal fields and use results.length, or page.items.length per page. See Known limitations.

Aggregation

groupBy() and aggregate() are entry points on both GraphQLEntityClient and GraphQLQueryBuilder. They return a GraphQLAggregationBuilder, whose execute() resolves to one row per group. Calling aggregate() without groupBy() produces a single grand-total row whose fields is empty.

class GraphQLEntityClient<TSchema extends EntitySchema, TEntity extends keyof TSchema> {
  groupBy<const TGroup extends readonly ScalarKeys<TSchema[TEntity]>[]>(
    fields: TGroup,
  ): GroupedAggregationStage<TSchema, TEntity, TGroup>;
  aggregate<const TSpec extends AggregationSpec<TSchema[TEntity]>>(
    spec: TSpec,
  ): GraphQLAggregationBuilder<TSchema, TEntity, readonly [], TSpec>;
}

class GraphQLAggregationBuilder<TSchema, TEntity, TGroup, TSpec> {
  execute(): Promise<GroupedAggregationRow<TSchema[TEntity], TGroup, TSpec>[]>;
}

The specification is keyed by aliases you choose. Each entry holds exactly one operation, whose value is either a field-name shorthand or an options object:

type AggregationOps<T> = {
  sum: AggregationOpValue<NumericKeys<T>>;
  avg: AggregationOpValue<NumericKeys<T>>;
  min: AggregationOpValue<NumericKeys<T>>;
  max: AggregationOpValue<NumericKeys<T>>;
  count: AggregationOpValue<NumericKeys<T>>;
};

type AggregationOpValue<F> = F | { field: F; having?: NumberFilterInput; distinct?: boolean };
type AggregationSpec<T> = Record<string, ExactlyOne<AggregationOps<T>>>;

interface GroupedAggregationRow<T, G extends readonly ScalarKeys<T>[], S> {
  fields: GraphQLResponseValue<Pick<T, G[number] & keyof T>>;
  aggregations: { [K in Extract<keyof S, string>]: AggregationResult<S[K]> };
}

fields is wrapped in GraphQLResponseValue in 2.0, so a grouped Date field is typed Date | string and a grouped boolean boolean | 'true' | 'false', matching row results. Changed in 2.0

AggregationResult is number for count and number | null for sum / avg / min / max, because DAB emits those as nullable and SQL returns NULL over an empty or all-null group.

const rows = await client.data.Order
  .where({ status: { eq: 'shipped' } })
  .groupBy(['region'])
  .aggregate({
    revenue: { sum: 'amount' },
    biggest: { max: { field: 'amount', having: { gt: 500 } } },
  })
  .execute();

Note

Every operation — count included — is typed against NumericKeys<T>, because DAB generates each aggregation's field argument as the entity's NumericAggregateFields enum. Non-numeric fields are a compile error.

Aliases and field tokens are validated at runtime against the GraphQL name grammar (/^[_A-Za-z][_0-9A-Za-z]*$/, and must not begin with __). DAB rejects a query combining groupBy with items, so aggregate() throws if it follows select(), first(), after(), or orderBy(); the RowQueryBuilder return type makes those combinations compile-time errors as well.

Mutations

Changed in 2.0

class GraphQLEntityClient<TSchema extends EntitySchema, TEntity extends keyof TSchema> {
  create<const TInput extends CreateInput<TSchema[TEntity]>>(
    input: TInput,
    ..._invalidKeys: NoExtraKeys<TInput, CreateInput<TSchema[TEntity]>>
  ): Promise<MutationResult<TSchema[TEntity], TInput>>;
  update<const TInput extends UpdateInput<TSchema[TEntity]>>(
    where: WhereUniqueInput<TSchema[TEntity]>,
    data: TInput,
    ..._invalidKeys: NoExtraKeys<TInput, UpdateInput<TSchema[TEntity]>>
  ): Promise<MutationResult<TSchema[TEntity], TInput>>;
  delete(
    where: WhereUniqueInput<TSchema[TEntity]>,
  ): Promise<GraphQLResponseValue<PrimaryKeyOnly<TSchema[TEntity]>>>;
  upsert<
    const TCreate extends CreateInput<TSchema[TEntity]>,
    const TUpdate extends UpdateInput<TSchema[TEntity]>,
  >(
    where: WhereUniqueInput<TSchema[TEntity]>,
    create: TCreate,
    update: TUpdate,
    ..._invalidKeys: [
      ...NoExtraKeys<TCreate, CreateInput<TSchema[TEntity]>>,
      ...NoExtraKeys<TUpdate, UpdateInput<TSchema[TEntity]>>,
    ]
  ): Promise<MutationResult<TSchema[TEntity], TCreate> | MutationResult<TSchema[TEntity], TUpdate>>;
}

// MutationInput<T> turns @one() relationship fields into keyed references
type CreateInput<T> = Omit<MutationInput<T>, 'id'> & Partial<Pick<T, 'id'>>;
type UpdateInput<T> = Partial<MutationInput<T>>;
type WhereUniqueInput<T> = { id: string };

NoExtraKeys rejects fields the entity does not declare at compile time. Each method also has overloads that accept ComputedMutationInput for objects built with computed keys such as { [field]: value }; when TypeScript widens a computed key, the result guarantees only the primary key (plus the required fields, for create), and other possible fields need optional guards. create, update, delete, and upsert run immediately — they do not go through the query builder.

OperationResult type
create(input)Primary key plus eligible input fields
update(where, input)Primary key plus eligible input fields
delete(where)Primary key only
upsert(where, create, update)Union of the create and update result shapes

Eligible input fields are scalars (including null), Date values, and relationship values that include their key. Fields passed as optional or possibly undefined stay optional in the result, and returned values are wrapped in GraphQLResponseValue like query results. A relationship input comes back as an object holding only its key, as lowercase id. Non-Date objects without an id or Id, including Buffer, are omitted from the mutation and its result. Array inputs are serialized, but the result does not guarantee them.

const todo = await client.data.Todo.create({
  title: 'Write docs',
  isCompleted: false,
  createdAt: new Date(),
  user_id: session.user.id,
});
// todo: { id: string; title: string; isCompleted: boolean | 'true' | 'false';
//         createdAt: Date | string; user_id: string }

await client.data.Todo.update({ id: todo.id }, { isCompleted: true });

await client.data.Todo.delete({ id: todo.id }); // { id: string }

await client.data.Todo.upsert(
  { id: todo.id },
  {
    title: 'Write docs',
    isCompleted: false,
    createdAt: new Date(),
    user_id: session.user.id,
  },
  { isCompleted: true },
);

Relationship fields in mutations

Changed in 2.0

A @one() relationship field in create / update input takes a reference that includes a concrete primary key: { id } alone, a full entity, or a projected object containing the key plus some known fields. Only the key is serialized, into the {property}_id foreign key, and only the key is returned:

// Key-only reference (recommended)
await client.data.Note.create({
  title: 'Meeting notes',
  content: 'Discussion points',
  createdAt: new Date(),
  user_id: session.user.id,
  notebook: { id: notebookId },
});

// A projected object also works — only notebook.id is written
const notebook = await client.data.Notebook.select(['id', 'name'])
  .where({ id: { eq: notebookId } })
  .findFirst();

if (notebook) {
  await client.data.Note.create({
    title: 'Weekly summary',
    content: 'Status for the week',
    createdAt: new Date(),
    user_id: session.user.id,
    notebook,
  });
}
  • Each keyed reference must supply its own non-nullish id (or Id). The client rejects a relationship object whose key is null or undefined before it sends the mutation; a present lowercase id takes priority over Id.
  • Relationship values cannot be null. Omit an optional relationship (leave it undefined) instead. The client sends a foreign key scalar set to null as-is, so a schema whose foreign key type includes null can clear the relationship with { category_id: null }; nullable scalar and Date columns accept null the same way.
  • You can pass the foreign key scalar (notebook_id) instead of the relationship object when the entity declares it. Pass one or the other — if both are supplied for the same relationship, the client sends a single foreign key value and the relationship object wins.
  • Manage one-to-many relationships through the child entity. The serializer does not expand a @many() array into child writes, so do not pass relationship arrays as mutation input.

Advanced: the underlying GraphQL client

@microsoft/rayfin-data also exports the lower-level pieces GraphQLEntityClient builds on, for advanced or standalone use:

class GraphQLClient {
  constructor(apiClient: ApiClient, endpoint?: string);
  request<T = any>(query: string, variables?: Record<string, any>, operationName?: string): Promise<T>;
  query<T = any>(query: string, variables?: Record<string, any>): Promise<T>;
  mutation<T = any>(mutation: string, variables?: Record<string, any>): Promise<T>;
}

Most applications should use client.data.<Entity> rather than calling GraphQLClient directly — it exists so GraphQLEntityClient and GraphQLQueryBuilder have a raw query/mutation execution primitive to build on.

Subclass GraphQLQueryBuilder

New in 2.0

Because select() and the fluent findFirst() construct a new builder, a custom GraphQLQueryBuilder subclass is constructed normally for each selection, so private fields and bound methods stay valid. A subclass whose constructor takes extra arguments must override createSelectionClone(), and a subclass with mutable state can override copySelectionStateTo().

Custom members are hidden on row-query builders unless the subclass lists their keys in a readonly projectionSafeMembers declaration. Only opt in members that never expose a builder or reset its result type, including through promises, unions, or overloads:

class LabeledQuery extends GraphQLQueryBuilder<AppSchema, 'Todo'> {
  declare readonly projectionSafeMembers: 'label';

  label(): string {
    return 'todos';
  }
}
Something wrong on this page?Report an issueEdit this page

On this page