永続化クエリとAutomatic Persisted Queries
クエリを事前登録してリクエストのペイロードを削減し、パフォーマンスを向上させます。Automatic Persisted Queries(APQ)の仕組みと、Spring Boot GraphQLでの有効化方法を学びます。
「永続化クエリとAutomatic Persisted Queries」はCoddyKit上の無料GraphQL APIs with Spring Bootレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.
よくある質問
「永続化クエリとAutomatic Persisted Queries」レッスンは無料ですか?
はい。「永続化クエリとAutomatic Persisted Queries」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、GraphQL APIs with Spring Bootコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 GraphQL APIs with Spring Bootコースには全4レッスンが含まれています。
「永続化クエリとAutomatic Persisted Queries」で何を学びますか?
クエリを事前登録してリクエストのペイロードを削減し、パフォーマンスを向上させます。Automatic Persisted Queries(APQ)の仕組みと、Spring Boot GraphQLでの有効化方法を学びます。 ブラウザで直接実行するハンズオンコードでGraphQL APIs with Spring Bootを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
GraphQL APIs with Spring Bootを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのGraphQL APIs with Spring Bootは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「永続化クエリとAutomatic Persisted Queries」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このGraphQL APIs with Spring Bootレッスンでコードを書いて実行できますか?
はい。すべてのGraphQL APIs with Spring Bootレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- クエリ複雑度の分析
- GraphQLのキャッシュ戦略
- GraphQLの監視とトレーシング
- 永続化クエリとAutomatic Persisted Queries