From 0418b9886132835f7d982b10cbcf6f34c8e275b0 Mon Sep 17 00:00:00 2001 From: Johanna Mantilla Date: Sun, 22 Jan 2023 22:21:15 -0500 Subject: [PATCH] Allow using custom HTTP clients (#68) Allow using custom HTTP clients (#68) --- graphql.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/graphql.go b/graphql.go index 0f1ed2d..ff378fe 100644 --- a/graphql.go +++ b/graphql.go @@ -14,6 +14,12 @@ 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) @@ -21,14 +27,14 @@ 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 }