-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsimple_example.cr
241 lines (208 loc) · 4.55 KB
/
simple_example.cr
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
require "spec"
require "../src/graphql-crystal"
#
# Given this simple domain model of users and posts
#
class User
property name
def initialize(@name : String); end
end
class Post
property :title, :body, :author
def initialize(@title : String, @body : String, @author : User); end
end
POSTS = [] of Post
USERS = [User.new("Alice"), User.new("Bob")]
#
# We can instantiate a GraphQL Schema Validator/Executor
# by parsing from a graphql schema definition
#
schema = GraphQL::Schema.from_schema(
%{
schema {
query: QueryType,
mutation: MutationType
}
type QueryType {
posts: [PostType]
users: [UserType]
user(name: String!): User
}
type MutationType {
post(post: PostInput) : PostType
}
input PostInput {
author: String!
title: String!
body: String!
}
type UserType {
name: String
posts: [PostType]
}
type PostType {
author: UserType
title: String
body: String
}
}
)
#
# Then we create the backing types by including the
# GraphQL::ObjectType and defining the fields
# reopening User and Post class
class User
include GraphQL::ObjectType
# defaults to the method of
# the same name without block
field :name
field :posts do
POSTS.select &.author.==(self)
end
end
class Post
include GraphQL::ObjectType
field :title
field :body
field :author
end
#
# A Struct to hold input parameters
#
struct PostInput < GraphQL::Schema::InputType
JSON.mapping(
author: String,
title: String,
body: String
)
end
schema.add_input_type("PostInput", PostInput)
#
# Then we define the top level queries
# extending self to make the module
# act as a singleton model
module QueryType
include GraphQL::ObjectType
extend self
field :users do
USERS
end
field :user do |args|
USERS.find(&.name.==(args["name"].as(String))) || raise "no user by that name"
end
field :posts do
POSTS
end
end
module MutationType
include GraphQL::ObjectType
extend self
field :post do |args|
input = args["post"].as(PostInput)
author = USERS.find &.name.==(input.author) ||
raise "author doesn't exist"
POSTS << Post.new(input.title, input.body, author)
POSTS.last
end
end
#
# finally set the top level Object Types
# on the schema
schema.query_resolver = QueryType
schema.mutation_resolver = MutationType
describe "my graphql schema" do
it "does queries" do
schema.execute("{ users { name posts } }")
.should eq ({
"data" => {
"users" => [
{
"name" => "Alice",
"posts" => [] of String,
},
{
"name" => "Bob",
"posts" => [] of String,
},
],
},
})
end
it "does mutations" do
mutation_string = %{
mutation post($post: PostInput) {
post(post: $post) {
author {
name
posts { title }
}
title
body
}
}
}
payload = {
"post" => {
"author" => "Alice",
"title" => "the long and windy road",
"body" => "that leads to your door",
},
}
schema.execute(mutation_string, payload)
.should eq ({
"data" => {
"post" => {
"title" => "the long and windy road",
"body" => "that leads to your door",
"author" => {
"name" => "Alice",
"posts" => [
{
"title" => "the long and windy road",
},
],
},
},
},
})
end
it "does introspection" do
query_string = %{
{
__schema {
types {
name
}
}
}
}
schema.execute(query_string)
.should eq ({
"data" => {
"__schema" => {
"types" => [
{"name" => "String"},
{"name" => "Boolean"},
{"name" => "Int"},
{"name" => "Float"},
{"name" => "ID"},
{"name" => "QueryType"},
{"name" => "MutationType"},
{"name" => "PostInput"},
{"name" => "UserType"},
{"name" => "PostType"},
{"name" => "__Schema"},
{"name" => "__Type"},
{"name" => "__Field"},
{"name" => "__InputValue"},
{"name" => "__EnumValue"},
{"name" => "__Directive"},
{"name" => "__TypeKind"},
{"name" => "__DirectiveLocation"},
],
},
},
}
)
end
end