A production Relay app sends no query text. relay-compiler gives every operation an id, the hash of its text, and writes the map of id to text; the app sends the id, and the server runs only the documents of that map. A query nobody compiled is never run, and a request is a few bytes. gratos-server's trustedDocuments() plugin is that server side.

Persisting with relay-compiler

persistConfig makes relay-compiler write the map (a local file, here hashed with SHA-256) instead of the text into each artifact:

relay.config.json
"persistConfig": {
  "file": "./src/generated/persisted-documents.json",
  "algorithm": "SHA256"
}

An artifact then has an id, a text of null, and a @relayRequestID comment. The fetch function sends the id as documentId, the member the GraphQL over HTTP persisted-documents proposal defines, and refuses to send an operation that was not persisted:

app/relay/fetchGraphQL.ts
export const requestBody = (params: RequestParameters, variables: Variables): string => {
  if (params.id === null) {
    throw new Error(
      `${params.name} has no persisted id: run \`pnpm codegen\` at the repo root, or \`pnpm relay\` in apps/docs.`,
    );
  }
  return JSON.stringify({ documentId: params.id, operationName: params.name, variables });
};

Running only the map

The server loads the map at startup and gives it to trustedDocuments(). In onReady, the plugin validates every entry against the schema the server is about to serve, so a map that no longer matches the schema stops the server before it serves anything:

src/graphql.ts
trustedDocuments({ documents: options.documents }),

A known documentId runs its document, over JSON, multipart/mixed and SSE alike. An unknown one gets a 400 with PERSISTED_DOCUMENT_NOT_FOUND, which is what a page built before a deploy sees; a query sent as text gets PERSISTED_DOCUMENT_REQUIRED. Neither runs an executor. A route that must take text queries, such as a playground, is a second createServer without the plugin (see Plugins).

Rebuilding the map from scratch

relay-compiler merges into an existing map, so an operation removed from the app would stay trusted. And it reuses the ids in its existing artifacts without persisting those operations again, so an empty map alone would drop the unchanged ones. So the app writes an empty map first, then runs relay-compiler with --repersist:

package.json
"relay": "node scripts/relay-schema.ts && node scripts/persisted-documents.ts && relay-compiler --repersist && relay-compiler relay.test.config.json",

relay-compiler --validate does not look at the map, so the read-only check (relay:check) runs a script of the app's own as well: the map's ids must be exactly the artifacts' ids, and each text must hash to its id.

scripts/persisted-documents.ts
/** The ids of the operations relay-compiler persisted, from their artifacts. */
const operationIds = readdirSync(artifacts)
  .filter((name) => name.endsWith(".graphql.ts"))
  .flatMap((name) => {
    const match = /^\/\/ @relayRequestID (\S+)$/m.exec(
      readFileSync(new URL(name, artifacts), "utf8"),
    );
    return match?.[1] === undefined ? [] : [match[1]];
  })
  .toSorted();

const ids = Object.keys(documents).toSorted();
const missing = operationIds.filter((id) => !ids.includes(id));
const extra = ids.filter((id) => !operationIds.includes(id));
if (missing.length > 0 || extra.length > 0) {
  fail(
    `stale (missing: ${missing.join(", ") || "none"}; not an operation: ${extra.join(", ") || "none"}).`,
  );
}

The site's tests serve every persisted operation by its id, check that no request carries query text, and compile a copy of the app with one operation removed: its old id is refused afterwards, and the others still resolve.