|
| 1 | +# Apollo Trusted Documents Plugin |
| 2 | + |
| 3 | +This plugin enables support for trusted documents (persisted queries) in Apollo Server, with built-in support for GraphQL Hive's App Deployments. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +pnpm add @labdigital/apollo-trusted-documents |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### With GraphQL Hive |
| 14 | + |
| 15 | +```ts |
| 16 | +import { ApolloServer } from "@apollo/server"; |
| 17 | +import { TrustedDocumentsPlugin, HiveStore } from "@labdigital/apollo-trusted-documents"; |
| 18 | +import Keyv from "keyv"; |
| 19 | + |
| 20 | +// Create a cache for the documents (e.g., using Keyv) |
| 21 | +const cache = new Keyv(); |
| 22 | + |
| 23 | +const trustedDocumentsPlugin = new TrustedDocumentsPlugin({ |
| 24 | + store: new HiveStore({ |
| 25 | + endpoint: "https://cdn.graphql-hive.com", |
| 26 | + accessToken: process.env.HIVE_CDN_ACCESS_TOKEN, |
| 27 | + cache, |
| 28 | + }), |
| 29 | + // Optional: strict mode (default: true) |
| 30 | + // When enabled, only trusted documents are allowed |
| 31 | + strict: true, |
| 32 | + // Optional: bypass header for development/testing |
| 33 | + bypassHeader: "x-bypass-trusted-operations", |
| 34 | + bypassSecret: process.env.BYPASS_SECRET, |
| 35 | +}); |
| 36 | + |
| 37 | +const server = new ApolloServer({ |
| 38 | + typeDefs, |
| 39 | + resolvers, |
| 40 | + plugins: [trustedDocumentsPlugin], |
| 41 | +}); |
| 42 | + |
| 43 | +await server.start(); |
| 44 | +``` |
| 45 | + |
| 46 | +### Custom Document Store |
| 47 | + |
| 48 | +You can implement your own document store by implementing the `DocumentStore` interface: |
| 49 | + |
| 50 | +```ts |
| 51 | +import type { DocumentStore } from "@labdigital/apollo-trusted-documents"; |
| 52 | + |
| 53 | +class MyCustomStore implements DocumentStore { |
| 54 | + async get(documentId: string): Promise<string | undefined> { |
| 55 | + // Fetch the document from your custom store |
| 56 | + return myDatabase.getDocument(documentId); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +const trustedDocumentsPlugin = new TrustedDocumentsPlugin({ |
| 61 | + store: new MyCustomStore(), |
| 62 | +}); |
| 63 | +``` |
| 64 | + |
| 65 | +## Options |
| 66 | + |
| 67 | +| Option | Type | Default | Description | |
| 68 | +|--------|------|---------|-------------| |
| 69 | +| `store` | `DocumentStore` | - | The document store to use for fetching trusted documents | |
| 70 | +| `strict` | `boolean` | `true` | When enabled, only trusted documents are allowed | |
| 71 | +| `bypassHeader` | `string` | `"x-bypass-trusted-operations"` | Header name to check for bypass secret | |
| 72 | +| `bypassSecret` | `string` | - | Secret value to allow bypassing trusted documents check | |
0 commit comments