|
| 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%3Aecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38 |
| 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 consistently, 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 documents_ are persisted using `@sdl(executables:)` the schema calculates the document identifiers automatically. |
| 88 | + |
| 89 | +## Example |
| 90 | + |
| 91 | +This example uses a simple mocked schema with a `Customer` type and a single `Query` field `customer`. |
| 92 | + |
| 93 | +`operations.graphql` contains three GraphQL query operations `Customer`, `CustomerEmail` and `CustomerName` |
| 94 | +each with a different selection against `Query.customer`. |
| 95 | + |
| 96 | +A client can execute them using these request parameters, shown as JavaScript: |
| 97 | + |
| 98 | +``` |
| 99 | +{ |
| 100 | + documentId: "sha256:9d50d8e35b5882139e836a126f5d6d5a28cf41c5efd80a6e67f920d284b5f6d0", |
| 101 | + operationName: "Customer", |
| 102 | + variables: { |
| 103 | + id: 1789, |
| 104 | + }, |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +``` |
| 109 | +{ |
| 110 | + documentId: "sha256:9d50d8e35b5882139e836a126f5d6d5a28cf41c5efd80a6e67f920d284b5f6d0", |
| 111 | + operationName: "CustomerEmail", |
| 112 | + variables: { |
| 113 | + id: 2845, |
| 114 | + }, |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +``` |
| 119 | +{ |
| 120 | + documentId: "sha256:9d50d8e35b5882139e836a126f5d6d5a28cf41c5efd80a6e67f920d284b5f6d0", |
| 121 | + operationName: "CustomerName", |
| 122 | + variables: { |
| 123 | + id: 3651, |
| 124 | + }, |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +For example use `curl` and HTTP `GET` as follows: |
| 129 | + |
| 130 | +``` |
| 131 | +curl \ |
| 132 | + --header "Authorization: Apikey $(stepzen whoami --apikey)" \ |
| 133 | + --header "Content-Type: application/json" \ |
| 134 | + 'https://london.us-east-a.ibm.stepzen.net/api/miscellaneous/graphql?documentId=sha256:9d50d8e35b5882139e836a126f5d6d5a28cf41c5efd80a6e67f920d284b5f6d0&operationName=Customer&variables=%7B%22id%22%3A%201789%7D' |
| 135 | +``` |
0 commit comments