GraphQL queries are usually POST
requests. POST
request aren't cached by nature. This means that we need different solutions to deliver our content efficiently.
-
Apollo Server provide a apollo-cache-control in order to configure how long a query should be cached. Cache hint inside the schema.
-
Automatic persisted queries (APQs) is a performance technique which allows a hash to be sent to the server instead of the entire GraphQL query string. It is possible to use APQs with
GET
requests here. In that way we can also benefit from CDN. How it works. In theory it's the same mechanism as theetag
header but on server-side.
- DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching. Here you can find a simple example.
- Automatic persisted queries allows us to use cacheable
GET
requests. - There are also native GraphQL CDN's like Fastql.
Read the article it shows in few examples how we can efficiently integrate REST API with GraphQL. It takes all points like HTTP caching, introspection and authorization into account. If you need a more comprehensive overview see here.