06

The compiler does the work before your app runs

relay-compiler reads every graphql tag in the project, validates it against schema.graphql, and writes an artifact per fragment and operation: a precomputed runtime AST, TypeScript types, and (here) a persisted query id. Pick any artifact this app generated.
Kind
mutation
Artifact size
2.5 KB
Operation text
133 B
What the browser sends
45 B + vars

1 · You write

src/components/consistency/RenameForm.tsx
mutation RenameFormMutation($input: RenameUserInput!) {
  renameUser(input: $input) {
    user {
      id
      name
    }
  }
}

2 · Types you import

RenameFormMutation.graphql.ts
export type RenameUserInput = {
  clientMutationId?: string | null | undefined;
  name: string;
  userId: string;
};
export type RenameFormMutation$variables = {
  input: RenameUserInput;
};
export type RenameFormMutation$data = {
  readonly renameUser: {
    readonly user: {
      readonly id: string;
      readonly name: string;
    } | null | undefined;
  } | null | undefined;
};
export type RenameFormMutation = {
  response: RenameFormMutation$data;
  variables: RenameFormMutation$variables;
};

3 · Operation text, persisted at build time

md5 432e7de7536e0cb18edbbfcd7828d900

Fragments from every component are inlined into one document. It lives in persisted_queries.json on the server; the client only knows the hash.

mutation RenameFormMutation(
  $input: RenameUserInput!
) {
  renameUser(input: $input) {
    user {
      id
      name
    }
  }
}

4 · Runtime artifact

imported in place of the graphql`` tag

A preprocessed AST, so there's no GraphQL parsing in the browser. The reader part tells useFragment what to read; the normalization part tells the store how to write.

/**
 * @generated SignedSource<<5bea46395addfd1e127c37523ee633d6>>
 * @relayHash 432e7de7536e0cb18edbbfcd7828d900
 * @lightSyntaxTransform
 */

/* tslint:disable */
/* eslint-disable */
// @ts-nocheck

// @relayRequestID 432e7de7536e0cb18edbbfcd7828d900

import { ConcreteRequest } from 'relay-runtime';
export type RenameUserInput = {
  clientMutationId?: string | null | undefined;
  name: string;
  userId: string;
};
export type RenameFormMutation$variables = {
  input: RenameUserInput;
};
export type RenameFormMutation$data = {
  readonly renameUser: {
    readonly user: {
      readonly id: string;
      readonly name: string;
    } | null | undefined;
  } | null | undefined;
};
export type RenameFormMutation = {
  response: RenameFormMutation$data;
  variables: RenameFormMutation$variables;
};

const node: ConcreteRequest = (function(){
var v0 = [
  {
    "defaultValue": null,
    "kind": "LocalArgument",
    "name": "input"
  }
],
v1 = [
  {
    "alias": null,
    "args": [
      {
        "kind": "Variable",
        "name": "input",
        "variableName": "input"
      }
    ],
    "concreteType": "RenameUserPayload",
    "kind": "LinkedField",
    "name": "renameUser",
    "plural": false,
    "selections": [
      {
        "alias": null,
        "args": null,
        "concreteType": "User",
        "kind": "LinkedField",
        "name": "user",
        "plural": false,
        "selections": [
          {
            "alias": null,
            "args": null,
            "kind": "ScalarField",
            "name": "id",
            "storageKey": null
          },
          {
            "alias": null,
            "args": null,
            "kind": "ScalarField",
            "name": "name",
            "storageKey": null
          }
        ],
        "storageKey": null
      }
    ],
    "storageKey": null
  }
];
return {
  "fragment": {
    "argumentDefinitions": (v0/*:: as any*/),
    "kind": "Fragment",
    "metadata": null,
    "name": "RenameFormMutation",
    "selections": (v1/*:: as any*/),
    "type": "Mutation",
    "abstractKey": null
  },
  "kind": "Request",
  "operation": {
    "argumentDefinitions": (v0/*:: as any*/),
    "kind": "Operation",
    "name": "RenameFormMutation",
    "selections": (v1/*:: as any*/)
  },
  "params": {
    "id": "432e7de7536e0cb18edbbfcd7828d900",
    "metadata": {},
    "name": "RenameFormMutation",
    "operationKind": "mutation",
    "text": null
  }
};
})();

(node as any).hash = "9ee1afddd45276a28ef13aab612f64f2";

export default node;
  • why it mattersA typo in a field name fails the build, not the user's session. Every fragment is checked against the schema on every save.
  • why it mattersTypes come from the fragment rather than the whole schema, so a component's data type has exactly the fields it asked for.
  • why it mattersPersisted queries ship for free: smaller requests, cacheable GETs, and a server that can refuse any query it didn't compile.