Skip to content

Commit 73d4805

Browse files
authored
Merge pull request #10 from cli/update-from-origin
Bring in latest changes from shurcool/graphql
2 parents af8dbfa + e696888 commit 73d4805

File tree

14 files changed

+102
-109
lines changed

14 files changed

+102
-109
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
- name: Set up Go
1212
uses: actions/setup-go@v4
1313
with:
14-
go-version: '1.16'
14+
go-version: '1.21'
1515

1616
- name: Lint
1717
uses: golangci/golangci-lint-action@v3

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
fail-fast: false
77
matrix:
88
os: [ubuntu-latest, windows-latest, macos-latest]
9-
go: [1.16]
9+
go: [1.21]
1010
runs-on: ${{ matrix.os }}
1111

1212
steps:

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ Package `graphql` provides a GraphQL client implementation, and is forked from `
66
Installation
77
------------
88

9-
`graphql` requires Go version 1.16 or later.
10-
119
```bash
1210
go get -u github.com/cli/shurcooL-graphql
1311
```
@@ -130,7 +128,7 @@ var q struct {
130128
Then, define a `variables` map with their values:
131129
132130
```Go
133-
variables := map[string]interface{}{
131+
variables := map[string]any{
134132
"id": graphql.ID(id),
135133
"unit": starwars.LengthUnit("METER"),
136134
}
@@ -250,7 +248,7 @@ var m struct {
250248
Commentary graphql.String
251249
} `graphql:"createReview(episode: $ep, review: $review)"`
252250
}
253-
variables := map[string]interface{}{
251+
variables := map[string]any{
254252
"ep": starwars.Episode("JEDI"),
255253
"review": starwars.ReviewInput{
256254
Stars: graphql.Int(5),

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
module github.com/cli/shurcooL-graphql
22

3-
go 1.16
4-
5-
require golang.org/x/net v0.15.0
3+
go 1.21

go.sum

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +0,0 @@
1-
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
2-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
3-
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
4-
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
5-
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
6-
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
7-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
8-
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
9-
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
10-
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
11-
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
12-
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
13-
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
14-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
15-
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
16-
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
17-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
18-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
19-
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
20-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
21-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
22-
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
23-
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
24-
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
25-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
26-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
27-
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
28-
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
29-
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
30-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
31-
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
32-
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
33-
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
34-
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
35-
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
36-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
37-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
38-
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
39-
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
40-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

graphql.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
"io/ioutil"
8+
"io"
99
"net/http"
1010
"strings"
1111

1212
"github.com/cli/shurcooL-graphql/internal/jsonutil"
13-
"golang.org/x/net/context/ctxhttp"
1413
)
1514

1615
// Client is a GraphQL client.
1716
type Client struct {
18-
url string // GraphQL server URL.
19-
httpClient *http.Client
17+
url string // GraphQL server URL.
18+
httpClient *http.Client // Non-nil.
2019
}
2120

2221
// NewClient creates a GraphQL client targeting the specified GraphQL server URL.
@@ -34,29 +33,29 @@ func NewClient(url string, httpClient *http.Client) *Client {
3433
// Query executes a single GraphQL query request,
3534
// with a query derived from q, populating the response into it.
3635
// Argument q should be a pointer to struct that corresponds to the GraphQL schema.
37-
func (c *Client) Query(ctx context.Context, q interface{}, variables map[string]interface{}) error {
36+
func (c *Client) Query(ctx context.Context, q any, variables map[string]any) error {
3837
return c.do(ctx, queryOperation, q, variables, "")
3938
}
4039

4140
// QueryNamed is the same as Query but allows a name to be specified for the query.
42-
func (c *Client) QueryNamed(ctx context.Context, queryName string, q interface{}, variables map[string]interface{}) error {
41+
func (c *Client) QueryNamed(ctx context.Context, queryName string, q any, variables map[string]any) error {
4342
return c.do(ctx, queryOperation, q, variables, queryName)
4443
}
4544

4645
// Mutate executes a single GraphQL mutation request,
4746
// with a mutation derived from m, populating the response into it.
4847
// Argument m should be a pointer to struct that corresponds to the GraphQL schema.
49-
func (c *Client) Mutate(ctx context.Context, m interface{}, variables map[string]interface{}) error {
48+
func (c *Client) Mutate(ctx context.Context, m any, variables map[string]any) error {
5049
return c.do(ctx, mutationOperation, m, variables, "")
5150
}
5251

5352
// MutateNamed is the same as Mutate but allows a name to be specified for the mutation.
54-
func (c *Client) MutateNamed(ctx context.Context, queryName string, m interface{}, variables map[string]interface{}) error {
53+
func (c *Client) MutateNamed(ctx context.Context, queryName string, m any, variables map[string]any) error {
5554
return c.do(ctx, mutationOperation, m, variables, queryName)
5655
}
5756

5857
// do executes a single GraphQL operation.
59-
func (c *Client) do(ctx context.Context, op operationType, v interface{}, variables map[string]interface{}, queryName string) error {
58+
func (c *Client) do(ctx context.Context, op operationType, v any, variables map[string]any, queryName string) error {
6059
var query string
6160
switch op {
6261
case queryOperation:
@@ -65,8 +64,8 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab
6564
query = constructMutation(v, variables, queryName)
6665
}
6766
in := struct {
68-
Query string `json:"query"`
69-
Variables map[string]interface{} `json:"variables,omitempty"`
67+
Query string `json:"query"`
68+
Variables map[string]any `json:"variables,omitempty"`
7069
}{
7170
Query: query,
7271
Variables: variables,
@@ -76,19 +75,24 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab
7675
if err != nil {
7776
return err
7877
}
79-
resp, err := ctxhttp.Post(ctx, c.httpClient, c.url, "application/json", &buf)
78+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.url, &buf)
79+
if err != nil {
80+
return err
81+
}
82+
req.Header.Set("Content-Type", "application/json")
83+
resp, err := c.httpClient.Do(req)
8084
if err != nil {
8185
return err
8286
}
8387
defer resp.Body.Close()
8488
if resp.StatusCode != http.StatusOK {
85-
body, _ := ioutil.ReadAll(resp.Body)
89+
body, _ := io.ReadAll(resp.Body)
8690
return fmt.Errorf("non-200 OK status code: %v body: %q", resp.Status, body)
8791
}
8892
var out struct {
8993
Data *json.RawMessage
9094
Errors Errors
91-
//Extensions interface{} // Unused.
95+
//Extensions any // Unused.
9296
}
9397
err = json.NewDecoder(resp.Body).Decode(&out)
9498
if err != nil {
@@ -111,15 +115,15 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab
111115
// Errors represents the "errors" array in a response from a GraphQL server.
112116
// If returned via error interface, the slice is expected to contain at least 1 element.
113117
//
114-
// Specification: http://spec.graphql.org/June2018/#sec-Errors
118+
// Specification: https://spec.graphql.org/October2021/#sec-Errors.
115119
type Errors []struct {
116120
Message string
117121
Locations []struct {
118122
Line int
119123
Column int
120124
}
121-
Path []interface{}
122-
Extensions map[string]interface{}
125+
Path []any
126+
Extensions map[string]any
123127
Type string
124128
}
125129

graphql_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package graphql_test
33
import (
44
"context"
55
"io"
6-
"io/ioutil"
76
"net/http"
87
"net/http/httptest"
98
"testing"
@@ -144,7 +143,7 @@ func TestClient_Query_emptyVariables(t *testing.T) {
144143
Name string
145144
}
146145
}
147-
err := client.Query(context.Background(), &q, map[string]interface{}{})
146+
err := client.Query(context.Background(), &q, map[string]any{})
148147
if err != nil {
149148
t.Fatal(err)
150149
}
@@ -166,7 +165,7 @@ func (l localRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
166165
}
167166

168167
func mustRead(r io.Reader) string {
169-
b, err := ioutil.ReadAll(r)
168+
b, err := io.ReadAll(r)
170169
if err != nil {
171170
panic(err)
172171
}

ident/ident.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func ParseScreamingSnakeCase(name string) Name {
118118
// Name is an identifier name, broken up into individual words.
119119
type Name []string
120120

121-
// ToMixedCaps expresses identifer name in MixedCaps naming convention.
121+
// ToMixedCaps expresses identifier name in MixedCaps naming convention.
122122
//
123123
// E.g., "ClientMutationID".
124124
func (n Name) ToMixedCaps() string {
@@ -141,7 +141,7 @@ func (n Name) ToMixedCaps() string {
141141
return strings.Join(n, "")
142142
}
143143

144-
// ToLowerCamelCase expresses identifer name in lowerCamelCase naming convention.
144+
// ToLowerCamelCase expresses identifier name in lowerCamelCase naming convention.
145145
//
146146
// E.g., "clientMutationId".
147147
func (n Name) ToLowerCamelCase() string {
@@ -180,8 +180,6 @@ func isTwoInitialisms(word string) (string, string, bool) {
180180
// Only add entries that are highly unlikely to be non-initialisms.
181181
// For instance, "ID" is fine (Freudian code is rare), but "AND" is not.
182182
var initialisms = map[string]struct{}{
183-
// These are the common initialisms from golint. Keep them in sync
184-
// with https://gotools.org/github.com/golang/lint#commonInitialisms.
185183
"ACL": {},
186184
"API": {},
187185
"ASCII": {},
@@ -201,6 +199,7 @@ var initialisms = map[string]struct{}{
201199
"RAM": {},
202200
"RHS": {},
203201
"RPC": {},
202+
"RSS": {},
204203
"SLA": {},
205204
"SMTP": {},
206205
"SQL": {},
@@ -211,18 +210,15 @@ var initialisms = map[string]struct{}{
211210
"UDP": {},
212211
"UI": {},
213212
"UID": {},
214-
"UUID": {},
215213
"URI": {},
216214
"URL": {},
217215
"UTF8": {},
216+
"UUID": {},
218217
"VM": {},
219218
"XML": {},
220219
"XMPP": {},
221220
"XSRF": {},
222221
"XSS": {},
223-
224-
// Additional common initialisms.
225-
"RSS": {},
226222
}
227223

228224
// isBrand reports whether word is a brand.
@@ -237,4 +233,9 @@ func isBrand(word string) (string, bool) {
237233
// Only add entries that are highly unlikely to be non-brands.
238234
var brands = map[string]string{
239235
"github": "GitHub",
236+
"gitlab": "GitLab",
237+
"devops": "DevOps", // For https://en.wikipedia.org/wiki/DevOps.
238+
// For https://docs.github.com/en/graphql/reference/enums#fundingplatform.
239+
"issuehunt": "IssueHunt",
240+
"lfx": "LFX",
240241
}

ident/ident_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func TestName_ToMixedCaps(t *testing.T) {
8989
{in: ident.Name{"client", "Mutation", "Id"}, want: "ClientMutationID"},
9090
{in: ident.Name{"CLIENT", "MUTATION", "ID"}, want: "ClientMutationID"},
9191
{in: ident.Name{"github", "logo"}, want: "GitHubLogo"},
92+
{in: ident.Name{"AZURE", "DEVOPS"}, want: "AzureDevOps"},
9293
}
9394
for _, tc := range tests {
9495
got := tc.in.ToMixedCaps()

internal/jsonutil/graphql.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
//
1818
// The implementation is created on top of the JSON tokenizer available
1919
// in "encoding/json".Decoder.
20-
func UnmarshalGraphQL(data []byte, v interface{}) error {
20+
func UnmarshalGraphQL(data []byte, v any) error {
2121
dec := json.NewDecoder(bytes.NewReader(data))
2222
dec.UseNumber()
2323
err := (&decoder{tokenizer: dec}).Decode(v)
@@ -57,7 +57,7 @@ type decoder struct {
5757
}
5858

5959
// Decode decodes a single JSON value from d.tokenizer into v.
60-
func (d *decoder) Decode(v interface{}) error {
60+
func (d *decoder) Decode(v any) error {
6161
rv := reflect.ValueOf(v)
6262
if rv.Kind() != reflect.Ptr {
6363
return fmt.Errorf("cannot decode into non-pointer %T", v)
@@ -280,10 +280,9 @@ func hasGraphQLName(f reflect.StructField, name string) bool {
280280
// GraphQL fragment. It doesn't have a name.
281281
return false
282282
}
283-
if i := strings.Index(value, "("); i != -1 {
284-
value = value[:i]
285-
}
286-
if i := strings.Index(value, ":"); i != -1 {
283+
// Cut off anything that follows the field name,
284+
// such as field arguments, aliases, directives.
285+
if i := strings.IndexAny(value, "(:@"); i != -1 {
287286
value = value[:i]
288287
}
289288
return strings.TrimSpace(value) == name

0 commit comments

Comments
 (0)