All Projects → leoasis → Graphql Persisted Document Loader

leoasis / Graphql Persisted Document Loader

Licence: mit
Webpack loader that adds a documentId to a compiled graphql document, which can be used when persisting/retrieving documents

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Graphql Persisted Document Loader

Graphql Let
A layer to start/scale the use of GraphQL code generator.
Stars: ✭ 282 (+281.08%)
Mutual labels:  graphql, webpack-loader
Graphql Import Loader
Webpack loader for `graphql-import`
Stars: ✭ 84 (+13.51%)
Mutual labels:  graphql, webpack-loader
Graphql Microservices
Showcasing a graphql microservice setup
Stars: ✭ 68 (-8.11%)
Mutual labels:  graphql
Serverless Backend
The serverless back end for JAMstack CMS. Use this back end to deploy a custom CMS using your own front end.
Stars: ✭ 73 (-1.35%)
Mutual labels:  graphql
Jshint Loader
[DEPRECATED] jshint loader for webpack, please migrate on `eslint`
Stars: ✭ 69 (-6.76%)
Mutual labels:  webpack-loader
Hasura Actions Examples
Examples of handling custom business logic with Hasura Actions
Stars: ✭ 69 (-6.76%)
Mutual labels:  graphql
Apollo Mocked Provider
Automatically mock GraphQL data with a mocked ApolloProvider
Stars: ✭ 70 (-5.41%)
Mutual labels:  graphql
Graphql Typescript
Define and build GraphQL Schemas using typed classes
Stars: ✭ 67 (-9.46%)
Mutual labels:  graphql
Go Graphql Api
Source code for a tutorial on Medium I published - "Building an API with GraphQL and Go"
Stars: ✭ 73 (-1.35%)
Mutual labels:  graphql
Graphql Batch
A query batching executor for the graphql gem
Stars: ✭ 1,164 (+1472.97%)
Mutual labels:  graphql
Type Graphql Dataloader
TypeGraphQL + DataLoader + TypeORM made easy
Stars: ✭ 73 (-1.35%)
Mutual labels:  graphql
Sketch Loader
Webpack loader for Sketch (+43) files
Stars: ✭ 69 (-6.76%)
Mutual labels:  webpack-loader
Kotlin Graphql Sample
Sample implementation of Kotlin+Spring+GraphQL
Stars: ✭ 69 (-6.76%)
Mutual labels:  graphql
Serverless Graphql Workshop
GraphQL and Serverless workshop
Stars: ✭ 70 (-5.41%)
Mutual labels:  graphql
Apollo Server Vercel
⚫ Production-ready Node.js GraphQL server for Vercel Serverless Functions
Stars: ✭ 69 (-6.76%)
Mutual labels:  graphql
Graphql Schema Language Cheat Sheet
GraphQL Shorthand Notation Cheat Sheet
Stars: ✭ 1,181 (+1495.95%)
Mutual labels:  graphql
Cynic
A bring your own types GraphQL client library for Rust
Stars: ✭ 68 (-8.11%)
Mutual labels:  graphql
Diana.jl
GraphQL for Julia
Stars: ✭ 69 (-6.76%)
Mutual labels:  graphql
Snowflaqe
A dotnet CLI tool to work with GraphQL queries: static query verification, type checking and code generating type-safe clients for F# and Fable.
Stars: ✭ 69 (-6.76%)
Mutual labels:  graphql
Svg Sprite Webpack Plugin
Webpack plugin for loading/extracting SVGs into a sprite file
Stars: ✭ 73 (-1.35%)
Mutual labels:  webpack-loader

GraphQL Persisted Document Loader

Webpack loader that assigns a documentId to a compiled GraphQL document's AST.

Why

When dealing with persisted documents in GraphQL, tools like PersistGraphQL generate a map from query to id that helps you determine the id for a given document and send that to the server instead of the full query string. This is useful to optimize the payload sent to the server, and also to allow the server to not parse and validate those queries, and also to optimize them particularly since the server now knows which queries the client will send.

However, on the client we still need to load up this map of queries to ids, which may become too big to load in one shot if your app has quite some queries. Moreover, if you are using code splitting, you'll be loading a file that includes queries for sections of your app that may never be executed or loaded.

To solve this problem, this loader works after the graphql-tag loader by injecting the document id as a property to the compiled AST, so you can access it directly when importing/requiring the document. This effectively co-locates the id with the query, and you no longer need a big lookup map to get the id for a particular query document.

Installation and setup

You need to have the graphql-tag (>= v2.8.0) package installed.

First install this package

npm install --save-dev graphql-persisted-document-loader

Then in the webpack configuration, add the loader BEFORE the graphql-tag/loader:

Note: This loader currently only works for .graphql files. It does not work for gql calls within JS files.

module.exports = {
  // ...,
  module: {
    rules: [
      {
        test: /\.graphql$/, use: [
          { loader: 'graphql-persisted-document-loader' }, // <= Before graphql-tag/loader!
          { loader: 'graphql-tag/loader' }
        ]
      }
    ]
  }
};

Usage

When importing or requiring .graphql files, you'll have the documentId property accessible from the imported object:

import query from 'query.graphql';
// OR
const query = require('query.graphql');

console.log(query.documentId); // => 5eef6cd6a52ee0d67bfbb0fdc72bbbde4d70331834eeec95787fe71b45f0a491

Loader options

  • generateId: function (querySource: string) => string Function that allows to generate a custom documentId from the query source. This source contains all the dependencies sources concatenated, so it's suitable for hashing. By default it generates the sha256 hash in hexadecimal format. The source is concatenated in the same way as you'd get it from the persistgraphql tool, so hashing the queries from the output of that tool should get you the same hash value.
  • 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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].