graphql icon indicating copy to clipboard operation
graphql copied to clipboard

`@nestjs/mercurius` graphql subscriptions not working on localhost

Open andreialecu opened this issue 3 years ago • 0 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current behavior

I recently experimented with migration from Apollo to Mercurius, and while I could get everything to work, I could not get graphql WebSocket subscriptions working at all.

The browser would instantly close all WebSocket connections to /graphql after they're open.

Here's a screenshot from Safari developer tools: image

After 10 hours of painful troubleshooting, nothing made sense. It appeared that connecting via wscat from the terminal was working correctly, and I was able to send and receive messages. But the browser would not connect.

However, reverting to Apollo would get everything working again.

I then discovered https://github.com/fastify/fastify/issues/4053, where it is mentioned that the WebSocket listener either starts on ::1 or 127.0.0.1. wscat was using the one that was working, while the browser used the other one.

After changing the app.listen call to await app.listen(process.env.PORT || 1338, '127.0.0.1'); I was finally able to get the browser to connect properly.

I'm leaving this write-up here in case it helps someone. We probably need to come up with a better solution about this either in fastify or nest.

Minimum reproduction code

...

Steps to reproduce

No response

Expected behavior

Explicit IP should not be required.

Package version

not important

Graphql version

graphql: apollo-server-express: apollo-server-fastify:

NestJS version

No response

Node.js version

No response

In which operating systems have you tested?

  • [ ] macOS
  • [ ] Windows
  • [ ] Linux

Other

This appears to be related to Fastify

andreialecu avatar Aug 27 '22 10:08 andreialecu