Skip to content

Commit 99ba18c

Browse files
committed
add query string in an http get method for graphql query
1 parent d48a9a7 commit 99ba18c

File tree

11 files changed

+63
-22
lines changed

11 files changed

+63
-22
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ install:
1212
script:
1313
- go get -t -v ./...
1414
- diff -u <(echo -n) <(gofmt -d -s .)
15-
- go tool vet .
15+
- go vet .
1616
- go test -v -race ./...

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
graphql
22
=======
33

4-
[![Build Status](https://travis-ci.org/shurcooL/graphql.svg?branch=master)](https://travis-ci.org/shurcooL/graphql) [![GoDoc](https://godoc.org/github.com/shurcooL/graphql?status.svg)](https://godoc.org/github.com/shurcooL/graphql)
4+
[![Build Status](https://travis-ci.org/shurcooL/graphql.svg?branch=master)](https://travis-ci.org/shurcooL/graphql) [![GoDoc](https://godoc.org/github.com/danielnaveda/graphql?status.svg)](https://godoc.org/github.com/danielnaveda/graphql)
55

66
Package `graphql` provides a GraphQL client implementation.
77

@@ -15,7 +15,7 @@ Installation
1515
`graphql` requires Go version 1.8 or later.
1616

1717
```bash
18-
go get -u github.com/shurcooL/graphql
18+
go get -u github.com/danielnaveda/graphql
1919
```
2020

2121
Usage
@@ -283,9 +283,9 @@ Directories
283283
284284
| Path | Synopsis |
285285
|----------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------|
286-
| [example/graphqldev](https://godoc.org/github.com/shurcooL/graphql/example/graphqldev) | graphqldev is a test program currently being used for developing graphql package. |
287-
| [ident](https://godoc.org/github.com/shurcooL/graphql/ident) | Package ident provides functions for parsing and converting identifier names between various naming convention. |
288-
| [internal/jsonutil](https://godoc.org/github.com/shurcooL/graphql/internal/jsonutil) | Package jsonutil provides a function for decoding JSON into a GraphQL query data structure. |
286+
| [example/graphqldev](https://godoc.org/github.com/danielnaveda/graphql/example/graphqldev) | graphqldev is a test program currently being used for developing graphql package. |
287+
| [ident](https://godoc.org/github.com/danielnaveda/graphql/ident) | Package ident provides functions for parsing and converting identifier names between various naming convention. |
288+
| [internal/jsonutil](https://godoc.org/github.com/danielnaveda/graphql/internal/jsonutil) | Package jsonutil provides a function for decoding JSON into a GraphQL query data structure. |
289289
290290
License
291291
-------

doc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
// opportunities for improvement are discovered; it is not yet frozen.
99
//
1010
// For now, see README for more details.
11-
package graphql // import "github.com/shurcooL/graphql"
11+
package graphql // import "github.com/danielnaveda/graphql"

example/graphqldev/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
"net/http/httptest"
1515
"os"
1616

17+
"github.com/danielnaveda/graphql"
1718
graphqlserver "github.com/graph-gophers/graphql-go"
1819
"github.com/graph-gophers/graphql-go/example/starwars"
1920
"github.com/graph-gophers/graphql-go/relay"
20-
"github.com/shurcooL/graphql"
2121
)
2222

2323
func main() {

graphql.go

+47-6
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ import (
77
"fmt"
88
"io/ioutil"
99
"net/http"
10+
"net/url"
1011

11-
"github.com/shurcooL/graphql/internal/jsonutil"
12+
"github.com/danielnaveda/graphql/internal/jsonutil"
1213
"golang.org/x/net/context/ctxhttp"
1314
)
1415

1516
// Client is a GraphQL client.
1617
type Client struct {
17-
url string // GraphQL server URL.
18-
httpClient *http.Client
18+
url string // GraphQL server URL.
19+
httpClient *http.Client
20+
queryString queryType
1921
}
2022

2123
// NewClient creates a GraphQL client targeting the specified GraphQL server URL.
@@ -25,11 +27,24 @@ func NewClient(url string, httpClient *http.Client) *Client {
2527
httpClient = http.DefaultClient
2628
}
2729
return &Client{
28-
url: url,
29-
httpClient: httpClient,
30+
url: url,
31+
httpClient: httpClient,
32+
queryString: disabled,
3033
}
3134
}
3235

36+
// EnableQueryString enables query string mode for graphql queries
37+
func (c *Client) EnableQueryString() *Client {
38+
c.queryString = enabled
39+
return c
40+
}
41+
42+
// DisableQueryString disables query string mode for graphql queries
43+
func (c *Client) DisableQueryString() *Client {
44+
c.queryString = disabled
45+
return c
46+
}
47+
3348
// Query executes a single GraphQL query request,
3449
// with a query derived from q, populating the response into it.
3550
// q should be a pointer to struct that corresponds to the GraphQL schema.
@@ -65,7 +80,14 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab
6580
if err != nil {
6681
return err
6782
}
68-
resp, err := ctxhttp.Post(ctx, c.httpClient, c.url, "application/json", &buf)
83+
84+
var resp *http.Response
85+
if op == queryOperation && c.queryString == enabled {
86+
resp, err = GetWithQueryString(ctx, c.httpClient, c.url, query, variables)
87+
} else {
88+
resp, err = ctxhttp.Post(ctx, c.httpClient, c.url, "application/json", &buf)
89+
}
90+
6991
if err != nil {
7092
return err
7193
}
@@ -121,3 +143,22 @@ const (
121143
mutationOperation
122144
//subscriptionOperation // Unused.
123145
)
146+
147+
// GetWithQueryString sends an http get request with the query and variables as a query string
148+
func GetWithQueryString(ctx context.Context, client *http.Client, graphqlURL string, query string, variables map[string]interface{}) (*http.Response, error) {
149+
queryString := url.QueryEscape(query)
150+
variableBytes, err := json.Marshal(variables)
151+
if err != nil {
152+
return &http.Response{}, err
153+
}
154+
variableString := url.QueryEscape(string(variableBytes))
155+
resp, err := ctxhttp.Get(ctx, client, graphqlURL+`?query=`+queryString+`&variables=`+variableString)
156+
return resp, err
157+
}
158+
159+
type queryType uint8
160+
161+
const (
162+
disabled queryType = iota
163+
enabled
164+
)

graphql_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"net/http/httptest"
99
"testing"
1010

11-
"github.com/shurcooL/graphql"
11+
"github.com/danielnaveda/graphql"
1212
)
1313

1414
func TestClient_Query_partialDataWithErrorResponse(t *testing.T) {

ident/ident_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"reflect"
66
"testing"
77

8-
"github.com/shurcooL/graphql/ident"
8+
"github.com/danielnaveda/graphql/ident"
99
)
1010

1111
func Example_lowerCamelCaseToMixedCaps() {

internal/jsonutil/benchmark_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"testing"
99
"time"
1010

11-
"github.com/shurcooL/graphql"
12-
"github.com/shurcooL/graphql/internal/jsonutil"
11+
"github.com/danielnaveda/graphql"
12+
"github.com/danielnaveda/graphql/internal/jsonutil"
1313
)
1414

1515
func TestUnmarshalGraphQL_benchmark(t *testing.T) {

internal/jsonutil/graphql_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"testing"
66
"time"
77

8-
"github.com/shurcooL/graphql"
9-
"github.com/shurcooL/graphql/internal/jsonutil"
8+
"github.com/danielnaveda/graphql"
9+
"github.com/danielnaveda/graphql/internal/jsonutil"
1010
)
1111

1212
func TestUnmarshalGraphQL(t *testing.T) {

query.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"reflect"
88
"sort"
99

10-
"github.com/shurcooL/graphql/ident"
10+
"github.com/danielnaveda/graphql/ident"
1111
)
1212

1313
func constructQuery(v interface{}, variables map[string]interface{}) string {

scalar_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package graphql_test
33
import (
44
"testing"
55

6-
"github.com/shurcooL/graphql"
6+
"github.com/danielnaveda/graphql"
77
)
88

99
func TestNewScalars(t *testing.T) {

0 commit comments

Comments
 (0)