From a0522c054c60f5fb3c03698a702be6fc7e7f6554 Mon Sep 17 00:00:00 2001 From: Laisky Date: Wed, 11 Dec 2019 19:22:14 +0800 Subject: [PATCH 1/3] feat: add `WithHeader` & `WithCookie` --- go.mod | 9 +++++++++ go.sum | 20 +++++++++++++++++++ graphql.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++----- query.go | 2 +- 4 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..69c8059 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/shurcooL/graphql + +go 1.13 + +require ( + github.com/graph-gophers/graphql-go v0.0.0-20191115155744-f33e81362277 + github.com/stretchr/testify v1.4.0 // indirect + golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..88965ff --- /dev/null +++ b/go.sum @@ -0,0 +1,20 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/graph-gophers/graphql-go v0.0.0-20191115155744-f33e81362277 h1:E0whKxgp2ojts0FDgUA8dl62bmH0LxKanMoBr6MDTDM= +github.com/graph-gophers/graphql-go v0.0.0-20191115155744-f33e81362277/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc= +github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= +github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8= +golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/graphql.go b/graphql.go index 8520956..5de635e 100644 --- a/graphql.go +++ b/graphql.go @@ -9,25 +9,58 @@ import ( "net/http" "github.com/shurcooL/graphql/internal/jsonutil" - "golang.org/x/net/context/ctxhttp" ) +var ( + defaultClientHeaders = map[string]string{ + "Content-Type": "application/json", + } +) + +// ClientOptFunc graphql client option +type ClientOptFunc func(*Client) + +// WithHeader set graphql client header +func WithHeader(key, val string) ClientOptFunc { + return func(c *Client) { + c.headers[key] = val + } +} + +// WithCookie set graphql client cookie +func WithCookie(key, val string) ClientOptFunc { + return func(c *Client) { + if c.cookies == nil { + c.cookies = map[string]string{} + } + c.cookies[key] = val + } +} + // Client is a GraphQL client. type Client struct { url string // GraphQL server URL. httpClient *http.Client + + headers, + cookies map[string]string } // 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 *http.Client, opts ...ClientOptFunc) (c *Client) { if httpClient == nil { httpClient = http.DefaultClient } - return &Client{ + c = &Client{ + headers: defaultClientHeaders, url: url, httpClient: httpClient, } + for _, optf := range opts { + optf(c) + } + return c } // Query executes a single GraphQL query request, @@ -65,8 +98,21 @@ 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) - if err != nil { + var ( + req *http.Request + resp *http.Response + ) + // fmt.Println(buf.String()) + if req, err = http.NewRequest("POST", c.url, &buf); err != nil { + return err + } + for k, v := range c.headers { + req.Header.Set(k, v) + } + for k, v := range c.cookies { + req.AddCookie(&http.Cookie{Name: k, Value: v}) + } + if resp, err = c.httpClient.Do(req.WithContext(ctx)); err != nil { return err } defer resp.Body.Close() diff --git a/query.go b/query.go index e10b771..0d1b896 100644 --- a/query.go +++ b/query.go @@ -70,7 +70,7 @@ func writeArgumentType(w io.Writer, t reflect.Type, value bool) { default: // Named type. E.g., "Int". name := t.Name() - if name == "string" { // HACK: Workaround for https://github.com/shurcooL/githubv4/issues/12. + if name == "string" { // HACK: Workaround for https://github.com/shurcooL/githubv4/issues/12 name = "ID" } io.WriteString(w, name) From 7db37e4c693c976336fef784d6f554ceff651ee9 Mon Sep 17 00:00:00 2001 From: Laisky Date: Thu, 12 Dec 2019 09:06:50 +0800 Subject: [PATCH 2/3] ci: fix travis & gitginore --- .gitignore | 2 ++ .travis.yml | 2 +- go.mod | 1 - go.sum | 5 ----- graphql.go | 18 +++++++++++++----- query.go | 2 +- 6 files changed, 17 insertions(+), 13 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1f7ae75 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor +coverage.txt diff --git a/.travis.yml b/.travis.yml index 93b1fcd..12607ca 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 ./... diff --git a/go.mod b/go.mod index 69c8059..7deda91 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,4 @@ go 1.13 require ( github.com/graph-gophers/graphql-go v0.0.0-20191115155744-f33e81362277 github.com/stretchr/testify v1.4.0 // indirect - golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 ) diff --git a/go.sum b/go.sum index 88965ff..4c89b17 100644 --- a/go.sum +++ b/go.sum @@ -9,11 +9,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8= -golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= diff --git a/graphql.go b/graphql.go index 5de635e..8a1f056 100644 --- a/graphql.go +++ b/graphql.go @@ -11,11 +11,11 @@ import ( "github.com/shurcooL/graphql/internal/jsonutil" ) -var ( - defaultClientHeaders = map[string]string{ +func getDefaultClientHeaders() map[string]string { + return map[string]string{ "Content-Type": "application/json", } -) +} // ClientOptFunc graphql client option type ClientOptFunc func(*Client) @@ -48,18 +48,26 @@ type Client struct { // 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, opts ...ClientOptFunc) (c *Client) { +func NewClient(url string, httpClient *http.Client) (c *Client) { if httpClient == nil { httpClient = http.DefaultClient } c = &Client{ - headers: defaultClientHeaders, + headers: getDefaultClientHeaders(), url: url, httpClient: httpClient, } + + return c +} + +// NewClientWithOptions creates a GraphQL client, same as NewClient but with some options can used to set HTTP Header +func NewClientWithOptions(url string, httpClient *http.Client, opts ...ClientOptFunc) (c *Client) { + c = NewClient(url, httpClient) for _, optf := range opts { optf(c) } + return c } diff --git a/query.go b/query.go index 0d1b896..e10b771 100644 --- a/query.go +++ b/query.go @@ -70,7 +70,7 @@ func writeArgumentType(w io.Writer, t reflect.Type, value bool) { default: // Named type. E.g., "Int". name := t.Name() - if name == "string" { // HACK: Workaround for https://github.com/shurcooL/githubv4/issues/12 + if name == "string" { // HACK: Workaround for https://github.com/shurcooL/githubv4/issues/12. name = "ID" } io.WriteString(w, name) From 52e98ccf6b2bdf81da8073ecc3e0f3e93e38e7d3 Mon Sep 17 00:00:00 2001 From: "Laisky.Cai" Date: Tue, 12 Jan 2021 15:47:26 +0800 Subject: [PATCH 3/3] Update graphql.go Co-authored-by: Steve Coffman --- graphql.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/graphql.go b/graphql.go index 8a1f056..df2d9e5 100644 --- a/graphql.go +++ b/graphql.go @@ -48,7 +48,8 @@ type Client struct { // 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) (c *Client) { +func NewClient(url string, httpClient *http.Client) *Client { + var c *Client if httpClient == nil { httpClient = http.DefaultClient }