-
Notifications
You must be signed in to change notification settings - Fork 12
/
graphql.go
159 lines (141 loc) · 3.78 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
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
package main
import (
"encoding/json"
"fmt"
"github.com/graphql-go/graphql"
"math/rand"
"net/http"
)
type Hotel struct {
Id string `json:"id"`
DisplayName string `json:"displayName"`
City string `json:"city"`
NoRooms int `json:"noRooms"`
StarRating int `json:"starRating"`
}
// define custom GraphQL ObjectType `hotelType` for our Golang struct `Hotel`
// Note that
// - the fields map with the json tags for the fields in our struct
// - the field types match the field type in our struct
var hotelType = graphql.NewObject(graphql.ObjectConfig{
Name: "Hotel",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.String,
},
"displayName": &graphql.Field{
Type: graphql.String,
},
"city": &graphql.Field{
Type: graphql.String,
},
"noRooms": &graphql.Field{
Type: graphql.Int,
},
"starRating": &graphql.Field{
Type: graphql.Int,
},
},
})
// define schema, with our rootQuery and rootMutation
var schema, schemaErr = graphql.NewSchema(graphql.SchemaConfig{
Query: rootQuery,
Mutation: rootMutation,
})
func main() {
if schemaErr != nil {
panic(schemaErr)
}
http.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("[in handler]", r.URL.Query())
result := executeQuery(r.URL.Query()["query"][0], schema)
json.NewEncoder(w).Encode(result)
})
fmt.Println("Graphql server is running on port 8080")
http.ListenAndServe(":8080", nil)
}
func executeQuery(query string, schema graphql.Schema) *graphql.Result {
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: query,
})
if len(result.Errors) > 0 {
fmt.Printf("wrong result, unexpected errors: %v", result.Errors)
}
return result
}
// root mutation
var rootMutation = graphql.NewObject(graphql.ObjectConfig{
Name: "RootMutation",
Fields: graphql.Fields{
"createHotel": &graphql.Field{
Type: hotelType, // the return type for this field
Description: "Create new hotel",
Args: graphql.FieldConfigArgument{
"displayName": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
"city": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
"noRooms": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.Int),
},
"starRating": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.Int),
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
// marshall and cast the argument value
displayName, _ := params.Args["displayName"].(string)
city, _ := params.Args["city"].(string)
noRooms, _ := params.Args["noRooms"].(int)
starRating, _ := params.Args["starRating"].(int)
// create in 'DB'
newHotel := Hotel{
Id: randomId(),
DisplayName: displayName,
City: city,
NoRooms: noRooms,
StarRating: starRating,
}
hotels[newHotel.Id] = newHotel
// return the new Hotel object
return newHotel, nil
},
},
},
})
//root query
var rootQuery = graphql.NewObject(graphql.ObjectConfig{
Name: "RootQuery",
Fields: graphql.Fields{
"hotel": &graphql.Field{
Type: hotelType,
Description: "Get a hotel with this id",
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
id, _ := params.Args["id"].(string)
return hotels[id], nil
},
},
},
})
// repository
var hotels map[string]Hotel
func init() {
hotels = make(map[string]Hotel)
}
// Random ID Generator
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randomId() string {
b := make([]rune, 8)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}