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

Allow setting websocket connection parameters #360

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

HaraldNordgren
Copy link
Contributor

@HaraldNordgren HaraldNordgren commented Nov 14, 2024

We are having an issue with authorization when using subscriptions in genqlient.

Our mobile app team is using Apollo for GraphQL requests, and Apollo seems to sends its auth in a way that is different from how genqlient does it:

https://www.apollographql.com/docs/react/data/subscriptions#5-authenticate-over-websocket-optional

This is causing a problems, because we use genqlient for testing the backend.

It seems that the way Apollo is implemented, authentication (and other headers) are not actually sent as HTTP headers, but as connection parameters instead.

@HaraldNordgren
Copy link
Contributor Author

HaraldNordgren commented Nov 14, 2024

@HaraldNordgren HaraldNordgren force-pushed the connection_params branch 4 times, most recently from 089fdf8 to 37fe24f Compare November 14, 2024 17:26
//
// connectionParams is a map of connection parameters to be sent to the server
// during the initial connection handshake.
func NewClientUsingWebSocketWithConnectionParams(endpoint string, wsDialer Dialer, headers http.Header, connParams map[string]any) WebSocketClient {
Copy link
Contributor Author

@HaraldNordgren HaraldNordgren Nov 14, 2024

Choose a reason for hiding this comment

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

Since there was never a a release that includes NewClientUsingWebSocket, we should be free to update the signature if we want, and possible only have one function:

func NewClientUsingWebSocket(endpoint string, wsDialer Dialer, headers http.Header, connParams map[string]any) WebSocketClient

@HaraldNordgren
Copy link
Contributor Author

HaraldNordgren commented Nov 14, 2024

A big gotcha regarding HTTP headers and connection parameters, that I realized recently, is that websocket requests in 99desgins gqlgen enter a different path, going through here:

	gqlgenServer.AddTransport(transport.Websocket{
		InitFunc: func(ctx context.Context, initPayload transport.InitPayload) (context.Context, *transport.InitPayload, error) {
			// initPayload contains connection parameters
		},
	})

than the middleware path that would normally be expected:

	r.With(ah.VerifyAuthMiddleware).Method(http.MethodPost, "/v1", c.GraphServer)
	r.With(ah.VerifyAuthMiddleware).Method(http.MethodGet, "/v1", c.GraphServer)

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.

1 participant