Odyssey

Caching in the router
beta

Course overview and setupCreating our graphIn-memory cachingAutomatic persisted queries (APQ)Caching client APQSending APQ to subgraphsDistributed cachingConfiguring Prometheus
5. Caching client APQ
2m

Overview

When receiving queries from a client, the router takes on the role of a traditional GraphQL server. To reduce the amount of data sent over the network, clients can use APQ to send the router an operation hash in place of the entire operation string.

In this lesson, we will:

  • Walk through how the router validates and processes APQ from a client
  • Review configuration options for client APQ

APQ from the client

We can mimic a client—such as a frontend app running Apollo Client, which supports APQ—using a curl command in the terminal.

Let's build out our command. We'll send a new request to our router on http://127.0.0.1:5000. Next, define a --header flag with a value of 'content-type: application/json'.

curl --get http://127.0.0.1:5000 \
--header 'content-type: application/json'

We'll also specify two separate instances of --data-urlencode.

curl --get http://127.0.0.1:5000 \
--header 'content-type: application/json' \
--data-urlencode # TODO
--data-urlencode # TODO

Recall that the first time we send the query to the router, we need to send the actual full-length query string AND its identifier (its SHA-256 hash).

Let's use the first --data-urlencode flag to pass in our entire query string. We include it in the following format:

--data-urlencode 'query={featuredListings{description id title}}'

To include the hash as part of the second --data-urlencode flag, we'll define a lengthier string with some additional data. We start by specifying extensions, and set it equal to an object with a persistedQuery key. As the value for persistedQuery, we'll define another object.

--data-urlencode 'extensions={"persistedQuery":{}}'

This next object takes two keys: version and sha256hash. We've pre-calculated the SHA-256 hash of the provided query, so you can copy the contents below.

--data-urlencode 'extensions={"persistedQuery":{"version":1,"sha256Hash":"91c070d387e026c84301ba5d7b500a5647e24de65e9dd9d28f4bebd96cb9c11c"}}'

Note: We derived the SHA-256 hash for the query string by passing {featuredListings{description id title}} exactly (no extra spaces!) into a SHA-256 generator.

Here's what the entire command should look like. Pop open a terminal, and let's run it!

curl --get http://127.0.0.1:5000 \
--header 'content-type: application/json' \
--data-urlencode 'query={featuredListings{description id title}}' \
--data-urlencode 'extensions={"persistedQuery":{"version":1,"sha256Hash":"91c070d387e026c84301ba5d7b500a5647e24de65e9dd9d28f4bebd96cb9c11c"}}'

After just a moment, we'll see the response with our data!

{
"data":{
"featuredListings":[
{
"description":"Enjoy this amazing cave campsite in snow MoundiiX, where you'll be one with the nature and wildlife in this wintery planet. All space survival amenities are available. We have complementary dehydrated wine upon your arrival. Check in between 34:00 and 72:00. The nearest village is 3AU away, so please plan accordingly. Recommended for extreme outdoor adventurers.",
"id":"listing-1",
"title":"Cave campsite in snowy MoundiiX"
},
{
"description":"Thiz cozy yurt has an aerodyne hull and efficient sublight engines. It is equipped with an advanced sensor system and defensive force shield. Meteor showers are quite common, please rest assured that our Kevlar-level shields will keep you safe from any space debris. Mraza suns are known to have high levels of UV hyper radiation, which we counteract with the yurt's UV protection shield.",
"id":"listing-2",
"title":"Cozy yurt in Mraza"
},
{
"description":"Enjoy this floaty, repurposed aircraft reminiscent of Earth's former converted airstreams. Includes lake access!",
"id":"listing-3",
"title":"Repurposed mid century aircraft in Kessail"
}
]
}
}

Now let's try it again; this time removing the full query string from our curl command.

curl --get http://127.0.0.1:5000 \
--header 'content-type: application/json' \
--data-urlencode 'extensions={"persistedQuery":{"version":1,"sha256Hash":"91c070d387e026c84301ba5d7b500a5647e24de65e9dd9d28f4bebd96cb9c11c"}}'

With this command, we can send just the operation identifier, and our router knows exactly which operation we're talking about. We should see the exact same response in the terminal as before.

Note: We're using a curl command here to quickly showcase how APQ works in action. To work correctly, the client sending the queries needs logic to generate the identifiers for the APQ, make requests to the router using the hashed queries, and if necessary, retry requests with the full query string and the hash. To read more about configuring this behavior using Apollo Client, check out the Apollo documentation.

Caching client operation APQ

The router enables caching client operation APQ by default. We can further customize how many operations it stores, or even turn off the feature entirely, by setting some flags in our router-config.yml file.

