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

1 · You write

src/components/advanced/ServerPreloaded.tsx
query ServerPreloadedQuery {
  feed(first: 4) {
    edges {
      node {
        id
        title
        author {
          name
          avatarColor
        }
        ...LikeButton_post
      }
    }
  }
}

2 · Types you import

ServerPreloadedQuery.graphql.ts
export type ServerPreloadedQuery$variables = Record<PropertyKey, never>;
export type ServerPreloadedQuery$data = {
  readonly feed: {
    readonly edges: ReadonlyArray<{
      readonly node: {
        readonly author: {
          readonly avatarColor: string;
          readonly name: string;
        };
        readonly id: string;
        readonly title: string;
        readonly " $fragmentSpreads": FragmentRefs<"LikeButton_post">;
      } | null | undefined;
    } | null | undefined> | null | undefined;
  };
};
export type ServerPreloadedQuery = {
  response: ServerPreloadedQuery$data;
  variables: ServerPreloadedQuery$variables;
};

3 · Operation text, persisted at build time

md5 4616046d837052e62db4c74e03d01558

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 ServerPreloadedQuery {
  feed(first: 4) {
    edges {
      node {
        id
        title
        author {
          name
          avatarColor
          id
        }
        ...LikeButton_post
      }
    }
  }
}

fragment LikeButton_post on Post {
  id
  likeCount
  viewerHasLiked
}

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<<ff5ebf3da348b7d39f9965f4e461732d>>
 * @relayHash 4616046d837052e62db4c74e03d01558
 * @lightSyntaxTransform
 */

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

// @relayRequestID 4616046d837052e62db4c74e03d01558

import { ConcreteRequest } from 'relay-runtime';
import { FragmentRefs } from "relay-runtime";
export type ServerPreloadedQuery$variables = Record<PropertyKey, never>;
export type ServerPreloadedQuery$data = {
  readonly feed: {
    readonly edges: ReadonlyArray<{
      readonly node: {
        readonly author: {
          readonly avatarColor: string;
          readonly name: string;
        };
        readonly id: string;
        readonly title: string;
        readonly " $fragmentSpreads": FragmentRefs<"LikeButton_post">;
      } | null | undefined;
    } | null | undefined> | null | undefined;
  };
};
export type ServerPreloadedQuery = {
  response: ServerPreloadedQuery$data;
  variables: ServerPreloadedQuery$variables;
};

const node: ConcreteRequest = (function(){
var v0 = [
  {
    "kind": "Literal",
    "name": "first",
    "value": 4
  }
],
v1 = {
  "alias": null,
  "args": null,
  "kind": "ScalarField",
  "name": "id",
  "storageKey": null
},
v2 = {
  "alias": null,
  "args": null,
  "kind": "ScalarField",
  "name": "title",
  "storageKey": null
},
v3 = {
  "alias": null,
  "args": null,
  "kind": "ScalarField",
  "name": "name",
  "storageKey": null
},
v4 = {
  "alias": null,
  "args": null,
  "kind": "ScalarField",
  "name": "avatarColor",
  "storageKey": null
};
return {
  "fragment": {
    "argumentDefinitions": [],
    "kind": "Fragment",
    "metadata": null,
    "name": "ServerPreloadedQuery",
    "selections": [
      {
        "alias": null,
        "args": (v0/*:: as any*/),
        "concreteType": "PostConnection",
        "kind": "LinkedField",
        "name": "feed",
        "plural": false,
        "selections": [
          {
            "alias": null,
            "args": null,
            "concreteType": "PostEdge",
            "kind": "LinkedField",
            "name": "edges",
            "plural": true,
            "selections": [
              {
                "alias": null,
                "args": null,
                "concreteType": "Post",
                "kind": "LinkedField",
                "name": "node",
                "plural": false,
                "selections": [
                  (v1/*:: as any*/),
                  (v2/*:: as any*/),
                  {
                    "alias": null,
                    "args": null,
                    "concreteType": "User",
                    "kind": "LinkedField",
                    "name": "author",
                    "plural": false,
                    "selections": [
                      (v3/*:: as any*/),
                      (v4/*:: as any*/)
                    ],
                    "storageKey": null
                  },
                  {
                    "args": null,
                    "kind": "FragmentSpread",
                    "name": "LikeButton_post"
                  }
                ],
                "storageKey": null
              }
            ],
            "storageKey": null
          }
        ],
        "storageKey": "feed(first:4)"
      }
    ],
    "type": "Query",
    "abstractKey": null
  },
  "kind": "Request",
  "operation": {
    "argumentDefinitions": [],
    "kind": "Operation",
    "name": "ServerPreloadedQuery",
    "selections": [
      {
        "alias": null,
        "args": (v0/*:: as any*/),
        "concreteType": "PostConnection",
        "kind": "LinkedField",
        "name": "feed",
        "plural": false,
        "selections": [
          {
            "alias": null,
            "args": null,
            "concreteType": "PostEdge",
            "kind": "LinkedField",
            "name": "edges",
            "plural": true,
            "selections": [
              {
                "alias": null,
                "args": null,
                "concreteType": "Post",
                "kind": "LinkedField",
                "name": "node",
                "plural": false,
                "selections": [
                  (v1/*:: as any*/),
                  (v2/*:: as any*/),
                  {
                    "alias": null,
                    "args": null,
                    "concreteType": "User",
                    "kind": "LinkedField",
                    "name": "author",
                    "plural": false,
                    "selections": [
                      (v3/*:: as any*/),
                      (v4/*:: as any*/),
                      (v1/*:: as any*/)
                    ],
                    "storageKey": null
                  },
                  {
                    "alias": null,
                    "args": null,
                    "kind": "ScalarField",
                    "name": "likeCount",
                    "storageKey": null
                  },
                  {
                    "alias": null,
                    "args": null,
                    "kind": "ScalarField",
                    "name": "viewerHasLiked",
                    "storageKey": null
                  }
                ],
                "storageKey": null
              }
            ],
            "storageKey": null
          }
        ],
        "storageKey": "feed(first:4)"
      }
    ]
  },
  "params": {
    "id": "4616046d837052e62db4c74e03d01558",
    "metadata": {},
    "name": "ServerPreloadedQuery",
    "operationKind": "query",
    "text": null
  }
};
})();

(node as any).hash = "7807d8785946373d047352e03419c443";

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.