Skip to content
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 ./...
51 changes: 46 additions & 5 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/url"

"github.com/shurcooL/graphql/internal/jsonutil"
"golang.org/x/net/context/ctxhttp"
)

// Client is a GraphQL client.
type Client struct {
url string // GraphQL server URL.
httpClient *http.Client
url string // GraphQL server URL.
httpClient *http.Client
queryString queryType
}

// NewClient creates a GraphQL client targeting the specified GraphQL server URL.
Expand All @@ -25,11 +27,24 @@ func NewClient(url string, httpClient *http.Client) *Client {
httpClient = http.DefaultClient
}
return &Client{
url: url,
httpClient: httpClient,
url: url,
httpClient: httpClient,
queryString: disabled,
}
}

// EnableQueryString enables query string mode for graphql queries
func (c *Client) EnableQueryString() *Client {
c.queryString = enabled
return c
}

// DisableQueryString disables query string mode for graphql queries
func (c *Client) DisableQueryString() *Client {
c.queryString = disabled
return c
}

// 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.
Expand Down Expand Up @@ -65,7 +80,14 @@ 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)

var resp *http.Response
if op == queryOperation && c.queryString == enabled {
resp, err = GetWithQueryString(ctx, c.httpClient, c.url, query, variables)
} else {
resp, err = ctxhttp.Post(ctx, c.httpClient, c.url, "application/json", &buf)
}

if err != nil {
return err
}
Expand Down Expand Up @@ -121,3 +143,22 @@ const (
mutationOperation
//subscriptionOperation // Unused.
)

// GetWithQueryString sends an http get request with the query and variables as a query string
func GetWithQueryString(ctx context.Context, client *http.Client, graphqlURL string, query string, variables map[string]interface{}) (*http.Response, error) {
queryString := url.QueryEscape(query)
variableBytes, err := json.Marshal(variables)
if err != nil {
return &http.Response{}, err
}
variableString := url.QueryEscape(string(variableBytes))
resp, err := ctxhttp.Get(ctx, client, graphqlURL+`?query=`+queryString+`&variables=`+variableString)
return resp, err
}

type queryType uint8

const (
disabled queryType = iota
enabled
)