1 | # relay-compiler-plus
|
2 |
|
3 | [![npm version](https://img.shields.io/npm/v/relay-compiler-plus.svg?style=flat-square)](https://www.npmjs.com/package/relay-compiler-plus) [![npm downloads](https://img.shields.io/npm/dm/relay-compiler-plus.svg?style=flat-square)](https://www.npmjs.com/package/relay-compiler-plus) [![npm](https://img.shields.io/npm/dt/relay-compiler-plus.svg?style=flat-square)](https://www.npmjs.com/package/relay-compiler-plus) [![npm](https://img.shields.io/npm/l/relay-compiler-plus.svg?style=flat-square)](https://www.npmjs.com/package/relay-compiler-plus)
|
4 |
|
5 | > **Custom relay compiler which supports persisted queries** :bowtie:
|
6 |
|
7 | ## Installation
|
8 |
|
9 | npm i -g relay-compiler-plus
|
10 |
|
11 | ## Usage
|
12 |
|
13 | 1. At your project dir, run
|
14 |
|
15 | ```
|
16 | relay-compiler-plus --schema <SCHEMA_FILE_PATH> --src <SRC_DIR_PATH>
|
17 | ```
|
18 |
|
19 | where `<SCHEMA_FILE_PATH>` is the path to your schema.graphql or schema.json file.
|
20 |
|
21 | This should generate:
|
22 | * root query files (*.graphql.js) containing query ids and null query text.
|
23 | * A `queryMap.json` file under `<SRC_DIR_PATH>/queryMap.json`.
|
24 | This file can be consumed by the server to map the query ids to actual queries.
|
25 |
|
26 | 2. On the server, you need map the query ids to actual queries.
|
27 |
|
28 | First, add `relay-compiler-plus` to your project.
|
29 | ```
|
30 | yarn add relay-compiler-plus
|
31 | ```
|
32 |
|
33 | Then, use `matchQueryMiddleware` prior to `express-graphql` to match the queryIds. Note that `queryMap.json` is auto-generated by relay-compiler-plus at step 1.
|
34 |
|
35 | ```javascript
|
36 | import Express from 'express';
|
37 | import expressGraphl from 'express-graphql';
|
38 | import {matchQueryMiddleware} from 'relay-compiler-plus'; // do this
|
39 | import queryMapJson from '../queryMap.json'; // do this
|
40 |
|
41 | const app = Express();
|
42 |
|
43 | app.use('/graphql',
|
44 | matchQueryMiddleware(queryMapJson), // do this
|
45 | expressGraphl({
|
46 | schema: graphqlSchema,
|
47 | graphiql: true,
|
48 | }));
|
49 | ```
|
50 |
|
51 | 3. On the client, modify your relay network fetch implementation to pass a `queryId` parameter in the
|
52 | request body instead of a `query` parameter.
|
53 |
|
54 | ```javascript
|
55 | function fetchQuery(operation, variables,) {
|
56 | return fetch('/graphql', {
|
57 | method: 'POST',
|
58 | headers: {
|
59 | 'content-type': 'application/json'
|
60 | },
|
61 | body: JSON.stringify({
|
62 | queryId: operation.id, // do this
|
63 | variables,
|
64 | }),
|
65 | }).then(response => {
|
66 | return response.json();
|
67 | });
|
68 | }
|
69 | ```
|
70 |
|
71 | ## Example
|
72 | Check the [example](https://github.com/yusinto/relay-compiler-plus/tree/master/example)
|
73 | for a fully working demo.
|
74 |
|