-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (42 loc) · 988 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package gql_payload
import (
"bytes"
"encoding/json"
)
type GraphQL struct {
q string
vars map[string]interface{}
}
// NewField makes a new gql object.
func NewField(q string) *GraphQL {
g := &GraphQL{
q: q,
}
return g
}
// Var sets a variable.
func (g *GraphQL) Var(key string, value interface{}) *GraphQL {
if g.vars == nil {
g.vars = make(map[string]interface{})
}
g.vars[key] = value
return g
}
//GenerateRequestBody generates the JSON encoded request body with:
// Query (Required)
// Variables (Optional)
// ref: https://graphql.org/learn/queries/#variables
func (g *GraphQL) GenerateRequestBody() (*bytes.Buffer, error) {
var requestBody bytes.Buffer
requestBodyObj := struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables"`
}{
Query: g.q,
Variables: g.vars,
}
if err := json.NewEncoder(&requestBody).Encode(requestBodyObj); err != nil {
return nil, err
}
return &requestBody, nil
}