@@ -5,18 +5,17 @@ import (
5
5
"context"
6
6
"encoding/json"
7
7
"fmt"
8
- "io/ioutil "
8
+ "io"
9
9
"net/http"
10
10
"strings"
11
11
12
12
"github.com/cli/shurcooL-graphql/internal/jsonutil"
13
- "golang.org/x/net/context/ctxhttp"
14
13
)
15
14
16
15
// Client is a GraphQL client.
17
16
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.
20
19
}
21
20
22
21
// NewClient creates a GraphQL client targeting the specified GraphQL server URL.
@@ -34,29 +33,29 @@ func NewClient(url string, httpClient *http.Client) *Client {
34
33
// Query executes a single GraphQL query request,
35
34
// with a query derived from q, populating the response into it.
36
35
// 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 {
38
37
return c .do (ctx , queryOperation , q , variables , "" )
39
38
}
40
39
41
40
// 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 {
43
42
return c .do (ctx , queryOperation , q , variables , queryName )
44
43
}
45
44
46
45
// Mutate executes a single GraphQL mutation request,
47
46
// with a mutation derived from m, populating the response into it.
48
47
// 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 {
50
49
return c .do (ctx , mutationOperation , m , variables , "" )
51
50
}
52
51
53
52
// 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 {
55
54
return c .do (ctx , mutationOperation , m , variables , queryName )
56
55
}
57
56
58
57
// 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 {
60
59
var query string
61
60
switch op {
62
61
case queryOperation :
@@ -65,8 +64,8 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab
65
64
query = constructMutation (v , variables , queryName )
66
65
}
67
66
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"`
70
69
}{
71
70
Query : query ,
72
71
Variables : variables ,
@@ -76,19 +75,24 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab
76
75
if err != nil {
77
76
return err
78
77
}
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 )
80
84
if err != nil {
81
85
return err
82
86
}
83
87
defer resp .Body .Close ()
84
88
if resp .StatusCode != http .StatusOK {
85
- body , _ := ioutil .ReadAll (resp .Body )
89
+ body , _ := io .ReadAll (resp .Body )
86
90
return fmt .Errorf ("non-200 OK status code: %v body: %q" , resp .Status , body )
87
91
}
88
92
var out struct {
89
93
Data * json.RawMessage
90
94
Errors Errors
91
- //Extensions interface{} // Unused.
95
+ //Extensions any // Unused.
92
96
}
93
97
err = json .NewDecoder (resp .Body ).Decode (& out )
94
98
if err != nil {
@@ -111,15 +115,15 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab
111
115
// Errors represents the "errors" array in a response from a GraphQL server.
112
116
// If returned via error interface, the slice is expected to contain at least 1 element.
113
117
//
114
- // Specification: http ://spec.graphql.org/June2018 /#sec-Errors
118
+ // Specification: https ://spec.graphql.org/October2021 /#sec-Errors.
115
119
type Errors []struct {
116
120
Message string
117
121
Locations []struct {
118
122
Line int
119
123
Column int
120
124
}
121
- Path []interface {}
122
- Extensions map [string ]interface {}
125
+ Path []any
126
+ Extensions map [string ]any
123
127
Type string
124
128
}
125
129
0 commit comments