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
query
Artifact size
3.9 KB
Operation text
233 B
What the browser sends
45 B + vars

1 · You write

src/components/advanced/RequiredCatchDemo.tsx
query RequiredCatchDemoQuery($postId: ID!) {
  users {
    id
    ...RequiredCatchDemo_user
  }
  post(id: $postId) {
    title
    sponsor @catch
  }
}

2 · Types you import

RequiredCatchDemoQuery.graphql.ts
export type RequiredCatchDemoQuery$variables = {
  postId: string;
};
export type RequiredCatchDemoQuery$data = {
  readonly post: {
    readonly sponsor: Result<string | null | undefined, unknown>;
    readonly title: string;
  } | null | undefined;
  readonly users: ReadonlyArray<{
    readonly id: string;
    readonly " $fragmentSpreads": FragmentRefs<"RequiredCatchDemo_user">;
  }>;
};
export type RequiredCatchDemoQuery = {
  response: RequiredCatchDemoQuery$data;
  variables: RequiredCatchDemoQuery$variables;
};

3 · Operation text, persisted at build time

md5 49f4b926e39652d8e0f1382b0119321a

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

query RequiredCatchDemoQuery(
  $postId: ID!
) {
  users {
    id
    ...RequiredCatchDemo_user
  }
  post(id: $postId) {
    title
    sponsor
    id
  }
}

fragment RequiredCatchDemo_user on User {
  name
  avatarColor
  website
}

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<<54fc0b80686a4ca8bf990c57742532d6>>
 * @relayHash 49f4b926e39652d8e0f1382b0119321a
 * @lightSyntaxTransform
 */

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

// @relayRequestID 49f4b926e39652d8e0f1382b0119321a

import { ConcreteRequest } from 'relay-runtime';
import { FragmentRefs, Result } from "relay-runtime";
export type RequiredCatchDemoQuery$variables = {
  postId: string;
};
export type RequiredCatchDemoQuery$data = {
  readonly post: {
    readonly sponsor: Result<string | null | undefined, unknown>;
    readonly title: string;
  } | null | undefined;
  readonly users: ReadonlyArray<{
    readonly id: string;
    readonly " $fragmentSpreads": FragmentRefs<"RequiredCatchDemo_user">;
  }>;
};
export type RequiredCatchDemoQuery = {
  response: RequiredCatchDemoQuery$data;
  variables: RequiredCatchDemoQuery$variables;
};

const node: ConcreteRequest = (function(){
var v0 = [
  {
    "defaultValue": null,
    "kind": "LocalArgument",
    "name": "postId"
  }
],
v1 = {
  "alias": null,
  "args": null,
  "kind": "ScalarField",
  "name": "id",
  "storageKey": null
},
v2 = [
  {
    "kind": "Variable",
    "name": "id",
    "variableName": "postId"
  }
],
v3 = {
  "alias": null,
  "args": null,
  "kind": "ScalarField",
  "name": "title",
  "storageKey": null
},
v4 = {
  "alias": null,
  "args": null,
  "kind": "ScalarField",
  "name": "sponsor",
  "storageKey": null
};
return {
  "fragment": {
    "argumentDefinitions": (v0/*:: as any*/),
    "kind": "Fragment",
    "metadata": null,
    "name": "RequiredCatchDemoQuery",
    "selections": [
      {
        "alias": null,
        "args": null,
        "concreteType": "User",
        "kind": "LinkedField",
        "name": "users",
        "plural": true,
        "selections": [
          (v1/*:: as any*/),
          {
            "args": null,
            "kind": "FragmentSpread",
            "name": "RequiredCatchDemo_user"
          }
        ],
        "storageKey": null
      },
      {
        "alias": null,
        "args": (v2/*:: as any*/),
        "concreteType": "Post",
        "kind": "LinkedField",
        "name": "post",
        "plural": false,
        "selections": [
          (v3/*:: as any*/),
          {
            "kind": "CatchField",
            "field": (v4/*:: as any*/),
            "to": "RESULT"
          }
        ],
        "storageKey": null
      }
    ],
    "type": "Query",
    "abstractKey": null
  },
  "kind": "Request",
  "operation": {
    "argumentDefinitions": (v0/*:: as any*/),
    "kind": "Operation",
    "name": "RequiredCatchDemoQuery",
    "selections": [
      {
        "alias": null,
        "args": null,
        "concreteType": "User",
        "kind": "LinkedField",
        "name": "users",
        "plural": true,
        "selections": [
          (v1/*:: as any*/),
          {
            "alias": null,
            "args": null,
            "kind": "ScalarField",
            "name": "name",
            "storageKey": null
          },
          {
            "alias": null,
            "args": null,
            "kind": "ScalarField",
            "name": "avatarColor",
            "storageKey": null
          },
          {
            "alias": null,
            "args": null,
            "kind": "ScalarField",
            "name": "website",
            "storageKey": null
          }
        ],
        "storageKey": null
      },
      {
        "alias": null,
        "args": (v2/*:: as any*/),
        "concreteType": "Post",
        "kind": "LinkedField",
        "name": "post",
        "plural": false,
        "selections": [
          (v3/*:: as any*/),
          (v4/*:: as any*/),
          (v1/*:: as any*/)
        ],
        "storageKey": null
      }
    ]
  },
  "params": {
    "id": "49f4b926e39652d8e0f1382b0119321a",
    "metadata": {},
    "name": "RequiredCatchDemoQuery",
    "operationKind": "query",
    "text": null
  }
};
})();

(node as any).hash = "402f3157be9ac974f99a2924c428a8f9";

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.