4. Automatic persisted queries (APQ)
10m

Overview

We've seen how the 's cache helps with performance by storing and responses. It can also store a particular 's identifier, allowing client queries to take up fewer network resources.

In this lesson, we will:

  • Explore ()
  • Discuss the 's role in both receiving and sending
  • Explore output when using and build requests that utilize them

What are automatic persisted queries (APQ)?

optimize the clients send to a . Instead of sending the full operation string, the client sends an operation identifier: the unique SHA-256 hash of the string.

Let's take a look at how this works!

First, a client sends a request to the , containing both the string and its unique identifier.

A diagram showing a client sending an automatic persisted query, consisting of the query string and its SHA-256 hash, to the router

The server validates that the provided identifier matches the it receives. It calculates the SHA-256 hash of the operation string, then compares it with the provided identifier. If all goes well, it stores the identifier as an automatic .

A diagram showing the router receiving an APQ, generating its own hash from the query string, and validating the result

The next time the client wants to send the same , it no longer needs to send the full-length operation string, just the identifier.

A diagram showing a client sending an automatic persisted query, consisting of just the SHA-256 hash, to the router

The server checks its cached list of for the identifier and retrieves the full it corresponds to. It resolves the operation as usual and sends the data back to the client.

APQ and the router

In our federated architecture, the is uniquely positioned to be the go-between for both client and server. It receives queries from the client, then breaks them up and forwards them to each responsible .

A diagram showing a browser client sending a request to the router. The router breaks it up into smaller requests that it sends to subgraphs

As the first point of contact for clients, the acts as a server that can receive .

A diagram focusing on the router acting as a GraphQL server that receives the APQ

Then, it breaks up the request into smaller that are then sent to the responsible . In this way, the acts as a client sending .

A diagram focusing on the router acting as a GraphQL server that receives the APQ

Whether the is receiving the or sending queries on to , we can configure exactly how it handles .

Practice

Which of the following statements about APQ are true?

Key takeaways

  • () are strings that have been converted into SHA-256 hashes for faster between client and server.
  • To use , clients first send the an string along with its SHA-256 hash, for validation. When the router confirms its hash of the string matches the hash provided, it stores it for future use.
  • Subsequent queries for the same data can provide the hash instead of the entire string.

Up next

Up next, let's see exactly how the receives from a client.

Previous

Share your questions and comments about this lesson

This course is currently in

beta
. Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.

You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.