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.2 KB
Operation text
—
What the browser sends
—
1 · You write
src/components/colocation/LikeBar.tsxfragment LikeBar_post on Post {
likeCount
viewerHasLiked
tags
}2 · Types you import
LikeBar_post.graphql.tsexport type LikeBar_post$data = {
readonly likeCount: number;
readonly tags: ReadonlyArray<string>;
readonly viewerHasLiked: boolean;
readonly " $fragmentType": "LikeBar_post";
};
export type LikeBar_post$key = {
readonly " $data"?: LikeBar_post$data;
readonly " $fragmentSpreads": FragmentRefs<"LikeBar_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<<5ea21dc13fbfdf2b9ea441b9d5c00874>>
* @lightSyntaxTransform
*/
/* tslint:disable */
/* eslint-disable */
// @ts-nocheck
import { ReaderFragment } from 'relay-runtime';
import { FragmentRefs } from "relay-runtime";
export type LikeBar_post$data = {
readonly likeCount: number;
readonly tags: ReadonlyArray<string>;
readonly viewerHasLiked: boolean;
readonly " $fragmentType": "LikeBar_post";
};
export type LikeBar_post$key = {
readonly " $data"?: LikeBar_post$data;
readonly " $fragmentSpreads": FragmentRefs<"LikeBar_post">;
};
const node: ReaderFragment = {
"argumentDefinitions": [],
"kind": "Fragment",
"metadata": null,
"name": "LikeBar_post",
"selections": [
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "likeCount",
"storageKey": null
},
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "viewerHasLiked",
"storageKey": null
},
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "tags",
"storageKey": null
}
],
"type": "Post",
"abstractKey": null
};
(node as any).hash = "64fb33cf161d8b9a92e04160ab202042";
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.