dotansimha / graphql-yoga

🧘 Rewrite of a fully-featured GraphQL Server with focus on easy setup, performance & great developer experience. The core of Yoga implements WHATWG Fetch API and can run/deploy on any JS environment.
https://the-guild.dev/graphql/yoga-server
MIT License
8.26k stars 574 forks source link

Test not executing code inside subscription callback when using GraphQL Yoga #3245

Open zeuscronos opened 7 months ago

zeuscronos commented 7 months ago

Describe the bug

I have a very simple dummy server with: Node.js + GraphQL + Apollo Server & Link that I'm migrating to Yoga Server & Link.

The Yoga version uses the same logic / idea as the Apollo version, just changed the necessary things.

You have the project repo here: https://github.com/zeuscronos/yoga-issue

On commit: "apollo server + test" you have the Apollo version while on commit: "yoga server + test" you have the Yoga version.

The Apollo version works as expected, i.e.:

In the other hand, the Yoga version has issues:

Additionally - Proposal

I would recommend the YogaLink to have an option to set an onConnected callback similarly to what it is on the line below. That may be needed in some scenarios where we need to wait for the WS connection to be stablished before doing other operations:

https://github.com/zeuscronos/yoga-issue/commit/bcd0bd38398a475707f92ad864e60a14425d5d9#diff-c7000107e5c2a5b190270c14f474e8538f6af25f052c8290bc0c3cddc2714c6aR86

Your Example Website or App

Apollo Server + Test

https://stackblitz.com/edit/github-hhdwqx-rk8lne?file=src%2Findex.js,src%2Ftest.js

All good with Apollo Server + Apollo Link...

image

Yoga Server + Test

https://stackblitz.com/edit/github-hhdwqx-owkeze?file=src%2Findex.js,src%2Ftest.js

Hanging when using Yoga Server + Yoga Link...

image

Steps to Reproduce the Bug or Issue

$ yarn
$ yarn start # terminal 1
$ yarn test  # terminal 2

Platform

tlg-265 commented 7 months ago

I'm also interested in an onConnect callback. If there's already a callback for this, or if you know of a workaround, please share it here.

zeuscronos commented 7 months ago

Adding the two lines below resolves the issues I mentioned on the description:

  1. The test doesn't execute the code within the subscription callback.
  2. The test process doesn't finish properly.

However, these approaches have drawbacks that make them unsuitable for my requirements.

None of these issues happened when using Apollo Server + Apollo Link.

image

zeuscronos commented 7 months ago

Additionally, I observed that if, instead of adding process.exit(0) at the end, a message is sent using mutation: sendMessage(...) from Yoga GraphiQL playground in the browser, then the test process completes. However, this isn't a viable option, as the process should finish without external intervention.

zeuscronos commented 7 months ago

Regarding the second issue (the test process doesn't finish properly) If I add the following to the end of the test:

import whyIsNodeRunning from 'why-is-node-running';
...
...
...
setTimeout(() => {
  console.log(whyIsNodeRunning());
}, 1000);

I get the following:

$ node src/test.js
All tests passed!
Test completed
There are 6 handle(s) keeping the process running

# TCPWRAP
C:\www\yoga-issue\node_modules\@whatwg-node\node-fetch\cjs\fetchNodeHttp.js:33 - const nodeRequest = requestFn(fetchRequ
est.url, {

# DNSCHANNEL
(unknown stack trace)

# HTTPCLIENTREQUEST
(unknown stack trace)

# TTYWRAP
C:\www\yoga-issue\node_modules\@whatwg-node\node-fetch\cjs\fetchNodeHttp.js:74 - responseBody = nodeResponse.pipe(new stream_1.PassThrough());

# TTYWRAP
C:\www\yoga-issue\node_modules\@whatwg-node\node-fetch\cjs\fetchNodeHttp.js:74 - responseBody = nodeResponse.pipe(new stream_1.PassThrough());

# Timeout
(unknown stack trace)
undefined