Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Is caching not supported? #561

Open
BruceL33t opened this issue Sep 23, 2019 · 3 comments
Open

Is caching not supported? #561

BruceL33t opened this issue Sep 23, 2019 · 3 comments

Comments

@BruceL33t
Copy link

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.

@digitalle
Copy link

@BruceL33t got the same issue - would prefer to use express-graphql instead of Apollo but need the caching goodness!

@mlnima
Copy link

mlnima commented Oct 30, 2019

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

@klaaz0r
Copy link

klaaz0r commented Jan 9, 2020

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants