Skip to content

Commit

Permalink
Add custom http client option to GraphQLClient (#122)
Browse files Browse the repository at this point in the history
Add custom http client option to GraphQLClient
  • Loading branch information
jainpiyush19 authored Feb 17, 2022
1 parent 13ff3d5 commit d501dfb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
7 changes: 7 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ func NewClient(opts ...ClientOpt) *GraphQLClient {
return c
}

// WithHTTPClient sets a custom HTTP client to be used when making downstream queries.
func WithHTTPClient(client *http.Client) ClientOpt {
return func(s *GraphQLClient) {
s.HTTPClient = client
}
}

// WithMaxResponseSize sets the max allowed response size. The client will only
// read up to maxResponseSize and that size is exceeded an an error will be
// returned.
Expand Down
29 changes: 29 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package bramble
import (
"context"
"net/http"
"net/http/cookiejar"
"net/http/httptest"
"net/url"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -34,6 +36,33 @@ func TestGraphqlClient(t *testing.T) {
assert.Equal(t, "value", res.Root.Test)
})

t.Run("with http client", func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("test_cookie")
require.NoError(t, err)
assert.Equal(t, "test_value", cookie.Value)
}))

jar, err := cookiejar.New(nil)
require.NoError(t, err)

serverURL, err := url.Parse(srv.URL)
require.NoError(t, err)

jar.SetCookies(serverURL, []*http.Cookie{
{

Name: "test_cookie",
Value: "test_value",
},
})

httpClient := &http.Client{Jar: jar}
c := NewClient(WithHTTPClient(httpClient))
var res interface{}
_ = c.Request(context.Background(), srv.URL, &Request{}, &res)
})

t.Run("with user agent", func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "My User Agent", r.Header.Get("User-Agent"))
Expand Down
9 changes: 8 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bramble
import (
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -38,6 +39,8 @@ type Config struct {
Plugins []PluginConfig
// Config extensions that can be shared among plugins
Extensions map[string]json.RawMessage
// HTTP client to customize for downstream services query
QueryHTTPClient *http.Client

plugins []Plugin
executableSchema *ExecutableSchema
Expand Down Expand Up @@ -264,7 +267,11 @@ func (c *Config) Init() error {
services = append(services, NewService(s))
}

queryClient := NewClient(WithMaxResponseSize(c.MaxServiceResponseSize), WithUserAgent(GenerateUserAgent("query")))
queryClientOptions := []ClientOpt{WithMaxResponseSize(c.MaxServiceResponseSize), WithUserAgent(GenerateUserAgent("query"))}
if c.QueryHTTPClient != nil {
queryClientOptions = append(queryClientOptions, WithHTTPClient(c.QueryHTTPClient))
}
queryClient := NewClient(queryClientOptions...)
es := newExecutableSchema(c.plugins, c.MaxRequestsPerQuery, queryClient, services...)
err = es.UpdateSchema(true)
if err != nil {
Expand Down

0 comments on commit d501dfb

Please sign in to comment.