mercurius-js / mercurius

Implement GraphQL servers and gateways with Fastify

Home Page:https://mercurius.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

igrlk opened this issue Β· comments

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:

  • In case of POST /graphql, console outputs the following: 1, closed, 2, 3
  • Whereas in case of POST /, console outputs the following: 1, 2, 3, closed

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

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.