mercurius-js / cache

Adds an in-process caching layer to Mercurius. Federation is fully supported.
MIT License
106 stars 19 forks source link

Skip the cache if the request is a mutation #26

Closed codeflyer closed 2 years ago

codeflyer commented 2 years ago

When a mutation is done, the user can add a query in the request.

It will be useful to verify the type of request and skip the cache when is a mutation.

In this way the user will get always data up to date after a modify action.

sameer-coder commented 2 years ago

I think this is already possible. You can check the type of operation and skip it if it is a mutation

Example code:

app.register(cache, {
  async skip (self, arg, ctx, info) {
    if (info.operation.operation === 'mutation') {
      console.log('Mutation detected skipping')
      return true
    }
    return false
  },
  all: true,
  onHit: function (type, fieldName) {
    app.log.info({ msg: 'hit from cache', type, fieldName })
  }
})

@mcollina Is the above snippet correct? If yes, do you want me to add an example in the repo?

mcollina commented 2 years ago

I mean this should be the default implementation.Mutations should always be skipped.