Skip to Content
API ReferenceChoosing an API Style

Choosing an API Style

Every read and write on the Foir API is a GraphQL document, and the same executor and scope checks run every one of them. Most are sent to POST /graphql; a read that can be shared may go through /cdn/graphql instead (see Response Caching).

What you choose is not a transport. It is who writes the document:

  • @foir/sdk/data — the generated client. You call foir.page.findUnique({ naturalKey: 'home' }) and the document is printed for you from the selection your project publishes, typed by your API key. No .graphql files, no codegen pipeline, no graphql package in your bundle.
  • @foir/sdk/client, or any GraphQL client — you write the document. Full control over the selection, several root fields per request, fragments, and graphql-codegen for types.

They share the endpoint, the credentials and the environment variables, so most non-trivial apps use both. There is no migration between them and nothing is lost by mixing. Which credential a client holds is a separate question with one answer per caller: your app’s publishable key (or its server key, on the server that renders its pages) and the user’s own bearer, an operation endpoint’s dispatch token, or a machine’s admin key (App architecture).

The short version

Use the generated client for application code: typed reads and writes, shaped by your key, with a compile error instead of a 403 when you reach for something the key cannot do.

Write the document yourself when you want field-level control inside a nested object, several roots in one round trip, or a shared fragment library.

foir.raw(document, variables?) is the second option without a second import: a document you wrote, executed on the client you already have.

The honest trade-offs

You get every field on the model

The generated client is complete by default: it selects every scalar on the model. A document you write selects what you name.

What completeness buys is that the model type is unconditionally true — no field is typed present and arrives undefined, which is the failure mode every partial-selection client carries. What it costs is bytes on reads where you wanted three fields out of forty.

pick and omit narrow it, three levels into a composite, and a pick request produces exactly the same document the same selection written by hand would. What they do not reach is inside a list of composites or inside a union — those are selected whole. If your payloads are large, your renders are narrow, or you need a few fragments out of a big block union, write the document. Selection and Responses shows the real payloads.

A schema change reaches you when you regenerate

This is the one that surprises people, and it is the same on both sides for different reasons.

The generated client resolves its selection at build time, so a field added upstream joins your responses after foir types plus a deploy, not on the next request. A document you wrote never picks up a new field at all, because you never named it.

Neither is “live”. The difference is that the generated client tells you when you are behind (the module records the surface it was generated from: foir types --check compares it in CI, and a client built with checkSchemaDrift: true reports drift once per instance), where a hand-written document has no equivalent signal. See schema drift.

The SDK is one operation per request

A typed call prints exactly one root field and reads one root field back. Three reads is three requests.

On an edge runtime close to the API that is often fine; over a slow link with a waterfall it is not. A document of your own can fetch a page, its navigation and the current user in one round trip — through foir.raw() if you want to stay on the same client.

Writing documents needs a toolchain to be typed

Typed hand-written GraphQL means graphql-codegen, a schema fetch, generated documents and a graphql runtime dependency. It works well and Foir supports it — but it is a build step to own, and on an edge bundle the parser is real weight.

foir types is one command producing one file, and the generated client has no dependencies beyond the SDK.

Neither is more secure

Both send a document to the same endpoint, and the executor authorizes it identically. A publishable key writes only what a signed-in user’s role grants, or what you opened to visitors, whichever one you used.

The generated client’s per-key type shaping is a convenience: an operation your key cannot perform is absent from your generated types, so you find out at compile time instead of in production. It is not the boundary. The server rejects the call regardless of what your types said.

User authentication

The auth surface is a namespace like any other. Your manifest publishes it as auth, so authConfig, authProviders, currentUser, logout, refreshToken, the password-reset mutations, a person’s own addresses, exportMyData, deleteMyAccount and the OAuth/OIDC provider flows all have typed operations:

const me = await foir.ns.auth.currentUser();

Prefer @foir/sdk/auth anyway. It calls those same operations underneath, and it owns the part that is actually hard: storing the token, scheduling the refresh, handling the redirect back from a provider, and putting the result in a React context. Calling the raw operations yourself means reimplementing that.

So: authenticate with the auth SDK, then hand the token to the data client as bearerToken. Reach for foir.ns.auth.* when you want one specific read — currentUser on a server route, say — rather than a session.

Which operations appear is a projection, as everywhere else, and it projects the project’s configuration as well as your key’s scopes: a project with password sign-in disabled publishes no login. foir.ns.auth.authConfig() is how you ask which methods are enabled.

Design tokens

Also a namespace. Your project’s tokens are foir.ns.project.designTokens() — one call, complete by default, returning every colour, font family, size, radius, shadow and typography entry your project defines:

const tokens = await foir.ns.project.designTokens();

They are a whole-project asset rather than a record, so there is no pick, no omit and nothing to paginate. Read them once at render time and cache them; they change when someone publishes tokens, not per request. The CDN route is the other way to get them, and is the better one when a build step can bake them in.

The app marketplace

The Foir app marketplace is a namespace too, and it is the same for every project: marketplace carries the published catalogue, readable with any key, a publishable one included.

const { items, total } = await foir.ns.marketplace.marketplaceListings({ category: 'commerce' }); const detail = await foir.ns.marketplace.marketplaceListing({ slug: 'shopify' });

marketplaceListing returns the listing and the app’s manifest as its publisher serves it now, in the proto JSON form of AppConfig. That is where what the app may read and write comes from. When the manifest cannot be fetched, manifest is null and manifestError says why; the listing still comes back. An unpublished listing is not found.

Installing is not here. An install acts on your project, so it happens in the console, signed in.

Root fields outside the model operations

A handful of root fields are not per-model operations. Each is still a typed operation, in a namespace, for the credential that can reach it.

The untyped record escape hatches

record, records, recordVersions, batchRecordOperations.

These name a model or a record by a string, so nothing they return is typed to your model: record and records return plain JSON. They are the records namespace, foir.ns.records.record() and the rest, for a credential holding records:read or records:write with no model named. A key restricted to particular models does not see them. Where the model is known, the typed per-model operations give you its real type.

Schema introspection

models, projectSchemas.

These describe the shape of your project. They are in the project namespace, as foir.ns.project.models() and foir.ns.project.projectSchemas(), for a credential holding schemas:read. GET /v1/manifest describes the same project as your key sees it.

Async operation callbacks

completeSendEmailExecution, failSendEmailExecution, reportSendEmailProgress, cancelSendEmailExecution — and their equivalents for any other supports_async operation.

Called by the extension executing an operation, not by a key holder. They require a scoped callback token plus an HMAC signature. They sit in the operations namespace for that caller only: no API key can hold the grant they need, so they never appear in a key’s manifest.

Subscriptions

Not an absence: @foir/sdk/data opens the same graphql-ws socket a hand-written client would, over the same credential, and prints the subscription document for you. See Subscriptions.

Using both

import { createFoirClient } from '@foir/sdk/data'; import { foirRequest } from '@foir/sdk/client'; // Typed reads and writes for application code const page = await foir.page.findUnique({ naturalKey: 'homepage' }); // Your own document where you want the control const bundle = await foirRequest(env, HomepageBundleQuery, { slug });

A common shape: the generated client for typed reads and writes (in your app, and in your operation endpoints with the dispatch token), a hand-written document for the render-critical query where payload size decides, and @foir/sdk/auth for sessions.

How these docs order the two

Where a page shows the same operation both ways, the GraphQL document comes first and the generated call second, everywhere.

That is not a ranking. It is because a document can express everything the API can do, and the generated call vocabulary is a projection of it, so GraphQL-first is a true ordering at every site.

Last updated on