0Pricing
GraphQL APIs with Spring Boot · 课时

持久化查询与自动持久化查询

通过提前注册查询来缩减请求负载并提升性能。学习 Automatic Persisted Queries(APQ)的工作原理,以及如何在 Spring Boot GraphQL 中启用它们。

持久化查询与自动持久化查询 是 CoddyKit 上的免费 GraphQL APIs with Spring Boot 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 GraphQL APIs with Spring Boot 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 GraphQL APIs with Spring Boot 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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.

常见问题解答

「持久化查询与自动持久化查询」课时是免费的吗?

是的 — 「持久化查询与自动持久化查询」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 GraphQL APIs with Spring Boot 课程的其余内容,请升级到 CoddyKit PRO。 GraphQL APIs with Spring Boot 课程共包含 4 节课。

「持久化查询与自动持久化查询」这节课中我会学到什么?

通过提前注册查询来缩减请求负载并提升性能。学习 Automatic Persisted Queries(APQ)的工作原理,以及如何在 Spring Boot GraphQL 中启用它们。 你通过在浏览器中直接运行的动手代码来练习 GraphQL APIs with Spring Boot,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 GraphQL APIs with Spring Boot 需要有经验吗?

无需任何先前经验。CoddyKit 上的 GraphQL APIs with Spring Boot 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「持久化查询与自动持久化查询」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 GraphQL APIs with Spring Boot 课中编写并运行代码吗?

能。每节 GraphQL APIs with Spring Boot 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 查询复杂度分析
  2. GraphQL 缓存策略
  3. 监控与追踪 GraphQL
  4. 持久化查询与自动持久化查询
← 返回 GraphQL APIs with Spring Boot