Limits the number of APQ in the cache to 100
apq:
router:
cache:
in_memory:
limit: 100
Alternatively, disables client APQ support
apq:
enabled: false

We can also cache our APQ in a distributed cache, which we'll see later on in the course.

Practice

Automatic persisted queries and the router
Automatic persisted queries enable the router to send and receive a query's 
 
 rather than the entire 
 
. To cache an APQ, the router must first receive 
 
, which it then validates and stores for the future. Receiving APQ from a client 
 
, whereas sending APQ to subgraph servers 
 
.

Drag items from this box to the blanks above

  • is not supported

  • SHA-512 hash

  • both the query string and its corresponding hash

  • operation string

  • the query plan

  • is configured by default

  • must be configured separately

  • introspection response

  • SHA-256 hash

To pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.

Key takeaways

  • To support sending the router APQ, a client should implement the logic to convert operation strings to hashes, to send the router these hashes, and to retry the request in the event the router requires the original query string. Apollo Client supports APQ out of the box.

Up next

Receiving queries from clients isn't the router's only job; it must also divide queries between the subgraphs responsible for providing data. In the next lesson, we'll walk through how the router takes on the role of client when sending APQ to subgraphs.

Previous
Next

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.

              router

              The single access point for a federated GraphQL architecture. It receives incoming operations and intelligently routes them across component services before returning a unified response.

              GraphQL server

              A server that contains a GraphQL schema and can resolve client-requested operations that are executed against that schema.

              APQ

              Automatic persisted queries (APQ). A technique for improving network performance by automatically generating a unique hash for each operation and enabling clients to execute operations by passing the hash.

              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              router

              The single access point for a federated GraphQL architecture. It receives incoming operations and intelligently routes them across component services before returning a unified response.

              APQ

              Automatic persisted queries (APQ). A technique for improving network performance by automatically generating a unique hash for each operation and enabling clients to execute operations by passing the hash.

              APQ

              Automatic persisted queries (APQ). A technique for improving network performance by automatically generating a unique hash for each operation and enabling clients to execute operations by passing the hash.

              APQ

              Automatic persisted queries (APQ). A technique for improving network performance by automatically generating a unique hash for each operation and enabling clients to execute operations by passing the hash.

              router

              The single access point for a federated GraphQL architecture. It receives incoming operations and intelligently routes them across component services before returning a unified response.

              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              router

              The single access point for a federated GraphQL architecture. It receives incoming operations and intelligently routes them across component services before returning a unified response.

              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              router

              The single access point for a federated GraphQL architecture. It receives incoming operations and intelligently routes them across component services before returning a unified response.

              APQ

              Automatic persisted queries (APQ). A technique for improving network performance by automatically generating a unique hash for each operation and enabling clients to execute operations by passing the hash.

              router

              The single access point for a federated GraphQL architecture. It receives incoming operations and intelligently routes them across component services before returning a unified response.

              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              Apollo Client

              An open-source library for client-side state management and GraphQL operation handling in Javascript/Typescript. Apollo Client is a fully featured caching GraphQL client with integrations for React, Angular, and more.

              router

              The single access point for a federated GraphQL architecture. It receives incoming operations and intelligently routes them across component services before returning a unified response.

              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              APQ

              Automatic persisted queries (APQ). A technique for improving network performance by automatically generating a unique hash for each operation and enabling clients to execute operations by passing the hash.

              APQ

              Automatic persisted queries (APQ). A technique for improving network performance by automatically generating a unique hash for each operation and enabling clients to execute operations by passing the hash.

              router

              The single access point for a federated GraphQL architecture. It receives incoming operations and intelligently routes them across component services before returning a unified response.

              APQ

              Automatic persisted queries (APQ). A technique for improving network performance by automatically generating a unique hash for each operation and enabling clients to execute operations by passing the hash.

              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              Apollo Client

              An open-source library for client-side state management and GraphQL operation handling in Javascript/Typescript. Apollo Client is a fully featured caching GraphQL client with integrations for React, Angular, and more.

              router

              The single access point for a federated GraphQL architecture. It receives incoming operations and intelligently routes them across component services before returning a unified response.

              subgraphs

              A service in a federated GraphQL architecture. Acts as a module for a supergraph. Includes both GraphQL services and REST services integrated via Apollo Connectors.

              APQ

              Automatic persisted queries (APQ). A technique for improving network performance by automatically generating a unique hash for each operation and enabling clients to execute operations by passing the hash.

              NEW COURSE ALERT

              Introducing Apollo Connectors

              Connectors are the new and easy way to get started with GraphQL, using existing REST APIs.

              Say goodbye to GraphQL servers and resolvers—now, everything happens in the schema!

              Take the course