Consultas persistentes y consultas persistentes automáticas
Reduzca el tamaño de las solicitudes y mejore el rendimiento registrando las consultas de antemano. Aprenda cómo funcionan las Automatic Persisted Queries (APQ) y cómo habilitarlas en Spring Boot GraphQL.
Consultas persistentes y consultas persistentes automáticas es una lección gratuita de GraphQL APIs with Spring Boot en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de GraphQL APIs with Spring Boot, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de GraphQL APIs with Spring Boot incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en 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.
Preguntas frecuentes
¿La lección «Consultas persistentes y consultas persistentes automáticas» es gratis?
Sí — el texto completo de «Consultas persistentes y consultas persistentes automáticas» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de GraphQL APIs with Spring Boot, actualiza a CoddyKit PRO. El curso de GraphQL APIs with Spring Boot incluye 4 lecciones en total.
¿Qué aprenderé en «Consultas persistentes y consultas persistentes automáticas»?
Reduzca el tamaño de las solicitudes y mejore el rendimiento registrando las consultas de antemano. Aprenda cómo funcionan las Automatic Persisted Queries (APQ) y cómo habilitarlas en Spring Boot Gra… Practicas GraphQL APIs with Spring Boot con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar GraphQL APIs with Spring Boot?
No se requiere experiencia previa. GraphQL APIs with Spring Boot en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Consultas persistentes y consultas persistentes automáticas»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de GraphQL APIs with Spring Boot?
Sí. Cada lección de GraphQL APIs with Spring Boot incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Análisis de complejidad de consultas
- Estrategias de caché para GraphQL
- Supervisión y trazado de GraphQL
- Consultas persistentes y consultas persistentes automáticas