4 ms·
That is not a naive, but very good question. Traditionally, many people assume, that GraphQL itself is hard to cache, while REST isn't, because REST can leverag
by timsuchanek 5y ago
That is not a naive, but very good question.
Traditionally, many people assume, that GraphQL itself is hard to cache, while REST isn't, because REST can leverage HTTP's power, while GraphQL with its POST requests can't.
However, that is missing the point.
What we need to talk about first is, how you want to invalidate your cache. Do you want to set a TTL of 60 seconds? That might work for certain apps - both in REST and GraphQL, but many apps can't afford stale content for such an amount of time.
You'll need cache invalidation when content changes.
That on its own is a hard problem, no matter if REST, GraphQL or any other protocol.
And it is one of the main reasons we built GraphCDN: Making it easy to purge the cache, when relevant content changed. How? We give you a purging api (also GraphQL) and additionally GraphQL has the concepts of mutations.
Once you run a mutation through GraphCDN, it'll detect the relevant entities involved and purge the cache accordingly.
So - yes, in GraphQL caching on the surface might seem harder - but we're not just solving the "I can't cache POST requests" problem, but rather give you powerful cache purging - which is only possible due to the well-defined structure of a typed GraphQL Schema.
Because of that, we're actually thinking of providing REST "connectors" one day - turning REST into GraphQL, so you can have one unified interface that is easy to cache and invalidate.
- pier25 5y ago> You'll need cache invalidation when content changes Or you can use Vercel's stale-while-revalidate which will update the cache periodically while (temporarily) serving stale responses.
- timsuchanek 5y agoYes, which we btw also offer. However, it's just one way to invalidate. The first request after the max age expired will still be stale, even if new content is refetched within the swr time frame - in certain applications not acceptable. Most of our customers even use both things together to reduce the likelyhood for stale content.