Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zeuscronos opened this issue Apr 25, 2024 · 4 comments

Comments

@zeuscronos
Copy link

zeuscronos commented Apr 25, 2024

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.:

  • the test executes the code inside the subscription callback
  • the test process finishes properly

In the other hand, the Yoga version has issues:

  • the test doesn't execute the code inside the subscription callback
  • the test process doesn't finish properly

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:

zeuscronos/yoga-issue@bcd0bd3#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

  • OS: Windows with Git Bash
  • NodeJS: v20.9.0
  • graphql-yoga: ^5.3.0
  • @graphql-yoga/apollo-link: ^3.3.0
@zeuscronos zeuscronos changed the title Jest testing process not finishing when subscribing to GraphQL Yoga server Test not executing code inside subscription callback when using GraphQL Yoga Apr 26, 2024
@tlg-265
Copy link

tlg-265 commented Apr 26, 2024

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

zeuscronos commented Apr 27, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants