-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.go
112 lines (103 loc) · 2.91 KB
/
app.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
package app
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/graphql-go/graphql"
"google.golang.org/appengine"
)
func makeListField(listType graphql.Output, resolve graphql.FieldResolveFn) *graphql.Field {
return &graphql.Field{
Type: listType,
Resolve: resolve,
Args: graphql.FieldConfigArgument{
"limit": &graphql.ArgumentConfig{Type: graphql.Int},
"offset": &graphql.ArgumentConfig{Type: graphql.Int},
},
}
}
func makeNodeListType(name string, nodeType *graphql.Object) *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: name,
Fields: graphql.Fields{
"nodes": &graphql.Field{Type: graphql.NewList(nodeType)},
"totalCount": &graphql.Field{Type: graphql.Int},
},
})
}
var schema graphql.Schema
var userType = graphql.NewObject(graphql.ObjectConfig{
Name: "User",
Fields: graphql.Fields{
"id": &graphql.Field{Type: graphql.String},
"name": &graphql.Field{Type: graphql.String},
"posts": makeListField(makeNodeListType("PostList", postType), queryPostsByUser),
},
})
var postType = graphql.NewObject(graphql.ObjectConfig{
Name: "Post",
Fields: graphql.Fields{
"id": &graphql.Field{Type: graphql.String},
"userId": &graphql.Field{Type: graphql.String},
"createdAt": &graphql.Field{Type: graphql.DateTime},
"content": &graphql.Field{Type: graphql.String},
},
})
var rootMutation = graphql.NewObject(graphql.ObjectConfig{
Name: "RootMutation",
Fields: graphql.Fields{
"createUser": &graphql.Field{
Type: userType,
Args: graphql.FieldConfigArgument{
"name": &graphql.ArgumentConfig{Type: graphql.NewNonNull(graphql.String)},
},
Resolve: createUser,
},
"createPost": &graphql.Field{
Type: postType,
Args: graphql.FieldConfigArgument{
"userId": &graphql.ArgumentConfig{Type: graphql.NewNonNull(graphql.String)},
"content": &graphql.ArgumentConfig{Type: graphql.NewNonNull(graphql.String)},
},
Resolve: createPost,
},
},
})
var rootQuery = graphql.NewObject(graphql.ObjectConfig{
Name: "RootQuery",
Fields: graphql.Fields{
"user": &graphql.Field{
Type: userType,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{Type: graphql.NewNonNull(graphql.String)},
},
Resolve: queryUser,
},
"posts": makeListField(makeNodeListType("PostList", postType), queryPosts),
},
})
func init() {
schema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: rootQuery,
Mutation: rootMutation,
})
http.HandleFunc("/", handler)
}
func handler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
responseError(w, "Invalid request body", http.StatusBadRequest)
return
}
resp := graphql.Do(graphql.Params{
Schema: schema,
RequestString: string(body),
Context: ctx,
})
if len(resp.Errors) > 0 {
responseError(w, fmt.Sprintf("%+v", resp.Errors), http.StatusBadRequest)
return
}
responseJSON(w, resp)
}