can lighthouse Laravel GraphQL cache the APIs response?

79 views Asked by At

Recently I have been working on GraphQL APIs. Due to the scalability of the project, I want to know if can i cache the response coming from apis.

I know that we can use the query cache and schema cache facility from the lighthouse package. However, my concern is they cache the response during the use of the cache facility in the project.

if we cannot cache the API's response using query cache directives then how can I can do that in my project?

1

There are 1 answers

0
Alex On

There a multiple levels of caching possible which you can read about in the documentation.

The first is field level caching with the @cache directive. This will cache the result of a field in your application cache. You can read about the specifics in the docs. Small excerpt:

If some fields in your schema are expensive or slow to compute, it can be beneficial to cache their result. Use the @cache directive to instruct Lighthouse to cache the result of a resolver. The cache is created on the first request and is cached forever by default. Use this for values that seldom change and take long to fetch/compute.

The other one is a full request/response cache, you can achieve this with a CDN or proxies like Varnish, how to set this up is out of scope but this requires the Cache-Control HTTP header to instruct the caching layer if and how it can cache the response. You can read about how to configure this in the docs. Small excerpt:

You can change the Cache-Control header (opens new window) of your response regardless of @cache by adding the @cacheControl directive to a field. The directive can be defined on the field-level or type-level. Note that field-level settings override type-level settings.