Skip to content

Commit

Permalink
support custom http client for subscription client (shurcooL#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
sermojohn authored Feb 21, 2022
1 parent dedae85 commit 965b8db
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -87,6 +88,7 @@ type SubscriptionClient struct {
url string
conn WebsocketConn
connectionParams map[string]interface{}
websocketOptions WebsocketOptions
context context.Context
subscriptions map[string]*subscription
cancel context.CancelFunc
Expand Down Expand Up @@ -138,6 +140,12 @@ func (sc *SubscriptionClient) WithWebSocket(fn func(sc *SubscriptionClient) (Web
return sc
}

// WithWebSocketOptions provides options to the websocket client
func (sc *SubscriptionClient) WithWebSocketOptions(options WebsocketOptions) *SubscriptionClient {
sc.websocketOptions = options
return sc
}

// WithConnectionParams updates connection params for sending to server through GQL_CONNECTION_INIT event
// It's usually used for authentication handshake
func (sc *SubscriptionClient) WithConnectionParams(params map[string]interface{}) *SubscriptionClient {
Expand Down Expand Up @@ -602,7 +610,9 @@ func newWebsocketConn(sc *SubscriptionClient) (WebsocketConn, error) {

options := &websocket.DialOptions{
Subprotocols: []string{"graphql-ws"},
HTTPClient: sc.websocketOptions.HTTPClient,
}

c, _, err := websocket.Dial(sc.GetContext(), sc.GetURL(), options)
if err != nil {
return nil, err
Expand All @@ -614,3 +624,9 @@ func newWebsocketConn(sc *SubscriptionClient) (WebsocketConn, error) {
timeout: sc.GetTimeout(),
}, nil
}

// WebsocketOptions allows implementation agnostic configuration of the websocket client
type WebsocketOptions struct {
// HTTPClient is used for the connection.
HTTPClient *http.Client
}

0 comments on commit 965b8db

Please sign in to comment.