UNPKG

3.02 kBMarkdownView Raw
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```bash
11yarn add relay-compiler-plus
12```
13
14Make sure you have the latest version of [graphql-js](https://github.com/graphql/graphql-js):
15```bash
16yarn upgrade graphql --latest
17```
18
19## Usage
201. Add this npm command to your package.json:
21
22 ```
23 "scripts": {
24 "rcp": "relay-compiler-plus --schema <SCHEMA_FILE_PATH> --src <SRC_DIR_PATH> -f"
25 },
26 ```
27
28 where
29 * `<SCHEMA_FILE_PATH>` is the path to your schema.graphql or schema.json file.
30 * `<SRC_DIR_PATH>` is the path to your src directory
31 * `-f` will delete all `__generated__/*.graphql.js` files under `SRC_DIR_PATH`
32
33 then:
34 ```
35 npm run rcp
36 ```
37 this should generate:
38 * query files (*.graphql.js) containing query ids and null query text.
39 * A `queryMap.json` file under `<SRC_DIR_PATH>/queryMap.json`.
40 This file can be consumed by the server to map the query ids to actual queries.
41
422. On the server, use `matchQueryMiddleware` prior to `express-graphql` to match queryIds to actual queries. Note
43 that `queryMap.json` is auto-generated by relay-compiler-plus at step 1.
44
45 ```javascript
46 import Express from 'express';
47 import expressGraphl from 'express-graphql';
48 import {matchQueryMiddleware} from 'relay-compiler-plus'; // do this
49 import queryMapJson from '../queryMap.json'; // do this
50
51 const app = Express();
52
53 app.use('/graphql',
54 matchQueryMiddleware(queryMapJson), // do this
55 expressGraphl({
56 schema: graphqlSchema,
57 graphiql: true,
58 }));
59 ```
60
613. On the client, modify your relay network fetch implementation to pass a `queryId` parameter in the
62 request body instead of a `query` parameter. `operation.id` is generated by `relay-compiler-plus` in step 1.
63
64 ```javascript
65 function fetchQuery(operation, variables,) {
66 return fetch('/graphql', {
67 method: 'POST',
68 headers: {
69 'content-type': 'application/json'
70 },
71 body: JSON.stringify({
72 queryId: operation.id, // do this
73 variables,
74 }),
75 }).then(response => {
76 return response.json();
77 });
78 }
79 ```
80
81Run your app and that's it!
82
83## Example
84Check the [example](https://github.com/yusinto/relay-compiler-plus/tree/master/example)
85for a fully working demo.
86