Consultas persistentes e consultas persistentes automáticas
Reduza os tamanhos das solicitações e melhore o desempenho registrando consultas antecipadamente. Aprenda como funcionam as consultas persistentes automáticas (APQ) e como habilitá-las no GraphQL do Spring Boot.
Consultas persistentes e consultas persistentes automáticas é uma aula grátis de GraphQL APIs with Spring Boot no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de GraphQL APIs with Spring Boot, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de GraphQL APIs with Spring Boot inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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.
Perguntas Frequentes
A aula “Consultas persistentes e consultas persistentes automáticas” é grátis?
Sim — o texto completo de “Consultas persistentes e consultas persistentes automáticas” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de GraphQL APIs with Spring Boot, atualize para CoddyKit PRO. O curso de GraphQL APIs with Spring Boot inclui 4 aulas no total.
O que vou aprender em “Consultas persistentes e consultas persistentes automáticas”?
Reduza os tamanhos das solicitações e melhore o desempenho registrando consultas antecipadamente. Aprenda como funcionam as consultas persistentes automáticas (APQ) e como habilitá-las no GraphQL do… Você pratica GraphQL APIs with Spring Boot com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar GraphQL APIs with Spring Boot?
Nenhuma experiência prévia é necessária. GraphQL APIs with Spring Boot no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Consultas persistentes e consultas persistentes automáticas”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de GraphQL APIs with Spring Boot?
Sim. Cada aula de GraphQL APIs with Spring Boot inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Análise da Complexidade de Consultas
- Estratégias de Cache para GraphQL
- Monitoramento e Rastreamento do GraphQL
- Consultas persistentes e consultas persistentes automáticas