@shopify/graphql-persisted
Apollo and Koa integrations for persisted GraphQL queries.
Last updated 13 days ago by shopify-dep .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @shopify/graphql-persisted 
SYNC missed versions from official npm registry.

@shopify/graphql-persisted

Build Status License: MIT npm version npm bundle size (minified + gzip)

Apollo and Koa integrations for persisted GraphQL queries.

Installation

$ yarn add @shopify/graphql-persisted

Usage

This package includes two parts, both of which must be used together in order to make persisted GraphQL queries:

  • An Apollo link that knows how to add persisted document IDs to an operation, and interpret server responses indicating that a persisted query was not found
  • A Koa middleware that knows how to extract a persisted document ID and fetch the full document body

Apollo

The @shopify/graphql-persisted/apollo entrypoint exports a createPersistedLink function. This function returns an Apollo link which should be added before the HTTP link:

import ApolloClient from 'apollo-client';
import {ApolloLink} from 'apollo-link';
import {createHttpLink} from 'apollo-link-http';
import {createPersistedLink} from '@shopify/graphql-persisted/apollo';

const client = new ApolloClient({
  /* other options, like the cache */
  link: ApolloLink.from([createPersistedLink(), createHttpLink()]),
});

This function accepts an optional options object. The following option is available:

  • idFromOperation?(operation: Operation): string | undefined | null: calculates the unique ID to use for the persisted query, which will eventually be passed to the server’s cache#get method to retrieve the full query body. If omitted, this option will default to pulling the id field off of the operation.query DocumentNode, which works well in combination with documents compiled using graphql-mini-transforms (used by default in sewing-kit).

The behavior of this link when a persisted query is not found for a particular ID depends on the cacheMissBehavior passed to your server middleware, which is documented below.

Koa

The Koa middleware from this package will parse the request body, extract the ID, and attempt to resolve it to a full document body, replacing the request body’s query field if a match is found. You can construct this middleware with the following options:

  • cache: A cache object with a get and set method (both of which can be asynchronous). You can use this option to store and retrieve persisted documents from a dedicated store like Redis.
  • cacheMissBehavior: A member of the exported CacheMissBehavior enum. This dictates how the system will handle cases where a full document body could not be retrieved for an ID. The default is CacheMissBehavior.AlwaysSend.
import Koa from 'koa';
import redisStore from 'koa-redis';
import {
  createPersistedGraphQLMiddleware,
  CacheMissBehavior,
} from '@shopify/graphql-persisted/koa';

const app = new Koa();

// We'll use koa-redis since we probably use redis for sessions,
// too. You can use any store you want, though.
const redis = redisStore();

app.use(
  createPersistedGraphQLMiddleware({
    cache: redis,
    cacheMissBehavior: CacheMissBehavior.SendAndStore,
  }),
);

// additional code, including whatever middleware will actually
// handle the GraphQL query

When the middleware attempts to resolve an ID to a query document, it attempts to do the following:

  • If ctx.state.assets.graphQLSource exists, call it and wait on the result. If a result is found and a cache option was provided when creating the middleware, return the result and save it to the cache using cache.set. This step makes it automatic to get persisted queries going for apps using sewing-kit and @shopify/sewing-kit-koa.
  • If a cache is present, call cache.get with the ID and wait on the result.

If no value was found during this process, the GraphQL call will fail in a way determined by the cacheMissBehavior option:

  • CacheMissBehavior.SendAlways (default): the server will respond to the client with an error that tells it to always send the full query document for operations with that ID. It will never attempt to store those documents in the cache. The query that had this error will be immediately retried before calling the remaining Apollo links in the chain.
  • CacheMissBehavior.SendAndStore: the server will respond to the client with an error that tells it to send the full query document on an immediate retry. The server will then store that value in the cache against the ID. Subsequent calls will once again only send the ID.
  • CacheMissBehavior.Error: the server will respond to the client with an error, and no retry will be performed.

These options provide you full control over the behavior of clients sending persisted queries you do not recognize. If you use sewing-kit and want to get persisted queries only for the "current" set of queries, but want to allow old queries to still work, use SendAlways. If you want to maximize the performance benefit of persisted queries, use SendAndStore. Finally, if you want to prevent queries that have not already been persisted, use Error.

Current Tags

  • 1.1.9                                ...           latest (13 days ago)

10 Versions

  • 1.1.9                                ...           13 days ago
  • 1.1.8                                ...           a month ago
  • 1.1.7                                ...           2 months ago
  • 1.1.6                                ...           2 months ago
  • 1.1.5                                ...           3 months ago
  • 1.1.4                                ...           3 months ago
  • 1.1.2                                ...           3 months ago
  • 1.1.1                                ...           3 months ago
  • 1.0.1                                ...           3 months ago
  • 1.0.0                                ...           5 months ago
Downloads
Today 0
This Week 10
This Month 38
Last Day 0
Last Week 28
Last Month 33
Dependencies (6)
Dev Dependencies (1)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |