@@ -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.
1716type 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.
115119type 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
0 commit comments