Zapytania utrwalone i automatyczne zapytania utrwalone
Zmniejszą Państwo rozmiar żądań i poprawią wydajność, rejestrując zapytania z wyprzedzeniem. Dowiedzą się Państwo, jak działa Automatic Persisted Queries (APQ) i jak włączyć je w Spring Boot GraphQL.
Zapytania utrwalone i automatyczne zapytania utrwalone to bezpłatna lekcja GraphQL APIs with Spring Boot na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej GraphQL APIs with Spring Boot, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs GraphQL APIs with Spring Boot zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Często zadawane pytania
Czy lekcja „Zapytania utrwalone i automatyczne zapytania utrwalone” jest bezpłatna?
Tak — pełny tekst „Zapytania utrwalone i automatyczne zapytania utrwalone” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu GraphQL APIs with Spring Boot, przejdź na CoddyKit PRO. Kurs GraphQL APIs with Spring Boot zawiera 4 lekcji w sumie.
Co nauczysz się w „Zapytania utrwalone i automatyczne zapytania utrwalone”?
Zmniejszą Państwo rozmiar żądań i poprawią wydajność, rejestrując zapytania z wyprzedzeniem. Dowiedzą się Państwo, jak działa Automatic Persisted Queries (APQ) i jak włączyć je w Spring Boot GraphQL. Ćwiczysz GraphQL APIs with Spring Boot z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć GraphQL APIs with Spring Boot?
Nie wymagamy żadnego doświadczenia. GraphQL APIs with Spring Boot w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Zapytania utrwalone i automatyczne zapytania utrwalone”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji GraphQL APIs with Spring Boot?
Tak. Każda lekcja GraphQL APIs with Spring Boot zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Analiza złożoności zapytań
- Strategie buforowania dla GraphQL
- Monitorowanie i śledzenie GraphQL
- Zapytania utrwalone i automatyczne zapytania utrwalone