express-graphql
express-graphql copied to clipboard
Is caching not supported?
I've found no documentation about how to do caching in express-graphql, and I really would like to not have to switch to apollo-server, to be able to set a simple TTL per resolver. Is caching really not something you think should be built into express-graphql (a generic solution that can be overridden) or at least supported so you can easily add your own cache implementation? If caching is indeed possible, could anyone point point me in the right direction as to how to implement it.
@BruceL33t got the same issue - would prefer to use express-graphql instead of Apollo but need the caching goodness!
Hey, guys, it took me around 10Sec but I also google it and got here , I'm Switching some part of an existing Project to GraphQl , hope its helpful
const apicache = require('apicache') // you need to install this one const cache = apicache.middleware; const onlyStatus200 = (req, res) => res.statusCode === 200; const cacheSuccesses = cache('1 day', onlyStatus200);
THNEN: use this middlewhere :
app.use('/server/graphql',cacheSuccesses, graphqlHTTP({ schema: graphqlSchema, graphiql: true, }));
this is my prefer chaching , just to chache 200Code, is the best Idea I think, but you can follow Docs of Package and if you want you can chache whole response code
I was looking into this, but since you can add cache directives and you receive them inside the extensions
field it should be possible the map those fields and find the correct caching control and set those headers.
https://github.com/apollographql/apollo-server/blob/master/packages/apollo-cache-control/src/index.ts this is how apollo server does it.
This is an example of what you should receive:
"cacheControl": {
"version": 1,
"hints": [
{
"path": [
"post"
],
"maxAge": 240
},
{
"path": [
"post",
"votes"
],
"maxAge": 30
},
{
"path": [
"post",
"readByCurrentUser"
],
"scope": "PRIVATE"
}
]
}
I could create a middleware that checks the resolver result and if it has cache hints, if it does set the correct cache-control headers