Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variadic RequestOption to Query and Mutate #47

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ install:
script:
- go get -t -v ./...
- diff -u <(echo -n) <(gofmt -d -s .)
- go tool vet .
- go vet .
- go test -v -race ./...
37 changes: 31 additions & 6 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,34 @@ func NewClient(url string, httpClient *http.Client) *Client {
}
}

// RequestOption is a variadic option for modifying an underlying HTTP request
// for GraphQL.
type RequestOption func(req *http.Request) error

// WithRequestHeader sets an explicit HTTP header for usage
func WithRequestHeader(key, value string) RequestOption {
return func(req *http.Request) error {
req.Header.Set(key, value)
return nil
}
}

// Query executes a single GraphQL query request,
// with a query derived from q, populating the response into it.
// q should be a pointer to struct that corresponds to the GraphQL schema.
func (c *Client) Query(ctx context.Context, q interface{}, variables map[string]interface{}) error {
return c.do(ctx, queryOperation, q, variables)
func (c *Client) Query(ctx context.Context, q interface{}, variables map[string]interface{}, opts ...RequestOption) error {
return c.do(ctx, queryOperation, q, variables, opts...)
}

// Mutate executes a single GraphQL mutation request,
// with a mutation derived from m, populating the response into it.
// m should be a pointer to struct that corresponds to the GraphQL schema.
func (c *Client) Mutate(ctx context.Context, m interface{}, variables map[string]interface{}) error {
return c.do(ctx, mutationOperation, m, variables)
func (c *Client) Mutate(ctx context.Context, m interface{}, variables map[string]interface{}, opts ...RequestOption) error {
return c.do(ctx, mutationOperation, m, variables, opts...)
}

// do executes a single GraphQL operation.
func (c *Client) do(ctx context.Context, op operationType, v interface{}, variables map[string]interface{}) error {
func (c *Client) do(ctx context.Context, op operationType, v interface{}, variables map[string]interface{}, opts ...RequestOption) error {
var query string
switch op {
case queryOperation:
Expand All @@ -65,7 +77,20 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab
if err != nil {
return err
}
resp, err := ctxhttp.Post(ctx, c.httpClient, c.url, "application/json", &buf)
req, err := http.NewRequest(http.MethodPost, c.url, &buf)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")

for _, opt := range opts {
err := opt(req)
if err != nil {
return err
}
}

resp, err := ctxhttp.Do(ctx, c.httpClient, req)
if err != nil {
return err
}
Expand Down
24 changes: 24 additions & 0 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,30 @@ func TestClient_Query_emptyVariables(t *testing.T) {
}
}

func TestClient_Query_requestOptions(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/graphql", func(w http.ResponseWriter, req *http.Request) {
header := req.Header.Get("Custom-Request-Header")
if got, want := header, "test-custom-header"; got != want {
t.Errorf("got header: %v, want %v", got, want)
}

w.Header().Set("Content-Type", "application/json")
mustWrite(w, `{"data": {"user": {"name": "Gopher"}}}`)
})
client := graphql.NewClient("/graphql", &http.Client{Transport: localRoundTripper{handler: mux}})

var q struct {
User struct {
Name string
}
}
err := client.Query(context.Background(), &q, map[string]interface{}{}, graphql.WithRequestHeader("Custom-Request-Header", "test-custom-header"))
if err != nil {
t.Fatal(err)
}
}

// localRoundTripper is an http.RoundTripper that executes HTTP transactions
// by using handler directly, instead of going over an HTTP connection.
type localRoundTripper struct {
Expand Down