Graphql, persistgraphql, webpack, wepback-plugin technologies

PersistGraphQL Webpack Plugin with Hot Code Reload released

The SysGears Team is pleased to announce the availability of PersistGraphQL Webpack Plugin with Hot Code Reload for backend & frontend.

PersistGraphQL Webpack Plugin is a tool to gather GraphQL queries from source code of GraphQL projects and inject them into the build bundles. Unique IDs assigned to GraphQL queries and since both back end and front end are aware of the mapping of these IDs to GraphQL queries, passing of full queries is not needed and replaced by passing query IDs only. Hence usage of persistent GraphQL queries is good to reduce bandwith and for security reasons, since all supported queries are known during build time.

Installation

1
npm install --save-dev persistgraphql-webpack-plugin

Usage

When Webpack is used for front-end only

Sample Webpack config:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var PersistGraphQLPlugin = require('persistgraphql-webpack-plugin');

module.exports = {
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        // ...
        use: [
          'babel-loader', 
          // Should come AFTER babel-loader
          'persistgraphql-webpack-plugin/js-loader'
        ]
      },
      {
        test: /\.(graphql|gql)$/,
        use: [
          'graphql-tag/loader', 
          // Should come AFTER graphql-tag/loader
          'persistgraphql-webpack-plugin/graphql-loader'
        ]
      },
    ]
  }
  
  plugins: [
    new PersistGraphQLPlugin({filename: 'persisted_queries.json'})
  ]
};

In the source code of front-end persisted GraphQL queries will be injected as a virtual module persisted_queries.json. This module will be updated if queries added or changed. Also asset with name persisted_queries.json will be generated during compilation and written to output directory.

1
2
var queryMap = require('persisted_queries.json');
console.log(queryMap);

When Webpack is used both for back-end and front-end

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var PersistGraphQLPlugin = require('persistgraphql-webpack-plugin');

const frontendPersistPlugin = new PersistGraphQLPlugin();
const backendPersistPlugin = 
    new PersistGraphQLPlugin({provider: clientPersistPlugin});

var frontendWebpackConfig = {
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        // ...
        use: [
          'babel-loader', 
          // Should come AFTER babel
          'persistgraphql-webpack-plugin/js-loader'
        ]
      },
      {
        test: /\.(graphql|gql)$/,
        use: [
          'graphql-tag/loader', 
          // Should come AFTER graphql-tag/loader
          'persistgraphql-webpack-plugin/graphql-loader'
        ]
      },
    ]
  }
  
  plugins: [
    frontendPersistPlugin
  ]
};

var backendWebpackConfig = {
  // ...
  plugins: [
    backendPersistPlugin
  ]
}

Both in the source code of front-end and back-end persisted GraphQL queries will be injected as a virtual module persisted_queries.json. This module will be updated if queries added or changed.

1
2
var queryMap = require('persisted_queries.json');
console.log(queryMap);

Options

1
new PersistGraphQLPlugin(options: object)
  • filename {String} Name of the ouput file with persisted GraphQL queries|
  • addTypename {Boolean} Apply a query transformation to the query documents, adding the __typename field at every level of the query. You must pass this option if your client code uses this query transformation.|
  • moduleName {String} Name of virtual wepback module with persisted GraphQL queries, persisted_queries.json by default|
  • provider {Object} Instance of plugin running on another webpack instance which will provide persisted GraphQL queries|

Looking to hire a software developer?
Don't hesitate to contact us.

Comments