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.3 KB
Operation text
384 B
What the browser sends
45 B + vars
1 · You write
src/components/pagination/PaginationDemo.tsxquery PaginationDemoQuery {
...PaginationList_query
}2 · Types you import
PaginationDemoQuery.graphql.tsexport type PaginationDemoQuery$variables = Record<PropertyKey, never>;
export type PaginationDemoQuery$data = {
readonly " $fragmentSpreads": FragmentRefs<"PaginationList_query">;
};
export type PaginationDemoQuery = {
response: PaginationDemoQuery$data;
variables: PaginationDemoQuery$variables;
};3 · Operation text, persisted at build time
md5 399bee76ad78574297166e3ef0b8cbd2Fragments from every component are inlined into one document. It lives in persisted_queries.json on the server; the client only knows the hash.
query PaginationDemoQuery {
...PaginationList_query
}
fragment PaginationList_query on Query {
feed(first: 3) {
totalCount
edges {
cursor
node {
id
title
likeCount
author {
name
avatarColor
id
}
__typename
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
4 · 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<<3891d01700be8d89950b5c2e770c7679>>
* @relayHash 399bee76ad78574297166e3ef0b8cbd2
* @lightSyntaxTransform
*/
/* tslint:disable */
/* eslint-disable */
// @ts-nocheck
// @relayRequestID 399bee76ad78574297166e3ef0b8cbd2
import { ConcreteRequest } from 'relay-runtime';
import { FragmentRefs } from "relay-runtime";
export type PaginationDemoQuery$variables = Record<PropertyKey, never>;
export type PaginationDemoQuery$data = {
readonly " $fragmentSpreads": FragmentRefs<"PaginationList_query">;
};
export type PaginationDemoQuery = {
response: PaginationDemoQuery$data;
variables: PaginationDemoQuery$variables;
};
const node: ConcreteRequest = (function(){
var v0 = [
{
"kind": "Literal",
"name": "first",
"value": 3
}
],
v1 = {
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "id",
"storageKey": null
};
return {
"fragment": {
"argumentDefinitions": [],
"kind": "Fragment",
"metadata": null,
"name": "PaginationDemoQuery",
"selections": [
{
"args": null,
"kind": "FragmentSpread",
"name": "PaginationList_query"
}
],
"type": "Query",
"abstractKey": null
},
"kind": "Request",
"operation": {
"argumentDefinitions": [],
"kind": "Operation",
"name": "PaginationDemoQuery",
"selections": [
{
"alias": null,
"args": (v0/*:: as any*/),
"concreteType": "PostConnection",
"kind": "LinkedField",
"name": "feed",
"plural": false,
"selections": [
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "totalCount",
"storageKey": null
},
{
"alias": null,
"args": null,
"concreteType": "PostEdge",
"kind": "LinkedField",
"name": "edges",
"plural": true,
"selections": [
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "cursor",
"storageKey": null
},
{
"alias": null,
"args": null,
"concreteType": "Post",
"kind": "LinkedField",
"name": "node",
"plural": false,
"selections": [
(v1/*:: as any*/),
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "title",
"storageKey": null
},
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "likeCount",
"storageKey": null
},
{
"alias": null,
"args": null,
"concreteType": "User",
"kind": "LinkedField",
"name": "author",
"plural": false,
"selections": [
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "name",
"storageKey": null
},
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "avatarColor",
"storageKey": null
},
(v1/*:: as any*/)
],
"storageKey": null
},
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "__typename",
"storageKey": null
}
],
"storageKey": null
}
],
"storageKey": null
},
{
"alias": null,
"args": null,
"concreteType": "PageInfo",
"kind": "LinkedField",
"name": "pageInfo",
"plural": false,
"selections": [
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "endCursor",
"storageKey": null
},
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "hasNextPage",
"storageKey": null
}
],
"storageKey": null
}
],
"storageKey": "feed(first:3)"
},
{
"alias": null,
"args": (v0/*:: as any*/),
"filters": null,
"handle": "connection",
"key": "PaginationList_feed",
"kind": "LinkedHandle",
"name": "feed"
}
]
},
"params": {
"id": "399bee76ad78574297166e3ef0b8cbd2",
"metadata": {},
"name": "PaginationDemoQuery",
"operationKind": "query",
"text": null
}
};
})();
(node as any).hash = "94a95e248fb268f96a44cf7efedbc818";
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.