Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

relay-compiler-plus

1.2.15 • Public • Published

relay-compiler-plus

npm version npm downloads npm npm

Custom relay compiler which supports persisted queries :bowtie:

Note: This is still unstable! A stable release is coming soon.

Installation

yarn add relay-compiler-plus

Usage

  1. Add this npm command to your package.json.

    "scripts": {
        "relay-compiler-plus": "relay-compiler-plus --schema <SCHEMA_FILE_PATH> --src <SRC_DIR_PATH>",
    },
    

    where <SCHEMA_FILE_PATH> is the path to your schema.graphql or schema.json file.

    This should generate:

    • root query files (*.graphql.js) containing query ids and null query text.
    • A queryMap.json file under <SRC_DIR_PATH>/queryMap.json. This file can be consumed by the server to map the query ids to actual queries.
  2. On the server, use matchQueryMiddleware prior to express-graphql to match queryIds to actual queries. Note that queryMap.json is auto-generated by relay-compiler-plus at step 1.

    import Express from 'express';
    import expressGraphl from 'express-graphql';
    import {matchQueryMiddleware} from 'relay-compiler-plus'; // do this
    import queryMapJson from '../queryMap.json'; // do this
     
    const app = Express();
     
    app.use('/graphql',
      matchQueryMiddleware(queryMapJson), // do this
      expressGraphl({
        schema: graphqlSchema,
        graphiql: true,
      }));
  3. On the client, modify your relay network fetch implementation to pass a queryId parameter in the request body instead of a query parameter.

    function fetchQuery(operation, variables,) {
      return fetch('/graphql', {
        method: 'POST',
        headers: {
          'content-type': 'application/json'
        },
        body: JSON.stringify({
          queryId: operation.id, // do this
          variables,
        }),
      }).then(response => {
        return response.json();
      });
    }

Example

Check the example for a fully working demo.

Install

npm i [email protected]

Version

1.2.15

License

MIT

Last publish

Collaborators

  • avatar