Automatic Persisted Queries

Automatic Persisted Queries can be used to avoid sending the query string each time and helps prepare the execution. To do so a query must be first associated with an unique identifier: the SHA-256 hash of the query string:

# SHA-256 4ef8d269e7944ef2cd6554ecb3d73164546945cf935806933448905abec554e5
query { __typename }

The query can be persisted at any time, it'll be cached for a day by sending the following payload:

{ "query": "query { __typename }", "extensions": { "persistedQuery": { "version": 1, "sha256Hash": "4ef8d269e7944ef2cd6554ecb3d73164546945cf935806933448905abec554e5" } } }

The next time you want to execute the same query, you can omit the query field like this:

{ "extensions": { "persistedQuery": { "version": 1, "sha256Hash": "4ef8d269e7944ef2cd6554ecb3d73164546945cf935806933448905abec554e5" } } }

If the query is not found, Grafbase will return the following error:

{ "errors": [ { "message": "Persisted query not found", "extensions": { "code": "PERSISTED_QUERY_NOT_FOUND" } } ] }

In this case you should send the request with the full query string.

Grafbase also supports executing GET HTTP requests. In this case fields should be passed as query parameters with their value being JSON-encoded first. So to execute a persisted query:

curl --get 'http://localhost:4000/graphql' \ --data-urlencode 'extensions={"persistedQuery":{"version":1,"sha256Hash":"4ef8d269e7944ef2cd6554ecb3d73164546945cf935806933448905abec554e5"}}'

And to register a query:

curl --get 'http://localhost:4000/graphql' \ --data-urlencode 'query=query { __typename }' \ --data-urlencode 'extensions={"persistedQuery":{"version":1,"sha256Hash":"4ef8d269e7944ef2cd6554ecb3d73164546945cf935806933448905abec554e5"}}'

This will allow you to benefist the most from our edge caching.

Was this page helpful?