-
Notifications
You must be signed in to change notification settings - Fork 4
/
schema.go
209 lines (191 loc) · 5.52 KB
/
schema.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"github.com/graphql-go/graphql"
"encoding/json"
"fmt"
"io/ioutil"
)
//Helper function to import json from file to map
func importJSONDataFromFile(fileName string, result interface{}) (isOK bool) {
isOK = true
content, err := ioutil.ReadFile(fileName)
if err != nil {
fmt.Print("Error:", err)
isOK = false
}
err = json.Unmarshal(content, result)
if err != nil {
isOK = false
fmt.Print("Error:", err)
}
return
}
var BeastList []Beast
var _ = importJSONDataFromFile("./beastData.json", &BeastList)
type Beast struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
OtherNames []string `json:"otherNames"`
ImageURL string `json:"imageUrl"`
}
// define custom GraphQL ObjectType `beastType` for our Golang struct `Beast`
// Note that
// - the fields in our todoType maps with the json tags for the fields in our struct
// - the field type matches the field type in our struct
var beastType = graphql.NewObject(graphql.ObjectConfig{
Name: "Beast",
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
},
"description": &graphql.Field{
Type: graphql.String,
},
"id": &graphql.Field{
Type: graphql.Int,
},
"otherNames": &graphql.Field{
Type: graphql.NewList(graphql.String),
},
"imageUrl": &graphql.Field{
Type: graphql.String,
},
},
})
var currentMaxId = 5
// root mutation
var rootMutation = graphql.NewObject(graphql.ObjectConfig{
Name: "RootMutation",
Fields: graphql.Fields{
"addBeast": &graphql.Field{
Type: beastType, // the return type for this field
Description: "add a new beast",
Args: graphql.FieldConfigArgument{
"name": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
"description": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
"otherNames": &graphql.ArgumentConfig{
Type: graphql.NewList(graphql.String),
},
"imageUrl": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
// marshall and cast the argument value
name, _ := params.Args["name"].(string)
description, _ := params.Args["description"].(string)
otherNames, _ := params.Args["otherNames"].([]string)
imageUrl, _ := params.Args["imageUrl"].(string)
// figure out new id
newID := currentMaxId + 1
currentMaxId = currentMaxId + 1
// perform mutation operation here
// for e.g. create a Beast and save to DB.
newBeast := Beast{
ID: newID,
Name: name,
Description: description,
OtherNames: otherNames,
ImageURL: imageUrl,
}
BeastList = append(BeastList, newBeast)
// return the new Beast object that we supposedly save to DB
// Note here that
// - we are returning a `Beast` struct instance here
// - we previously specified the return Type to be `beastType`
// - `Beast` struct maps to `beastType`, as defined in `beastType` ObjectConfig`
return newBeast, nil
},
},
"updateBeast": &graphql.Field{
Type: beastType, // the return type for this field
Description: "Update existing beast",
Args: graphql.FieldConfigArgument{
"name": &graphql.ArgumentConfig{
Type: graphql.String,
},
"description": &graphql.ArgumentConfig{
Type: graphql.String,
},
"id": &graphql.ArgumentConfig{Type: graphql.NewNonNull(graphql.Int)},
"otherNames": &graphql.ArgumentConfig{
Type: graphql.NewList(graphql.String),
},
"imageUrl": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
id, _ := params.Args["id"].(int)
affectedBeast := Beast{}
// Search list for beast with id
for i := 0; i < len(BeastList); i++ {
if BeastList[i].ID == id {
if _, ok := params.Args["description"]; ok {
BeastList[i].Description = params.Args["description"].(string)
}
if _, ok := params.Args["name"]; ok {
BeastList[i].Name = params.Args["name"].(string)
}
if _, ok := params.Args["imageUrl"]; ok {
BeastList[i].ImageURL = params.Args["imageUrl"].(string)
}
if _, ok := params.Args["otherNames"]; ok {
BeastList[i].OtherNames = params.Args["otherNames"].([]string)
}
// Assign updated beast so we can return it
affectedBeast = BeastList[i]
break
}
}
// Return affected beast
return affectedBeast, nil
},
},
},
})
// root query
// test with Sandbox at localhost:8080/sandbox
var rootQuery = graphql.NewObject(graphql.ObjectConfig{
Name: "RootQuery",
Fields: graphql.Fields{
"beast": &graphql.Field{
Type: beastType,
Description: "Get single beast",
Args: graphql.FieldConfigArgument{
"name": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
nameQuery, isOK := params.Args["name"].(string)
if isOK {
// Search for el with name
for _, beast := range BeastList {
if beast.Name == nameQuery {
return beast, nil
}
}
}
return Beast{}, nil
},
},
"beastList": &graphql.Field{
Type: graphql.NewList(beastType),
Description: "List of beasts",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return BeastList, nil
},
},
},
})
// define schema, with our rootQuery and rootMutation
var BeastSchema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: rootQuery,
Mutation: rootMutation,
})