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

on error ReferenceError: WebSocket is not defined #11761

Open
arenevier opened this issue Apr 5, 2024 · 5 comments
Open

on error ReferenceError: WebSocket is not defined #11761

arenevier opened this issue Apr 5, 2024 · 5 comments

Comments

@arenevier
Copy link

arenevier commented Apr 5, 2024

Issue Description

I am using apollo client with GraphQLWsLink in node.

When an error happens, an exception happens during the handling of that error

Trace is

on error  ReferenceError: WebSocket is not defined
    at isLikeErrorEvent (/home/arno/project/node_modules/@apollo/client/link/subscriptions/index.js:42:111)
    at Object.error (/home/arno/project/node_modules/@apollo/client/link/subscriptions/index.js:62:38)
    at Object.<anonymous> (/home/arno/project/node_modules/graphql-ws/lib/client.js:396:38)
    at emit (/home/arno/project/node_modules/graphql-ws/lib/client.js:106:94)
    at Object.emit (/home/arno/project/node_modules/graphql-ws/lib/client.js:133:21)
    at WebSocketImpl.socket.onmessage (/home/arno/project/node_modules/graphql-ws/lib/client.js:232:29)
    at callListener (/home/arno/project/node_modules/ws/lib/event-target.js:290:14)
    at WebSocketImpl.onMessage (/home/arno/project/node_modules/ws/lib/event-target.js:209:9)
    at WebSocketImpl.emit (node:events:519:28)
    at WebSocketImpl.emit (node:domain:488:12)

This happens because of that line. WebSocket is not defined in node environment.

return isNonNullObject(err) && err.target?.readyState === WebSocket.CLOSED;

Link to Reproduction

don't have one, but the root of the issue is obvious

Reproduction Steps

No response

@apollo/client version

3.9.10

@alessbell
Copy link
Member

Hi @arenevier 👋 Appreciate the bug report! What does the error event you're receiving look like? Are you using ws? A reproduction would be helpful to check our assumptions. We'd also love any PRs here :)

@arenevier
Copy link
Author

any type of error will create that. For example:

[                                                                                                                       
  {                                                                                                                                      
    message: 'Cannot query field "eid" on type "IFooBar". Did you mean "_eid"?',                                       
    locations: [ [Object] ]                                                                                                              
  }                                                                                                                                      
]

Yes, I'm using ws,

    const wsLink = new GraphQLWsLink(createClient({
     url: 'wss://my-url/graphql',
      webSocketImpl: WebSocket,
      connectionParams: {
           ...
      }
    }));
  

@alessbell
Copy link
Member

Thanks, @arenevier. You should also set that WebSocket implementation you're passing to webSocketImpl on globalThis in your node environment which I would expect to resolve the error. Can you please try that and let me know how it goes?

@arenevier
Copy link
Author

Yes, that would work.

It looks like I am not the first to bump into this issue: #11063

Should that be specified in the documentation?

Alternatively, replacing WebSocket.CLOSED by it's value (3) would also fix the issue for everyone.

@alessbell
Copy link
Member

Yes, that should definitely be added to the docs :) I'll look at opening that PR today.

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

No branches or pull requests

2 participants