-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain-3.go
145 lines (129 loc) · 3.2 KB
/
main-3.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"context"
"encoding/json"
"fmt"
graphql "github.com/graph-gophers/graphql-go"
)
// Define a more capable schema string:
const schemaString = `
schema {
query: Query
}
type Query {
# Generic greeting, e.g. "Hello, world!":
greet: String!
# Customized greeting, e.g. "Hello, Johan!":
greetPerson(person: String!): String!
# More customized greeting, e.g. "Good morning, Johan!":
greetPersonTimeOfDay(person: String!, timeOfDay: TimeOfDay!): String!
}
# Enumerate times of day:
enum TimeOfDay {
MORNING
AFTERNOON
EVENING
}
`
type RootResolver struct{}
func (*RootResolver) Greet() string {
return "Hello, world!"
}
func (*RootResolver) GreetPerson(args struct{ Person string }) string {
return fmt.Sprintf("Hello, %s!", args.Person)
}
// We don’t have to use type literals; we can also write a
// convenience type:
type PersonTimeOfDayArgs struct {
Person string // Note that fields need to be exported.
TimeOfDay string
}
var TimesOfDay = map[string]string{
"MORNING": "Good morning",
"AFTERNOON": "Good afternoon",
"EVENING": "Good evening",
}
// Resolvers can also be passed a context (first argument):
func (*RootResolver) GreetPersonTimeOfDay(ctx context.Context, args PersonTimeOfDayArgs) string {
timeOfDay, ok := TimesOfDay[args.TimeOfDay]
if !ok {
timeOfDay = "Go to bed"
}
return fmt.Sprintf("%s, %s!", timeOfDay, args.Person)
}
var Schema = graphql.MustParseSchema(schemaString, &RootResolver{})
func main() {
ctx := context.Background()
// We can use ClientQuery to aggregate the query’s values:
type ClientQuery struct {
OpName string // Operation name.
Query string // Query string.
Variables map[string]interface{} // Query variables (untyped).
}
q1 := ClientQuery{
OpName: "Greet",
Query: `query Greet {
greet
}`,
Variables: nil,
}
resp1 := Schema.Exec(ctx, q1.Query, q1.OpName, q1.Variables)
json1, err := json.MarshalIndent(resp1, "", "\t")
if err != nil {
panic(err)
}
fmt.Println(string(json1))
// Expected output:
//
// {
// "data": {
// "greet": "Hello, world!"
// }
// }
q2 := ClientQuery{
OpName: "GreetPerson",
// GraphQL queries and mutations can be parameterized:
Query: `query GreetPerson($person: String!) {
greetPerson(person: $person)
}`,
Variables: map[string]interface{}{
"person": "Johan",
},
}
resp2 := Schema.Exec(ctx, q2.Query, q2.OpName, q2.Variables)
json2, err := json.MarshalIndent(resp2, "", "\t")
if err != nil {
panic(err)
}
fmt.Println(string(json2))
// Expected output:
//
// {
// "data": {
// "greetPerson": "Hello, Johan!"
// }
// }
q3 := ClientQuery{
OpName: "GreetPersonTimeOfDay",
Query: `query GreetPersonTimeOfDay($person: String!, $timeOfDay: TimeOfDay!) {
greetPersonTimeOfDay(person: $person, timeOfDay: $timeOfDay)
}`,
Variables: map[string]interface{}{
"person": "Johan",
"timeOfDay": "MORNING",
},
}
resp3 := Schema.Exec(ctx, q3.Query, q3.OpName, q3.Variables)
json3, err := json.MarshalIndent(resp3, "", "\t")
if err != nil {
panic(err)
}
fmt.Println(string(json3))
// Expected output:
//
// {
// "data": {
// "greetPersonTimeOfDay": "Good morning, Johan!"
// }
// }
}