question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow web-ui users to configure the `payload` on `connection_init` messages when subscribing over websockets

See original GitHub issue

We are implementing a graphql server where clients can subscribe (using graphql subscriptions) and have to authenticate by sending an auth token in the payload of the initial connection_init message. So – for example – an initial message could look like this:

{
  "type": "connection_init",
  "payload": {
    "Authorization": "Bearer <some-jwt>"
  }
}

Is there a way in the graphiql web frontend to configure the payload that is being sent in the connection_init message? We couldn’t find a way and graphiql is broken for us.

For context: We’re using https://github.com/99designs/gqlgen for implementing the backend. gqlgen switched to using graphiql here: https://github.com/99designs/gqlgen/pull/1751. Before that it used https://github.com/graphql/graphql-playground. As far as I can tell, that project behaved like this: If users entered headers in the web frontend, it would:

  • Put those headers into the http request headers for queries and mutations. These requests would not use websockets.
  • For subscriptions, it would open a websocket, for the initial http update request, it would not include the custom headers, but then it would put the custom headers into the payload of the connection_init request.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
soenkehahncommented, Feb 19, 2022

@acao: Thanks for the replies. As I understand it, the spec does specify an optional payload field. And graphql-ws also supports that. I did some digging and graphiql also already allows to specify the payload in javascript, for example like this:

GraphiQL.createFetcher({
  url,
  subscriptionUrl,
  wsConnectionParams: { Authorization: 'Bearer <token>' }
});

wsConnectionParams is declared here and passed through to graphql-ws as the connectionParams argument that @enisdenjo mentioned.

This issue is about allowing users of the graphiql web interface to configure the payload without having to write javascript. (I’ll change the title of this issue to make it a bit more explicit.)

As I mentioned above https://github.com/graphql/graphql-playground just passes whatever users put into the ‘headers’ as a json object into that payload. Which I find a bit weird, since it kind of conflates http headers and this payload field. But that would work for us. Any other way to configure the payload would also work, though.

If I find the time, would you be willing to review a PR that added this? Pending a decision on how this should be supported in graphiql.

0reactions
acaocommented, Aug 28, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

GraphQL over WebSocket by enisdenjo · Pull Request #140
Allow web-ui users to configure the payload on connection_init messages when subscribing over websockets graphql/graphiql#2181.
Read more >
Part 1 - Send & receive - websockets 10.4 documentation
When it works, you can play the game from a single browser, with players taking alternate turns. Enable debug logs to see all...
Read more >
Building a real-time WebSocket client - AWS AppSync
The client unregisters the subscription by sending a stop subscription message. After unregistering all subscriptions and checking that there are no messages ......
Read more >
Set up WebSocket communication using Node-RED between ...
Learn how to set up a WebSocket server using Node-RED to enable communication between a web interface and a Jupyter Notebook on IBM...
Read more >
WebSocket Communication Patterns for Real-Time Web Apps
Only suitable for applications with smaller payloads per message as ... Sends updates in real-time to all subscribed users and efficient for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found