How does caching work in GraphQL?

2019-07-24 17:10发布

问题:

GraphQL works as a query language posting always against a single end point. So most caching tools can't be used. So is network caching completely impossible with GraphQL? Is client side caching the only possible way to cache? Or is there a workaround?

回答1:

When using HTTP transport for your GraphQL API you can cache the HTTP responses just like any other HTTP traffic. Typically the CDN will hash the full request and then cache the response with that hash. When the same request comes in next time it won't connect to your GraphQL server, it will just respond with the cached results.

The challenge with GraphQL is that it's traditionally sent via POST, which is not supported by many CDNs. You can either use GET with a normal CDN (Fastly, Cloudflare, Cloudfront), or you can use a GraphQL specific CDN (FastQL).

Full disclosure: I built FastQL after running into this issue.