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

feat: add more WebSocket options for subscription client #126

Merged
merged 4 commits into from
Feb 3, 2024

Conversation

hgiasac
Copy link

@hgiasac hgiasac commented Feb 3, 2024

Add more WebSocket options that are supported natively by the nhooyr.io/websocket client such as HTTP header and compression.

// WebsocketOptions allows implementation agnostic configuration of the websocket client
type WebsocketOptions struct {
	// HTTPClient is used for the connection.
	// Its Transport must return writable bodies for WebSocket handshakes.
	// http.Transport does beginning with Go 1.12.
	HTTPClient *http.Client

	// HTTPHeader specifies the HTTP headers included in the handshake request.
	HTTPHeader http.Header

	// Host optionally overrides the Host HTTP header to send. If empty, the value
	// of URL.Host will be used.
	Host string

	// CompressionMode controls the compression mode.
	// Defaults to CompressionDisabled.
	//
	// See docs on CompressionMode for details.
	CompressionMode websocket.CompressionMode

	// CompressionThreshold controls the minimum size of a message before compression is applied.
	//
	// Defaults to 512 bytes for CompressionNoContextTakeover and 128 bytes
	// for CompressionContextTakeover.
	CompressionThreshold int
}

It's useful for GraphQL servers that validate custom auth tokens on the header.

client := graphql.NewSubscriptionClient(serverEndpoint).
    WithWebSocketOptions(graphql.WebsocketOptions{
        HTTPHeader: http.Header{
            "Authorization": []string{"Bearer random-secret"},
        },
    })

@hgiasac hgiasac marked this pull request as ready for review February 3, 2024 04:44
Copy link

github-actions bot commented Feb 3, 2024

Code Coverage

Package Line Rate Health
github.com/hasura/go-graphql-client 71%
github.com/hasura/go-graphql-client/ident 100%
github.com/hasura/go-graphql-client/pkg/jsonutil 84%
Summary 75% (1414 / 1883)

Minimum allowed line rate is 60%

@hgiasac hgiasac merged commit 8f1d49e into master Feb 3, 2024
1 check passed
@hgiasac hgiasac deleted the graphql-ws-backward-example branch February 3, 2024 05:32
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.

None yet

1 participant