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.6 KB
Operation text
—
What the browser sends
—
1 · You write
src/components/consistency/HeroPost.tsxfragment HeroPost_post on Post {
excerpt
...PostTitle_post
...LikeButton_post
author {
...AuthorName_user
}
}2 · Types you import
HeroPost_post.graphql.tsexport type HeroPost_post$data = {
readonly author: {
readonly " $fragmentSpreads": FragmentRefs<"AuthorName_user">;
};
readonly excerpt: string;
readonly " $fragmentSpreads": FragmentRefs<"LikeButton_post" | "PostTitle_post">;
readonly " $fragmentType": "HeroPost_post";
};
export type HeroPost_post$key = {
readonly " $data"?: HeroPost_post$data;
readonly " $fragmentSpreads": FragmentRefs<"HeroPost_post">;
};3 · Runtime artifact
imported in place of the graphql`` tagA 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<<38e04fc3e536a15585456800eae14cd3>>
* @lightSyntaxTransform
*/
/* tslint:disable */
/* eslint-disable */
// @ts-nocheck
import { ReaderFragment } from 'relay-runtime';
import { FragmentRefs } from "relay-runtime";
export type HeroPost_post$data = {
readonly author: {
readonly " $fragmentSpreads": FragmentRefs<"AuthorName_user">;
};
readonly excerpt: string;
readonly " $fragmentSpreads": FragmentRefs<"LikeButton_post" | "PostTitle_post">;
readonly " $fragmentType": "HeroPost_post";
};
export type HeroPost_post$key = {
readonly " $data"?: HeroPost_post$data;
readonly " $fragmentSpreads": FragmentRefs<"HeroPost_post">;
};
const node: ReaderFragment = {
"argumentDefinitions": [],
"kind": "Fragment",
"metadata": null,
"name": "HeroPost_post",
"selections": [
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "excerpt",
"storageKey": null
},
{
"args": null,
"kind": "FragmentSpread",
"name": "PostTitle_post"
},
{
"args": null,
"kind": "FragmentSpread",
"name": "LikeButton_post"
},
{
"alias": null,
"args": null,
"concreteType": "User",
"kind": "LinkedField",
"name": "author",
"plural": false,
"selections": [
{
"args": null,
"kind": "FragmentSpread",
"name": "AuthorName_user"
}
],
"storageKey": null
}
],
"type": "Post",
"abstractKey": null
};
(node as any).hash = "24d589b954034750924127ca04b0fa85";
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
datatype 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.