@@ -29,8 +29,8 @@ type Response struct {
29
29
// request is the payload for GraphQL queries
30
30
type gqlRequest struct {
31
31
Query string `json:"query"`
32
- Variables map [string ]interface {} `json:"variables,omitempty "`
33
- OperationName string `json:"operationName,omitempty "`
32
+ Variables map [string ]interface {} `json:"variables"`
33
+ OperationName * string `json:"operationName"`
34
34
}
35
35
36
36
// Send a GraphQL request to struct or map
@@ -58,30 +58,47 @@ func (c *Client) Send(ctx context.Context, dest interface{}, query string, varia
58
58
// Raw sends a basic GraphQL request without any struct types.
59
59
// Parameter `variables` can be either a map or a struct
60
60
func (c * Client ) Raw (ctx context.Context , query string , variables map [string ]interface {}) (* Response , error ) {
61
- var err error
61
+ payload := gqlRequest {
62
+ Query : query ,
63
+ Variables : variables ,
64
+ }
65
+
66
+ response := & Response {}
67
+ err := c .do (ctx , payload , variables , response )
68
+ if err != nil {
69
+ return nil , errors .Wrap (err , "raw decode" )
70
+ }
62
71
63
- payload := & gqlRequest {
72
+ return response , err
73
+ }
74
+
75
+ func (c * Client ) Full (ctx context.Context , query string , variables map [string ]interface {}, response interface {}) error {
76
+ payload := gqlRequest {
64
77
Query : query ,
65
78
Variables : variables ,
66
79
}
67
80
81
+ return c .do (ctx , payload , variables , response )
82
+ }
83
+
84
+ func (c * Client ) do (ctx context.Context , payload gqlRequest , variables map [string ]interface {}, response interface {}) error {
68
85
requestBody , err := json .Marshal (payload )
69
86
if err != nil {
70
- return nil , errors .Wrap (err , "raw encode" )
87
+ return errors .Wrap (err , "raw encode" )
71
88
}
72
89
73
- req , err := http .NewRequest ("post " , c .url , bytes .NewBuffer (requestBody ))
90
+ req , err := http .NewRequest ("POST " , c .url , bytes .NewBuffer (requestBody ))
74
91
75
92
if err != nil {
76
- return nil , err
93
+ return err
77
94
}
78
95
79
96
req .Header .Set ("content-type" , "application/json" )
80
97
req = req .WithContext (ctx )
81
98
82
99
rawResponse , err := c .http .Do (req )
83
100
if err != nil {
84
- return nil , errors .Wrap (err , "raw post" )
101
+ return errors .Wrap (err , "raw post" )
85
102
}
86
103
defer func () {
87
104
_ = rawResponse .Body .Close ()
@@ -90,20 +107,19 @@ func (c *Client) Raw(ctx context.Context, query string, variables map[string]int
90
107
responseBody , err := ioutil .ReadAll (rawResponse .Body )
91
108
92
109
if err != nil {
93
- return nil , errors .Wrap (err , "raw read" )
110
+ return errors .Wrap (err , "raw read" )
94
111
}
95
112
96
113
if rawResponse .StatusCode != http .StatusOK {
97
- return nil , errors .Errorf ("http status code %d with response %s" , rawResponse .StatusCode , responseBody )
114
+ return errors .Errorf ("http status code %d with response %s" , rawResponse .StatusCode , responseBody )
98
115
}
99
116
100
117
// decode it into map string first, let mapstructure do the final decode
101
118
// mapstructure is way stricter about unknown fields, can handle embedded structs and more
102
- respDataRaw := & Response {}
103
- err = json .Unmarshal (responseBody , & respDataRaw )
119
+ err = json .Unmarshal (responseBody , & response )
104
120
if err != nil {
105
- return nil , errors .Wrap (err , "raw decode" )
121
+ return errors .Wrap (err , "raw decode" )
106
122
}
107
123
108
- return respDataRaw , nil
124
+ return nil
109
125
}
0 commit comments