-
Notifications
You must be signed in to change notification settings - Fork 122
docs(router): Persisted Documents #7260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ardatan
wants to merge
10
commits into
main
Choose a base branch
from
persisted_documents
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4a8c2bc
docs(router): Persisted Documents
ardatan 311a4ef
More
ardatan 1e13ae6
Fixes
ardatan 6a15482
Fix the link
ardatan 59eccad
Body instead of params
ardatan ad7ccd2
Update docs
ardatan 4848b4e
More
ardatan 874c7b8
Example
ardatan 859c500
Remove extra locs
ardatan be8890e
Better statement
ardatan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
packages/web/docs/src/content/router/configuration/persisted_documents.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| --- | ||
| title: 'persisted_documents' | ||
| --- | ||
|
|
||
| # persisted_documents | ||
|
|
||
| The `persisted_documents` configuration allows you to enable [Persisted Documents] on Hive Router | ||
| that allows you to reduce the payload size of your GraphQL requests and secure your GraphQL API by | ||
| allowing only operations that are know and trusted by your Router. | ||
|
|
||
| [Learn more about Persisted Documents with Hive Console](/schema-registry/app-deployments#persisted-documents-on-graphql-server-and-gateway) | ||
|
|
||
| ## Configuration Structure | ||
|
|
||
| The `persisted_documents` key is a top-level object in your `router.config.yaml`. It contains the | ||
| following fields: | ||
|
|
||
| First you need to enable persisted documents by setting the `enabled` field to `true`. | ||
|
|
||
| ```yaml | ||
| persisted_documents: | ||
| enabled: true | ||
| ``` | ||
|
|
||
| ### Source Configuration with `source` | ||
|
|
||
| The `source` field defines where the Router will load the persisted documents from. It supports two | ||
| sources: Hive Console CDN and a local file. | ||
|
|
||
| - `source`: **(object)** The source configuration for persisted documents. | ||
|
|
||
| #### Hive Console CDN | ||
|
|
||
| Then we need a way to fetch the persisted documents. The recommended way is to use Hive Console as | ||
| your persisted documents storage. | ||
|
|
||
| - `hive`: **(object)** The Hive Console CDN source configuration. | ||
| - `endpoint`: **(string)** The Hive Console CDN endpoint to fetch persisted documents from. | ||
| Alternatively you can set the `HIVE_CDN_ENDPOINT` environment variable. | ||
| - `key`: **(string)** The Hive Console CDN API key to authenticate requests. Alternatively you can | ||
| set the `HIVE_CDN_KEY` environment variable. | ||
|
|
||
| To do so, you can either use environment variables or directly set the `endpoint` and `key` fields. | ||
|
|
||
| ```yaml {2-4} filename="router.config.yaml" | ||
| persisted_documents: | ||
| enabled: true | ||
| source: | ||
| hive: | ||
| endpoint: https://cdn.graphql-hive.com/artifacts/v1/... # Use your Hive CDN endpoint here | ||
| key: hvABCD # Use your Hive CDN API key here | ||
| ``` | ||
|
|
||
| or using environment variables: | ||
|
|
||
| ```yaml {2-4} filename="router.config.yaml" | ||
ardatan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| persisted_documents: | ||
| enabled: true | ||
| ``` | ||
|
|
||
| Then set the following environment variables in your environment: | ||
|
|
||
| ```bash | ||
| export HIVE_CDN_ENDPOINT="https://cdn.graphql-hive.com/artifacts/v1/..." | ||
| export HIVE_CDN_KEY="hvABCD" | ||
| ``` | ||
|
|
||
| #### A Persisted Documents File as Source | ||
|
|
||
| Alternatively, you can also load persisted documents from a local file. | ||
|
|
||
| - `file`: **(string)** The path to the local persisted documents JSON file. | ||
|
|
||
| ```yaml {2-3} filename="router.config.yaml" | ||
| persisted_documents: | ||
| enabled: true | ||
| source: | ||
| file: ./persisted_documents.json # point to your local persisted documents file | ||
| ``` | ||
|
|
||
| That file must be a JSON file containing a map of document IDs to GraphQL operations, for example: | ||
|
|
||
| ```json | ||
| { | ||
| "a1b2c3d4e5": "query GetUser($id: ID!) { user(id: $id) { id name } }", | ||
| "f6g7h8i9j0": "mutation UpdateUser($id: ID!, $name: String!) { updateUser(id: $id, name: $name) { id name } }" | ||
| } | ||
| ``` | ||
|
|
||
| ### Configure Extraction of Document ID from Requests with `spec` | ||
|
|
||
| The `spec` field defines how the Router will extract the persisted document ID from incoming | ||
| requests. By default it is set to `hive` which expects the document ID to be provided in the body as | ||
| `documentId`. | ||
|
|
||
| - `spec`: **(object)** The specification for extracting document IDs from requests. | ||
|
|
||
| #### `documentId` in Request Body (Hive / Default) | ||
|
|
||
| The default `hive` spec expects the persisted document ID to be provided in the request body as | ||
| `documentId`. | ||
|
|
||
| So the request body should look like this: | ||
|
|
||
| ```json | ||
| { | ||
| "documentId": "my-app~my-version~a1b2c3d4e5", | ||
| "variables": { | ||
| "id": "123" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Then the Router will look up the operation associated with the provided `documentId` and execute it. | ||
|
|
||
| #### Apollo Persisted Documents Spec using `extensions.persistedQuery.sha256Hash` | ||
|
|
||
| There is also support for the Apollo Persisted Documents spec, which expects the document ID to be | ||
| provided in the `extensions.persistedQuery.sha256Hash` field of the request body. | ||
|
|
||
| To use this spec, set the `spec` field to `apollo`: | ||
|
|
||
| ```yaml {3} filename="router.config.yaml" | ||
| persisted_documents: | ||
| enabled: true | ||
| spec: apollo | ||
| ``` | ||
|
|
||
| So the request body should look like this: | ||
|
|
||
| ```json | ||
| { | ||
| "extensions": { | ||
| "persistedQuery": { | ||
| "version": 1, | ||
| "sha256Hash": "ecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38" | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| which can be sent like this using `curl`: | ||
|
|
||
| ```bash | ||
| curl -X POST -H 'Content-Type: application/json' http://localhost:4000/graphql \ | ||
| -d '{"extensions":{"persistedQuery":{"version":1,"sha256Hash":"ecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38"}}}' | ||
| ``` | ||
|
|
||
| Then the Router will look up the operation associated with the provided `sha256Hash` and execute it. | ||
|
|
||
| ### Allowing Non-Persisted Arbitrary Documents with `allow_arbitrary_operations` | ||
|
|
||
| After enabling persisted documents, by default the Router will reject any requests that do not | ||
| provide a valid persisted document ID. | ||
|
|
||
| - `allow_arbitrary_operations`: **(boolean)** Whether to allow arbitrary operations alongside | ||
| persisted documents. Default is `false`. | ||
|
|
||
| If you want to allow arbitrary operations alongside persisted documents, you can set the | ||
| `allow_arbitrary_operations` field to `true`. | ||
|
|
||
| ```yaml {3} filename="router.config.yaml" | ||
| persisted_documents: | ||
| enabled: true | ||
| allow_arbitrary_operations: true | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.