0Pricing
GraphQL APIs with Spring Boot · Lesson

Persisted Queries and Automatic Persisted Queries

Cut request payloads and improve performance by registering queries ahead of time. Learn how Automatic Persisted Queries (APQ) work and how to enable them in Spring Boot GraphQL.

Persisted Queries and Automatic Persisted Queries is a free GraphQL APIs with Spring Boot lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the GraphQL APIs with Spring Boot learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Cost of Sending Full Queries

Every GraphQL request normally carries the entire query string. Large queries mean larger payloads, more bandwidth, and repeated parsing and validation on the server.

Persisted queries reduce this overhead by sending a short hash instead.

What Is a Persisted Query?

A persisted query is a query string the server already knows, stored under a unique ID (usually a SHA-256 hash). Clients send the ID, and the server looks up the full query.

Automatic Persisted Queries (APQ)

APQ automates registration. The client first sends only the hash. If the server does not recognize it, the client resends with the full query, which the server then caches under that hash.

The APQ Handshake

The flow has two possible steps:

  • Client sends hash only -> if known, server responds
  • If unknown, server returns PersistedQueryNotFound
  • Client resends hash + full query -> server caches and responds

The Request Extension

APQ travels in the request's extensions field, carrying the hash and protocol version.

{
  "extensions": {
    "persistedQuery": { "version": 1, "sha256Hash": "abc123..." }
  }
}

Performance Benefits

Once a query is persisted, the server can skip parsing and validating its document, and the network carries only a small hash. This lowers latency and CPU for hot queries.

GET Requests and CDN Caching

Because a persisted query is just a hash, it fits in a URL. Sending it as a GET request lets a CDN cache the response, offloading the server entirely for repeated reads.

GET /graphql?extensions={"persistedQuery":{"sha256Hash":"abc123"}}

Enabling APQ in Spring

Add an AutomaticPersistedQueriesProvider backed by a cache (such as Caffeine) when building the GraphQL source.

PreparsedDocumentProvider provider =
    new ApolloPersistedQuerySupport(cacheStore);
GraphQL.newGraphQL(schema)
    .preparsedDocumentProvider(provider)
    .build();

Security as a Bonus

You can run APQ in safelist mode: only pre-approved queries are allowed, and unknown hashes are rejected outright. This blocks arbitrary client queries, shrinking your attack surface.

When Not to Use APQ

APQ shines for stable, repeated queries from your own apps. For ad-hoc tooling, exploratory queries, or constantly changing documents, the handshake overhead may outweigh the savings.

Best Practices

Get the most from persisted queries:

  • Size the cache to hold your hot queries
  • Use GET + CDN for cacheable reads
  • Consider safelist mode for production security
  • Monitor cache hit rates to tune

Quick Check

Test your persisted query knowledge.

Recap

You optimized with persisted queries:

  • Send a hash instead of the full query string
  • APQ registers queries automatically via a handshake
  • Skips re-parsing and enables GET + CDN caching
  • Safelist mode adds security; enable via a document provider

Persisted queries cut payload, latency, and attack surface for hot queries.

Frequently asked questions

Is the “Persisted Queries and Automatic Persisted Queries” lesson free?

Yes — the full text of “Persisted Queries and Automatic Persisted Queries” is free to read here on the web, and the GraphQL APIs with Spring Boot course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the GraphQL APIs with Spring Boot course, upgrade to CoddyKit PRO.

What will I learn in “Persisted Queries and Automatic Persisted Queries”?

Cut request payloads and improve performance by registering queries ahead of time. Learn how Automatic Persisted Queries (APQ) work and how to enable them in Spring Boot GraphQL. You practise GraphQL APIs with Spring Boot with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start GraphQL APIs with Spring Boot?

No prior experience is required. GraphQL APIs with Spring Boot on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Persisted Queries and Automatic Persisted Queries” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this GraphQL APIs with Spring Boot lesson?

Yes. Every GraphQL APIs with Spring Boot lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Query Complexity Analysis
  2. Caching Strategies for GraphQL
  3. Monitoring and Tracing GraphQL
  4. Persisted Queries and Automatic Persisted Queries
← Back to GraphQL APIs with Spring Boot