Skip to content

Using subscriptions with non .NET clients or playgrounds #40

@rivantsov

Description

@rivantsov

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions