Automatic Persisted Queries (APQ)
Understanding APQ
Automatic Persisted Queries (APQ) is a feature that enables the GraphQL router to automatically store queries it receives in a persisted format, allowing clients to resubmit these queries by simply referencing their hash. This can significantly reduce payload size, especially in cases where the same query is repeatedly sent.
When a client sends a query, APQ stores it using a hash-based identifier. Future requests (even from different graphql clients) can then retrieve and execute the query using this hash, even through a GET operation, which is highly efficient for caching, especially over a Content Delivery Network (CDN).
Setting up APQ
To enable APQ, ensure that the automatic_persisted_queries
configuration section is defined in the GraphQL router configuration file. This setup supports both in-memory caching and Redis-based storage for persisted queries.
Using Local Cache
The local cache allows the APQ feature to store queries directly within the router's in-memory cache. This approach is simple and fast, although it does not support persistence across router restarts. The local cache is recommended for smaller applications or testing environments where query persistence across server sessions is not critical.
Using Redis Cache
For persistent query storage across server restarts, you can configure Redis as the storage provider. Redis allows queries to be stored with the specified TTL, and they remain available even if the router restarts, making this setup ideal for production environments.
Testing APQ
To test APQ, you can run the following steps (using the built in __typename
query)
First, ensure that this query fails first (because the query itself isn't persisted, and isn't recognized yet by the router)
Then, send a follow up request that has both the query string, as well as the hash, in the form:
This time, the server persists the query string, and then responds with the result we'd expect to see:
Finally, you can send the same query that we tried before, and you should be able to get the persisted query result
Last updated