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

Introduce GraphQLWebSocketClient #330

Merged
merged 9 commits into from
Apr 23, 2022
Merged

Introduce GraphQLWebSocketClient #330

merged 9 commits into from
Apr 23, 2022

Conversation

lyubo
Copy link
Contributor

@lyubo lyubo commented Feb 22, 2022

Partially resolves #77

This implementation uses WebSocket communication channel between the server and the client.
Supported protocol is graphql-transport-ws
All operations (queries, mutations, subscriptions) are supported.

This implementation itself adds no new dependencies. Added development dependencies (ws, @types/ws, graphql-ws) are used to setup a test server.

@Jaikant
Copy link

Jaikant commented Mar 8, 2022

Any plans to merge?

@jasonkuhrt
Copy link
Owner

Need to look

@adnankurt16
Copy link

is there any progress? when is it merged? @jasonkuhrt

src/graphql-ws.ts Show resolved Hide resolved
src/graphql-ws.ts Show resolved Hide resolved
src/graphql-ws.ts Show resolved Hide resolved
@lyubo
Copy link
Contributor Author

lyubo commented Apr 13, 2022

@jasonkuhrt What changes do I have to make? It's not clear to me. Should I find a dependency which declares such data structure?

@jasonkuhrt
Copy link
Owner

I find it highly surprising that we have to invent this much code inline to get client side GraphQL websockets working.

I see two approaches here:

  1. Take the time to add some research about the current landscape
  2. Roll with this PR and let future PRs refine if needed

I'm inclined toward (2) because I do not have time to actively develop on this project right now. Currently I just help the community iterate.

Copy link
Owner

@jasonkuhrt jasonkuhrt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give a few more days for community feedback and then merge this otherwise.

@jasonkuhrt
Copy link
Owner

Ok it has been a few days. Let's give it a try. Super open to hearing feedback and receiving more iteration around this.

@jasonkuhrt jasonkuhrt merged commit be8f368 into jasonkuhrt:master Apr 23, 2022
@ehynds
Copy link

ehynds commented Apr 25, 2022

Super excited to see this! What's the best way to try this out? Can you release under the next tag, @jasonkuhrt?

@jasonkuhrt
Copy link
Owner

Will do, looks like CI needs some fixing, something it depends on changed.

@adnankurt16
Copy link

@lyubo i am getting an error, you can send any sample?

TypeError: this.socket.send is not a function
    at GraphQLWebSocketClient.makeSubscribe (graphql-ws.js?50e4:208:1)
    at GraphQLWebSocketClient.rawSubscribe (graphql-ws.js?50e4:241:1)

@lyubo
Copy link
Contributor Author

lyubo commented Apr 27, 2022

@adnankurt16

i am getting an error, you can send any sample?

TypeError: this.socket.send is not a function
    at GraphQLWebSocketClient.makeSubscribe (graphql-ws.js?50e4:208:1)
    at GraphQLWebSocketClient.rawSubscribe (graphql-ws.js?50e4:241:1)

You can look at test code: tests/graphql-ws.test.ts

Basically, you should do the following:

  socket = new WebSocket(url, GRAPHQL_TRANSPORT_WS_PROTOCOL);
  client = new GraphQLWebSocketClient(socket);

If you want to know when the socket gets acknowledged (according to the graphql-trasport-ws protocol) you can supply a callback function:

   async function onAcknowledged(payload: any): Promise<void> {
      ...
   }

  client = new GraphQLWebSocketClient(socket), { onAcknowledged })

The WebSocket implementation I use in my code is reconnecting-websocket

@artalar
Copy link

artalar commented Aug 14, 2022

Any plans to document it? 👀

@rotembm12
Copy link

is there a doc that shows how to subscribe to GQL events?

@SimenB
Copy link
Contributor

SimenB commented Sep 4, 2022

I'm getting a TS error after upgrading to 5.0.0.

../../node_modules/graphql-request/dist/graphql-ws.d.ts:19:25 - error TS2304: Cannot find name 'WebSocket'.

19     constructor(socket: WebSocket, { onInit, onAcknowledged, onPing, onPong }: SocketHandler);

I tried adding a dependency on @types/ws, but that didn't help

@Qix-
Copy link

Qix- commented Sep 7, 2022

Running into the same issue, this broke us. Node does not have WebSocket available in the global scope like the DOM does. Adding "dom" to the libs list in tsconfig "solves" this but is not correct.

@EmirBoyaci
Copy link

I'm getting a TS error after upgrading to 5.0.0.

../../node_modules/graphql-request/dist/graphql-ws.d.ts:19:25 - error TS2304: Cannot find name 'WebSocket'.

19     constructor(socket: WebSocket, { onInit, onAcknowledged, onPing, onPong }: SocketHandler);

I tried adding a dependency on @types/ws, but that didn't help

I am getting exact same error, any news here?

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

Successfully merging this pull request may close these issues.

Subscriptions Support
10 participants