more documentation in client.go, simplify unmarshaling slightly
This commit is contained in:
+10
-4
@@ -12,6 +12,11 @@ import (
|
|||||||
|
|
||||||
// Client is the interface that the generate code calls into to actually make
|
// Client is the interface that the generate code calls into to actually make
|
||||||
// requests.
|
// requests.
|
||||||
|
//
|
||||||
|
// Unstable: This interface is likely to change before v1.0.
|
||||||
|
//
|
||||||
|
// TODO: figure out what we, and others, actually want to use this for, and
|
||||||
|
// consider supporting explicit hooks for that instead.
|
||||||
type Client interface {
|
type Client interface {
|
||||||
// MakeRequest must make a request to the client's GraphQL API.
|
// MakeRequest must make a request to the client's GraphQL API.
|
||||||
//
|
//
|
||||||
@@ -63,14 +68,15 @@ func NewClient(endpoint string, httpClient *http.Client) Client {
|
|||||||
return &client{endpoint, http.MethodPost, httpClient}
|
return &client{endpoint, http.MethodPost, httpClient}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: set OperationName?
|
||||||
type payload struct {
|
type payload struct {
|
||||||
Query string `json:"query"`
|
Query string `json:"query"`
|
||||||
Variables map[string]interface{} `json:"variables"`
|
Variables map[string]interface{} `json:"variables"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type response struct {
|
type response struct {
|
||||||
Data json.RawMessage `json:"data"`
|
Data interface{} `json:"data"`
|
||||||
Errors gqlerror.List `json:"errors"`
|
Errors gqlerror.List `json:"errors"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) MakeRequest(ctx context.Context, query string, retval interface{}, variables map[string]interface{}) error {
|
func (c *client) MakeRequest(ctx context.Context, query string, retval interface{}, variables map[string]interface{}) error {
|
||||||
@@ -104,6 +110,7 @@ func (c *client) MakeRequest(ctx context.Context, query string, retval interface
|
|||||||
}
|
}
|
||||||
|
|
||||||
var dataAndErrors response
|
var dataAndErrors response
|
||||||
|
dataAndErrors.Data = retval
|
||||||
err = json.NewDecoder(resp.Body).Decode(&dataAndErrors)
|
err = json.NewDecoder(resp.Body).Decode(&dataAndErrors)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -112,6 +119,5 @@ func (c *client) MakeRequest(ctx context.Context, query string, retval interface
|
|||||||
if len(dataAndErrors.Errors) > 0 {
|
if len(dataAndErrors.Errors) > 0 {
|
||||||
return dataAndErrors.Errors
|
return dataAndErrors.Errors
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
return json.Unmarshal(dataAndErrors.Data, &retval)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user