0Pricing
GraphQL APIs with Spring Boot · Lezione

Query persistenti e Automatic Persisted Queries

Riduca il payload delle richieste e migliori le prestazioni registrando in anticipo le query. Scopra come funzionano le Automatic Persisted Queries (APQ) e come abilitarle in Spring Boot GraphQL.

Query persistenti e Automatic Persisted Queries è una lezione GraphQL APIs with Spring Boot gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento GraphQL APIs with Spring Boot, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso GraphQL APIs with Spring Boot include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Domande Frequenti

La lezione «Query persistenti e Automatic Persisted Queries» è gratuita?

Sì — il testo completo di «Query persistenti e Automatic Persisted Queries» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso GraphQL APIs with Spring Boot, passa a CoddyKit PRO. Il corso GraphQL APIs with Spring Boot include 4 lezioni in totale.

Cosa imparerò in «Query persistenti e Automatic Persisted Queries»?

Riduca il payload delle richieste e migliori le prestazioni registrando in anticipo le query. Scopra come funzionano le Automatic Persisted Queries (APQ) e come abilitarle in Spring Boot GraphQL. Eserciti GraphQL APIs with Spring Boot con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare GraphQL APIs with Spring Boot?

Non è richiesta alcuna esperienza precedente. GraphQL APIs with Spring Boot su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Query persistenti e Automatic Persisted Queries»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione GraphQL APIs with Spring Boot?

Sì. Ogni lezione GraphQL APIs with Spring Boot include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Analisi della complessità delle query
  2. Strategie di caching per GraphQL
  3. Monitoraggio e tracing di GraphQL
  4. Query persistenti e Automatic Persisted Queries
← Torna a GraphQL APIs with Spring Boot