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
2.6 KB
Operation text
109 B
What the browser sends
45 B + vars

1 · You write

src/components/store/StoreDemo.tsx
query StoreDemoProfileQuery {
  user(username: "alan") {
    bio
    location
    followerCount
  }
}

2 · Types you import

StoreDemoProfileQuery.graphql.ts
export type StoreDemoProfileQuery$variables = Record<PropertyKey, never>;
export type StoreDemoProfileQuery$data = {
  readonly user: {
    readonly bio: string | null | undefined;
    readonly followerCount: number;
    readonly location: string | null | undefined;
  } | null | undefined;
};
export type StoreDemoProfileQuery = {
  response: StoreDemoProfileQuery$data;
  variables: StoreDemoProfileQuery$variables;
};

3 · Operation text, persisted at build time

md5 8e383c9fc4ba8f20057a91764a057c0b

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 StoreDemoProfileQuery {
  user(username: "alan") {
    bio
    location
    followerCount
    id
  }
}

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<<94dadd285d99d72a2295aaa748f99cb4>>
 * @relayHash 8e383c9fc4ba8f20057a91764a057c0b
 * @lightSyntaxTransform
 */

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

// @relayRequestID 8e383c9fc4ba8f20057a91764a057c0b

import { ConcreteRequest } from 'relay-runtime';
export type StoreDemoProfileQuery$variables = Record<PropertyKey, never>;
export type StoreDemoProfileQuery$data = {
  readonly user: {
    readonly bio: string | null | undefined;
    readonly followerCount: number;
    readonly location: string | null | undefined;
  } | null | undefined;
};
export type StoreDemoProfileQuery = {
  response: StoreDemoProfileQuery$data;
  variables: StoreDemoProfileQuery$variables;
};

const node: ConcreteRequest = (function(){
var v0 = [
  {
    "kind": "Literal",
    "name": "username",
    "value": "alan"
  }
],
v1 = {
  "alias": null,
  "args": null,
  "kind": "ScalarField",
  "name": "bio",
  "storageKey": null
},
v2 = {
  "alias": null,
  "args": null,
  "kind": "ScalarField",
  "name": "location",
  "storageKey": null
},
v3 = {
  "alias": null,
  "args": null,
  "kind": "ScalarField",
  "name": "followerCount",
  "storageKey": null
};
return {
  "fragment": {
    "argumentDefinitions": [],
    "kind": "Fragment",
    "metadata": null,
    "name": "StoreDemoProfileQuery",
    "selections": [
      {
        "alias": null,
        "args": (v0/*:: as any*/),
        "concreteType": "User",
        "kind": "LinkedField",
        "name": "user",
        "plural": false,
        "selections": [
          (v1/*:: as any*/),
          (v2/*:: as any*/),
          (v3/*:: as any*/)
        ],
        "storageKey": "user(username:\"alan\")"
      }
    ],
    "type": "Query",
    "abstractKey": null
  },
  "kind": "Request",
  "operation": {
    "argumentDefinitions": [],
    "kind": "Operation",
    "name": "StoreDemoProfileQuery",
    "selections": [
      {
        "alias": null,
        "args": (v0/*:: as any*/),
        "concreteType": "User",
        "kind": "LinkedField",
        "name": "user",
        "plural": false,
        "selections": [
          (v1/*:: as any*/),
          (v2/*:: as any*/),
          (v3/*:: as any*/),
          {
            "alias": null,
            "args": null,
            "kind": "ScalarField",
            "name": "id",
            "storageKey": null
          }
        ],
        "storageKey": "user(username:\"alan\")"
      }
    ]
  },
  "params": {
    "id": "8e383c9fc4ba8f20057a91764a057c0b",
    "metadata": {},
    "name": "StoreDemoProfileQuery",
    "operationKind": "query",
    "text": null
  }
};
})();

(node as any).hash = "0fface36574843d6d9ef70492a9d919c";

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.