mercurius-js / cache

Adds an in-process caching layer to Mercurius. Federation is fully supported.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Original resolver executed twice when an error is thrown

mfellner opened this issue · comments

When a resolver throws an error, the original resolver is executed twice.

const resolvers = {
  Query: {
    hello(parent, args, context) {
      console.log("query hello", context.count++);
      throw new Error("oops");
    },
  },
};

const app = Fastify();

app.register(mercurius, {
  schema,
  resolvers,
  context: () => ({ count: 0 }),
  graphiql: true,
});

app.register(mercuriusCache, {
  ttl: 2, // seconds
  all: true,
  storage: {
    type: "memory",
    options: {
      size: 2048,
    },
  },
});

Actual output

query hello 0
query hello 1

Expected output

query hello 0

I was able to debug the code and find the second location where the original resolver gets called:

  1. first call https://github.com/mercurius-js/cache/blob/v2.0.0/index.js#L184
  2. second call https://github.com/mercurius-js/cache/blob/v2.0.0/index.js#L257

I've also made a reproduction repository here: https://github.com/mfellner/mercurius-cache-bug

Dependencies:

  • fastify: 4.5.3
  • mercurius: 10.5.0
  • mercurius-cache: 2.0.0
  • Node.js: 18.8.0

This is an interesting bug. Have you got some ideas on how to fix this? I guess we'd need to do quite a bit of refactoring to cater for this case.

@simone-sanfratello wdyt?

Thanks for spotting @mfellner!

I can work on this in the next days, yeah probably some refactor is nice