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


1.4.3 • Public • Published


npm version npm downloads npm npm

Custom relay compiler which supports persisted queries :bowtie:

Relay modern is awesome. However it's missing a few things, one of which is persisted queries. This package is a custom relay compiler which supports persisted queries.


yarn add relay-compiler-plus

Make sure you have the latest version of graphql-js:

yarn upgrade graphql --latest  


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

    "scripts": {
        "rcp": "relay-compiler-plus --schema <SCHEMA_FILE_PATH> --src <SRC_DIR_PATH> -f"


    • <SCHEMA_FILE_PATH> is the path to your schema.graphql or schema.json file.
    • <SRC_DIR_PATH> is the path to your src directory
    • -f will delete all __generated__/*.graphql.js files under SRC_DIR_PATH


    npm run rcp

    this should generate:

    • 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();
      matchQueryMiddleware(queryMapJson), // do this
        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. Note that operation.id is generated by relay-compiler-plus in step 1.

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

Run your app and that's it!


Check the example for a fully working demo.