Secure your GraphQL API with Persisted Documents

Execute only the GraphQL operations your apps need and reject everything else. Learn how persisted documents shrink your API's attack surface.

What are persisted documents?

A GraphQL endpoint allows executing complex arbitary queries. That flexibility is useful for developers, but a risk for production GraphQL APIs. Unless you are running a public GraphQL API, there is no reason for an unknown client to invent an ad-hoc operation in production.

A persisted document (also referred to as trusted document or persisted query) is a query, mutation, or subscription that that has a stable ID assigned and is known by the GraphQL server or Gateway.

Clients send the document ID instead of the document string. Your GraphQL server or gateway loads the corresponding document from a persisted document store. All arbitary GraphQL operations are rejected.

Executing arbitary queries belongs into the development process, not the production environment.

Client

POST /graphql
documentId:
sha256:7c6fa92e

GraphQL endpoint

Known documentResolve and execute
×
Anything elseReject before execution

Why Persisted Documents?

persisted documents turn your GraphQL endpoint from an open execution surface into an allowlist.
The benefits reach far beyond security.

Adopting Persisted Documents

Build an allowlist from your client operations, publish them to a store, and enforce it on your GraphQL endpoint.

  1. Step 1

    Generate your document manifest

    Extract the reviewed GraphQL operations from your client code and assign each one a stable document ID (e.g. SHA-256 hash). Generate the manifest with GraphQL Code Generator or Relay.

  2. Step 2

    Write manifest to store or registry

    Write the manifest to a blob storage such as S3 or use a schema registry such as Hive, which futher validates the manifest against your schema, groups it by app and version, and distributes the approved documents through its CDN.

  3. Step 3

    Enforce on your GraphQL endpoint

    Clients send hashes instead of operation strings. Your server, router, or gateway resolves each document ID via your store or schema registry and rejects anything that was not reviewed and published before execution.

Persisted Documents on Hive

From development to publishing to distribution and GraphQL endpoint enforcement, see how each part of the workflow fits together with the Hive Platform.

Arbitary vs. Allowlist Execution

Without an allowlist, any valid operation can run. A curious visitor or malicious actor has full access to your GraphQL schema and can craft potential harmful queries.

Disabling Introspection and disabling suggestions might reduce risk, but tools exist for reverse engineering the full GraphQL schema via brute-force.

What changes with persisted documents

With persisted documents, the runtime resolves a documentId against a published allowlist. Operations that were never reviewed, published, or shipped are rejected before they are ever executed.

  • Only operations that passed code review can run
  • Unpublished or tampered IDs are rejected outright
  • No introspection-based discovery of hidden fields

Do not confuse this with APQ

Automatic Persisted Queries are a bandwidth optimization. An unknown ID is retried with the full query and stored automatically. persisted documents are registered through an authenticated delivery pipeline; production clients cannot add new operations.

Read the persisted documents guide
your-app200

query GetAccount { account { id } }

unknown-client200

query ExploreAdmin { admin { users } }

unknown-client200

query ExpensiveSearch(first: 99999) {

No allowlist: any valid operation runs, whoever sends it.

sha256:2e94ac18ALLOW

GetAccount · web 4.8.0

query ExploreAdmin{REJECT

No document ID

sha256:unknownREJECT

Document not published

Allowlist: only the persisted documents you allow can run.

Hive Console and App Deployments

App Deployments group persisted documents by application and version. Hive validates them against your schema, distributes them through its CDN, tracks their usage, and includes active versions in breaking-change checks for proposed schema changes.

Check on pull requests, publish on deploy

Generate a manifest from your client operations, verify it in CI, and publish it from your CD pipeline before the application that depends on it goes live.

  • Generate — build the manifest from your client operations
  • Check — run hive app:check in pull-request CI
  • Publish — run hive app:create --publish from CD
  • Deploy — release only after documents are available

Because Hive knows which app versions are still active, you can alter your GraphQL schema with confidence.

Explore App Deployments

storefront

Active documents

3 versions

GetAccount

sha256:2e94ac18

web · 4.8.0

published

CompleteCheckout

sha256:7c6fa92e

web · 4.8.0

published

UpdateProfile

sha256:913dbe03

ios · 12.2.1

published

Hive Console groups persisted documents by application and version.

Enforce It in Any GraphQL Runtime

Enforcement happens on your GraphQL endpoint. You do not need a dedicated router or gateway. Add simple middleware to your existing GraphQL server, use GraphQL Yoga, or choose a runtime with persisted document support built in.

Hive provides two ready-made options: the Rust Hive Router for high performance, or the JavaScriptHive Gateway for flexible customization. Both resolve published documents from the Hive CDN and reject arbitrary GraphQL documents.

The request path

  • The client sends a documentId and variables
  • Your GraphQL runtime resolves that ID from the document store
  • Unknown IDs stop there
  • Known documents execute

Migrating an existing API

For an existing API, first observe requests arriving without IDs, migrate those clients, and only then enable strict enforcement. Hive's usage reporting tells you when it is safe to flip the switch.

Request

documentId:
sha256:7c6fa92e

GraphQL API

Resolve ID from Hive CDN

Unknown IDs stop here.

GraphQL API

Execute persisted document

Variables and identity still validated.

Why Choose Hive for persisted documents?

Frequently Asked Questions About Persisted Documents

Get Started with Persisted Documents

Begin with our step-by-step App Deployments guide, which walks you through generating a manifest, checking it in CI, publishing it, and enforcing it at your GraphQL edge.

Start building now