diff --git a/graphql/client.go b/graphql/client.go index db7b8269..f938b25f 100644 --- a/graphql/client.go +++ b/graphql/client.go @@ -47,7 +47,7 @@ type Client interface { } type client struct { - httpClient *http.Client + httpClient Doer endpoint string method string } @@ -61,13 +61,20 @@ type client struct { // // The typical method of adding authentication headers is to wrap the client's // Transport to add those headers. See example/caller.go for an example. -func NewClient(endpoint string, httpClient *http.Client) Client { - if httpClient == nil { +func NewClient(endpoint string, httpClient Doer) Client { + if httpClient == nil || httpClient == (*http.Client)(nil) { httpClient = http.DefaultClient } return &client{httpClient, endpoint, http.MethodPost} } +// Doer encapsulates the methods from *http.Client needed by Client. +// The methods should have behavior to match that of *http.Client +// (or mocks for the same). +type Doer interface { + Do(*http.Request) (*http.Response, error) +} + type payload struct { Query string `json:"query"` Variables interface{} `json:"variables,omitempty"`