mercurius-js / mercurius

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

GraphQL subscription does not work reliably unless listening on 0.0.0.0 #875

Closed andreialecu closed 2 years ago

andreialecu commented 2 years ago

I've been migrating a NestJS app from Apollo to Mercurius and found that the GraphQL WebSocket endpoint was not working. No connection could be established.

Upon some further inspection, I found that I needed to listen specifically on an explicit IP.

I was able to get things to work initially by specifying 127.0.0.1 as the bound IP address. However, once deployed via docker, this wouldn't work properly.

Instead, I need to specify 0.0.0.0 as mentioned in this comment: https://github.com/mercurius-js/mercurius/issues/838#issuecomment-1212655506

I wonder why this is necessary. Is it a bug? If not, I haven't seen it documented anywhere.

Related: https://github.com/mercurius-js/mercurius/issues/838 https://github.com/nestjs/nest/issues/9903#issuecomment-1229169259 https://github.com/nestjs/graphql/issues/2365

mcollina commented 2 years ago

It's documented here: https://www.fastify.io/docs/latest/Reference/Server/#listen.

Note that we do not have the resources to support Nest.js users. Please refer to the Nest Discord channel (support) for such questions.

andreialecu commented 2 years ago

@mcollina I see, thanks. I missed that doc page. However the default seems to imply that by default it uses localhost which listens on both ipv4 and ipv6, but that doesn't seem to be the case for websocket connections.

For regular HTTP connections, the documentation seems correct. But the websocket server does NOT seem to start on my computer on ipv4/127.0.0.1 unless I explicitly specify either 0.0.0.0 or 127.0.0.1. The documentation implies both ipv4 and ipv6 should start by default.

I'd like to reiterate that this seems to be websocket specific.

andreialecu commented 2 years ago

In particular, Chrome is not able to establish a connection when developing locally unless I either use :: or 0.0.0.0 or 127.0.0.1.

Screenshot 2022-09-27 at 13 32 16
mcollina commented 2 years ago

Are you using the latest version of Fastify? This looks like te problem we fixed in https://github.com/fastify/fastify/pull/4190.

I'll be happy to look into this given a simple repro, ideally just with Fastify and Fastify-websockets

mcollina commented 2 years ago

This looks like some sort of DNS or networking issue on your machine. What does localhost resolves to?

andreialecu commented 2 years ago
➜ yarn why fastify             
├─ @nestjs/platform-fastify@npm:9.0.11
│  └─ fastify@npm:4.4.0 (via npm:4.4.0)
│
├─ @nestjs/platform-fastify@npm:9.0.11 [fc2e5]
│  └─ fastify@npm:4.4.0 (via npm:4.4.0)
│
└─ ...actual-project...
   └─ fastify@npm:4.5.3 (via npm:^4.5.3)

Oh, thank you. Indeed the version of @nestjs/platform-fastify I was using had a previous version of fastify pinned.

➜ yarn why fastify                
├─ @nestjs/platform-fastify@npm:9.1.2
│  └─ fastify@npm:4.6.0 (via npm:4.6.0)
│
├─ @nestjs/platform-fastify@npm:9.1.2 [fc2e5]
│  └─ fastify@npm:4.6.0 (via npm:4.6.0)
│
└─ ...actual-project...
   └─ fastify@npm:4.6.0 (via npm:^4.5.3)

After bumping everything, it's all good. Sorry for taking your time, and thanks for pointing me in the right direction!