hasura / go-graphql-client

Package graphql provides a GraphQL client implementation.
MIT License
395 stars 91 forks source link

feat: add more WebSocket options for subscription client #126

Closed hgiasac closed 7 months ago

hgiasac commented 7 months ago

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"},
        },
    })
github-actions[bot] commented 7 months ago

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%