-
Notifications
You must be signed in to change notification settings - Fork 13
/
graphql.go
53 lines (41 loc) · 1.12 KB
/
graphql.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
49
50
51
52
53
package graphqlparser
import (
"bytes"
"encoding/json"
)
type operationType uint8
const (
queryOperation operationType = iota
mutationOperation
)
type QueryParser struct{}
func NewQueryParser() *QueryParser {
return &QueryParser{}
}
func (c *QueryParser) ParseQuery(q interface{}, variables map[string]interface{}) (bytes.Buffer, error) {
return c.parse(queryOperation, q, variables)
}
func (c *QueryParser) ParseMutation(m interface{}, variables map[string]interface{}) (bytes.Buffer, error) {
return c.parse(mutationOperation, m, variables)
}
func (c *QueryParser) parse(op operationType, v interface{}, variables map[string]interface{}) (bytes.Buffer, error) {
var query string
switch op {
case queryOperation:
query = constructQuery(v, variables)
case mutationOperation:
query = constructMutation(v, variables)
}
in := struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables,omitempty"`
}{
Query: query,
Variables: variables,
}
var buff bytes.Buffer
if err := json.NewEncoder(&buff).Encode(in); err != nil {
return buff, err
}
return buff, nil
}