|
| 1 | +# Persisted Documents |
| 2 | + |
| 3 | +GraphQL _executable documents_ containing at least one operation definition |
| 4 | +and optional fragment defintions can be persisted with a schema. |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +The optional `executables` argument of `@sdl` takes a list of input document files |
| 9 | +that are GraphQL _executable documents_. |
| 10 | + |
| 11 | +``` |
| 12 | + @sdl( |
| 13 | + files: [] |
| 14 | + executables: [{ document: "operations.graphql", persist: true }] |
| 15 | + ) |
| 16 | +``` |
| 17 | + |
| 18 | +When a schema is deployed the _executable documents_ must validate successfully |
| 19 | +against the schema. |
| 20 | + |
| 21 | +By itself this can be used to validate applications' use |
| 22 | +of a GraphQL endpoint remain valid when the schema changes. |
| 23 | +This requires that the application loads GraphQL requests from |
| 24 | +files containing executable documents that are declared in the schema. |
| 25 | + |
| 26 | +In addition any _executable document_ in the schema that is marked with `persist: true` |
| 27 | +is loaded as a _persisted document_. |
| 28 | + |
| 29 | +A client uses a _persisted document_ by specifying its document identifier |
| 30 | +(based upon a SHA256 hash) in a request instead of the content of the _executable document_. |
| 31 | + |
| 32 | +For example instead of this POST body for a request: |
| 33 | + |
| 34 | +``` |
| 35 | +{ |
| 36 | + "query": "{__typname}" |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +a request can use this: |
| 41 | + |
| 42 | +``` |
| 43 | +{ |
| 44 | +"documentId": "sha256:ecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38" |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +if an _executable document_ was declared in the schema with the matching SHA256 hash. |
| 49 | + |
| 50 | +The request parameters `operationName` and `variables` can be used as required with `documentId`. |
| 51 | + |
| 52 | +## Benefits |
| 53 | + |
| 54 | +Use of persisted documents typically saves network bandwidth as for real application requests |
| 55 | +the hash is smaller than the body of the _executable document_. |
| 56 | + |
| 57 | +In addition query requests can use HTTP GET while maintaining a reasonable sized URL |
| 58 | +that improves caching of URL requests. For example the above request can be a coded as an HTTP GET: |
| 59 | + |
| 60 | +``` |
| 61 | +https://london.us-east-a.ibm.stepzen.net/api/customer/graphql?documentId=sha256:ecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38 |
| 62 | +``` |
| 63 | + |
| 64 | +## Executable Documents |
| 65 | + |
| 66 | +A good practice is to ensure that a CI/CD process ensures that _executable documents_ declared as |
| 67 | +part of the schema using `@sdl(executables:)` are formatted consistentcy, using tooling such as `prettier`. |
| 68 | + |
| 69 | +For example the simple _executable document_ shown above `{__typename}` stored in a document file |
| 70 | +and formatted will have contents (including a newline at the end): |
| 71 | + |
| 72 | +``` |
| 73 | +{ |
| 74 | + __typename |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +and thus a document identifier of `sha256:8d8f7365e9e86fa8e3313fcaf2131b801eafe9549de22373089cf27511858b39`. |
| 79 | + |
| 80 | +Clients can obtain the SHA256 hash for a document identifer using any standard mechanism for calculating hashes, |
| 81 | +for example on Linux/Unix systems this command can be used: |
| 82 | + |
| 83 | +``` |
| 84 | +shasum -a 256 operations.graphql |
| 85 | +``` |
| 86 | + |
| 87 | +When _executable document_ are persisted using `@sdl(executables:)` the schema calculates the document identifiers automatically. |
0 commit comments