Giter Club home page Giter Club logo

relay-compiler-plus's Introduction

relay-compiler-plus

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
  • direct compilation of graphql-js

Direct graphql-js support means you can generate your relay queries, schema.graphql and query map files all in a single step!

Installation

yarn add relay-compiler-plus

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

yarn upgrade graphql --latest  

Usage

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

    "scripts": {
        "rcp": "NODE_ENV=production 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 or schema.js (yes! rcp now supports direct compilation from graphql-js!).
    • <SRC_DIR_PATH> is the path to your src directory

    then:

    npm run rcp
    

    this should generate:

    • query files (*.graphql.js) containing query ids and null query text. Note that if you omit NODE_ENV=production, rcp will include both the query id and the query text in your query files. This can be useful for debugging in development.
    • 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.
    • If you specified a schema.js file, this will also generate a schema.graphql file under ../<SRC_DIR_PATH>/schema.graphql. The schema.graphql has to sit outside the src folder otherwise the relay-compiler will complain.

    If your graphql-js file is complex and you need to override the default webpack config you can do so like this:

    "scripts": {
        "rcp": "NODE_ENV=production relay-compiler-plus --webpackConfig <WEBPACK_CONFIG_PATH> --src <SRC_DIR_PATH>"
    },
    

    where

    • <WEBPACK_CONFIG_PATH> is the path to your custom webpack config to transpile your graphql-js schema. In your custom webpack config, you need to set output.libraryTarget: 'commonjs2'. See the example config for a working copy.
  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. 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
          variables,
        }),
      }).then(response => {
        return response.json();
      });
    }

Run your app and that's it!

Example

Check the example for a fully working demo.

relay-compiler-plus's People

Contributors

vabruzzo avatar yusinto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

relay-compiler-plus's Issues

Option to keep operation.text in the generated queries?

Is there a way of keeping operation.text in the generated queries?

I'm working around it for now, but I can see the null text being a problem, for example, if you use a mock network layer for testing, or want to turn off the query-by-ID in some environments without re-compiling all the queries using the standard relay-compiler.

(For example, we're thinking of only enforcing query-by-ID on live)

thanks, btw! This lib looks like a good start for what I want to do - hopefully some PRs when I figure things out in the near future.

Allow custom RelayJSModuleParser.js

Hi there!

I love the idea of this package and would love to use it in my app. I currently I have a forked version of relay-compiler that can go through .tsx files and returns a .js version of the file so that relay-compiler can read the file for graphql tags.

Two commits that's in the fork:

getFileFilter function: ONEHOPEWINE/relay@69c68d5
parseFile function:
ONEHOPEWINE/relay@a12b931#diff-6843e3b7dfd6e16bf7a6bfd9fcc17344

What would be the best way to support .tsx file extension in this package?

Thanks!

Remove console.log()'s

Hey, first of all thanks for making and open sourcing this library! :)

I was just skimming through the source code when I noticed a console.log here. Unless I'm missing something and this gets stripped in production, using console.log in production is considered harmful™ which is ironical, seeing that this package seeks to improve performance ;)

Thanks for looking into this!

Error: Cannot find module 'relay-compiler/lib/DotGraphQLParser'

Hi,

I love the concept here! I ran into this while trying out your examples and I reproduced it in my own project. Admittedly I didn't invest much effort into figuring this out, but I figured I'd drop a note here. Essentially, just npm i -S relay-compiler-plus and then trying to run it fails with this error on OSX 10.13. I can provide more info and steps to reproduce if necessary.

Support passing in extensions

relay-compiler supports passing in --extensions, so you can run it if you have a convention of naming files .jsx for instance.

In my case, I didn't have much of a convention yet, so I worked around it by just renaming my files, but I may come back to looking at the code here and improving this later.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.