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
fragment
Artifact size
1.8 KB
Operation text
—
What the browser sends
—

1 · You write

src/components/advanced/DeferDemo.tsx
fragment DeferDemoInsights_post on Post {
  insights {
    readMinutes
    wordCount
    sentiment
    related
  }
}

2 · Types you import

DeferDemoInsights_post.graphql.ts
export type DeferDemoInsights_post$data = {
  readonly insights: {
    readonly readMinutes: number;
    readonly related: ReadonlyArray<string>;
    readonly sentiment: string;
    readonly wordCount: number;
  };
  readonly " $fragmentType": "DeferDemoInsights_post";
};
export type DeferDemoInsights_post$key = {
  readonly " $data"?: DeferDemoInsights_post$data;
  readonly " $fragmentSpreads": FragmentRefs<"DeferDemoInsights_post">;
};

3 · 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<<6fa0c237bc46466a19c6f568500be33d>>
 * @lightSyntaxTransform
 */

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

import { ReaderFragment } from 'relay-runtime';
import { FragmentRefs } from "relay-runtime";
export type DeferDemoInsights_post$data = {
  readonly insights: {
    readonly readMinutes: number;
    readonly related: ReadonlyArray<string>;
    readonly sentiment: string;
    readonly wordCount: number;
  };
  readonly " $fragmentType": "DeferDemoInsights_post";
};
export type DeferDemoInsights_post$key = {
  readonly " $data"?: DeferDemoInsights_post$data;
  readonly " $fragmentSpreads": FragmentRefs<"DeferDemoInsights_post">;
};

const node: ReaderFragment = {
  "argumentDefinitions": [],
  "kind": "Fragment",
  "metadata": null,
  "name": "DeferDemoInsights_post",
  "selections": [
    {
      "alias": null,
      "args": null,
      "concreteType": "PostInsights",
      "kind": "LinkedField",
      "name": "insights",
      "plural": false,
      "selections": [
        {
          "alias": null,
          "args": null,
          "kind": "ScalarField",
          "name": "readMinutes",
          "storageKey": null
        },
        {
          "alias": null,
          "args": null,
          "kind": "ScalarField",
          "name": "wordCount",
          "storageKey": null
        },
        {
          "alias": null,
          "args": null,
          "kind": "ScalarField",
          "name": "sentiment",
          "storageKey": null
        },
        {
          "alias": null,
          "args": null,
          "kind": "ScalarField",
          "name": "related",
          "storageKey": null
        }
      ],
      "storageKey": null
    }
  ],
  "type": "Post",
  "abstractKey": null
};

(node as any).hash = "d5e49fae24636d0344e093327101a566";

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.