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