mercurius-js / mercurius

Implement GraphQL servers and gateways with Fastify
https://mercurius.dev/
MIT License
2.34k stars 234 forks source link

request.raw.on('close') is triggered not when request really ends if using `/graphql` #923

Closed igrlk closed 1 year ago

igrlk commented 1 year ago

Hey 👋 Looks like I found a bug:

minimal reproducible example

I have default fastify + mercurius setup from the quick start docs:

... imports, schema, resolvers

app.register(mercurius, {
  schema,
  resolvers,
})

This, by default, creates POST /graphql endpoint which handles the graphql requests.

Additionally, I've added another endpoint that handles graphql:

app.post('/', async function (req, reply) {
  const query = '{ hello }'
  return reply.graphql(query)
})

I have only one resolver that has this code:

    hello: async (_, __, ctx) => {
      ctx.reply.request.raw.on('close', () => {
        console.log('closed');
      });

      console.log('1');
      await sleep(0.5);
      console.log('2');
      await sleep(0.5);
      console.log('3');

      return 'World';
    }

That's expected that my 2 endpoints should work the same, but I get different outputs:

reply.request.raw.on('close') is being triggered before the request is really finished with default mercurius setup

Curious if it's a known issue, expected behaviour, or perhaps you know what can be the issue? I attached the minimal reproducible repo Didn't have a lot of time to debug it yet so let me know if you have any thoughts

mcollina commented 1 year ago

According to https://nodejs.org/api/http.html#event-close_3:

The close event is now emitted when the request has been completed and not when the underlying socket is closed.

This is entirely ok.