rivantsov / ngraphql

GraphQL .NET Server and Client
MIT License
43 stars 5 forks source link

Using subscriptions with non .NET clients or playgrounds #40

Open rivantsov opened 2 months ago

rivantsov commented 2 months ago

The latest release contains full implementation of Subscriptions, server and full support by GraphQLClient (Client in NGraphQL). Unit tests run successfully using this client. The question comes about other clients (js/ts in browser) or playground apps like Graphiql. I did not try it yet in playgrounds, but I will in the coming weeks and will report here, if it's doable and how. The situation is that GraphQL spec 'defines' subscriptions, but does not really specify any details of implementation of the actual protocol, so implementations may vary. Looks like the today the common agreed protocol is this:

https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md

and NGraphQL implementation is based on it, as close as possible. One important note - the Subscribe message, with subscription query inside, is sent over WebSocket connection, not through GraphQL usual Post request. So do not try to do POST with subscription query, like you do with Query or Mutation - that would not work. For accessing NGraphQL subscription from JS in the page or other client. Two steps:

  1. Establish WebSocket connection
  2. Use the protocol above to exchange json messages over this connection

For item 1, NGraphQL is using SignalR for websockets, so here are the instructions of how to connect to SignalR server from JS:

https://learn.microsoft.com/en-us/aspnet/core/signalr/javascript-client?view=aspnetcore-8.0&tabs=visual-studio

Edit: NGraphQL subscription endpoint for websockets: /graphql/subscriptions

After connecting, start sending/receiving messages according to the protocol. NGraphQL server supports all message including ConnectionInit, Ping, Subscribe, Complete etc. I am sure there is some ready to use JS/React component doing this, some GraphQL client. Let me know if something does not work.

In the meantime, I will try to play with playgrounds and see if they can make to work with NGraphQL server.

jasonlaw commented 4 weeks ago

May I know is there any update on this? I am currently using flutter as my front end, unfortunately, I am still yet to find a good flutter component for signalr. I have done the websocket before in the flutter end but not the signalr. It would be great if ngraphql can just use the native websocket. Anyway, I would like to see how the playground work with that then maybe I have better idea how to work with signalr in flutter. Thanks.

rivantsov commented 3 weeks ago

Hi, sorry for late response. I spent some time looking at this, and at signalR, websockets, etc, looked at actual traffic. And you know, you are right, SignalR is not a good choice for GraphQL subscriptions. All subscription and notification messages are wrapped into SignalR's 'envelope', and this makes it not compliant with protocol. Plus SignalR is really heavy. I am looking at plain websocket implementation, not sure how long it would take, hopefully a week or two (busy at work and other projects as well). but this is definitely one of my top priorities. Thank you for bringing it up, stay tuned, will try my best sincerely Roman