Skip to content

Commit

Permalink
Allow using custom HTTP clients (shurcooL#68)
Browse files Browse the repository at this point in the history
Allow using custom HTTP clients (shurcooL#68)
  • Loading branch information
senekis authored Jan 23, 2023
1 parent 1265577 commit 0418b98
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,27 @@ import (
"github.com/hasura/go-graphql-client/pkg/jsonutil"
)

// Doer interface has the method required to use a type as custom http client.
// The net/*http.Client type satisfies this interface.
type Doer interface {
Do(*http.Request) (*http.Response, error)
}

// This function allows you to tweak the HTTP request. It might be useful to set authentication
// headers amongst other things
type RequestModifier func(*http.Request)

// Client is a GraphQL client.
type Client struct {
url string // GraphQL server URL.
httpClient *http.Client
httpClient Doer
requestModifier RequestModifier
debug bool
}

// NewClient creates a GraphQL client targeting the specified GraphQL server URL.
// If httpClient is nil, then http.DefaultClient is used.
func NewClient(url string, httpClient *http.Client) *Client {
func NewClient(url string, httpClient Doer) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}
Expand Down

0 comments on commit 0418b98

Please sign in to comment.