-
Notifications
You must be signed in to change notification settings - Fork 59
/
abstract_test.ml
255 lines (244 loc) · 6.73 KB
/
abstract_test.ml
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
open Graphql
type cat = {
name : string;
kittens : int;
}
type dog = {
name : string;
puppies : int;
}
let meow : cat = { name = "Meow"; kittens = 1 }
let fido : dog = { name = "Fido"; puppies = 2 }
let cat = Schema.(obj "Cat"
~fields:(fun _ -> [
field "name"
~typ:(non_null string)
~args:Arg.[]
~resolve:(fun () (cat : cat) -> cat.name)
;
field "kittens"
~typ:(non_null int)
~args:Arg.[]
~resolve:(fun () (cat : cat) -> cat.kittens)
;
])
)
let dog = Schema.(obj "Dog"
~fields:(fun _ -> [
field "name"
~typ:(non_null string)
~args:Arg.[]
~resolve:(fun () (dog : dog) -> dog.name)
;
field "puppies"
~typ:(non_null int)
~args:Arg.[]
~resolve:(fun () (dog : dog) -> dog.puppies)
;
])
)
let pet : (unit, [`pet]) Schema.abstract_typ = Schema.union "Pet"
let cat_as_pet = Schema.add_type pet cat
let dog_as_pet = Schema.add_type pet dog
let named : (unit, [`named]) Schema.abstract_typ = Schema.(interface "Named"
~fields:(fun _ -> [
abstract_field "name"
~typ:(non_null string)
~args:Arg.[]
])
)
let cat_as_named = Schema.add_type named cat
let dog_as_named = Schema.add_type named dog
let pet_type = Schema.(Arg.enum "pet_type"
~values:[
enum_value "CAT" ~value:`Cat;
enum_value "DOG" ~value:`Dog;
])
let schema = Schema.(schema [
field "pet"
~typ:(non_null pet)
~args:Arg.[
arg "type" ~typ:(non_null pet_type)
]
~resolve:(fun () () pet_type ->
match pet_type with
| `Cat ->
cat_as_pet meow
| `Dog ->
dog_as_pet fido
)
;
field "pets"
~typ:(non_null (list (non_null pet)))
~args:Arg.[]
~resolve:(fun () () -> [cat_as_pet meow; dog_as_pet fido])
;
field "named_objects"
~typ:(non_null (list (non_null named)))
~args:Arg.[]
~resolve:(fun () () -> [cat_as_named meow; dog_as_named fido])
])
let test_query = Test_common.test_query schema ()
let suite = [
("dog as pet", `Quick, fun () ->
let query = "{ pet(type: \"DOG\") { ... on Dog { name puppies } } }" in
test_query query (`Assoc [
"data", `Assoc [
"pet", `Assoc [
"name", `String "Fido";
"puppies", `Int 2
]
]
])
);
("cat as pet", `Quick, fun () ->
let query = "{ pet(type: \"CAT\") { ... on Cat { name kittens } } }" in
test_query query (`Assoc [
"data", `Assoc [
"pet", `Assoc [
"name", `String "Meow";
"kittens", `Int 1
]
]
])
);
("pets", `Quick, fun () ->
let query = "{ pets { ... on Dog { name puppies } ... on Cat { name kittens } } }" in
test_query query (`Assoc [
"data", `Assoc [
"pets", `List [
`Assoc [
"name", `String "Meow";
"kittens", `Int 1
];
`Assoc [
"name", `String "Fido";
"puppies", `Int 2
]
]
]
])
);
("named_objects", `Quick, fun () ->
let query = "{ named_objects { name ... on Dog { puppies } ... on Cat { kittens } } }" in
test_query query (`Assoc [
"data", `Assoc [
"named_objects", `List [
`Assoc [
"name", `String "Meow";
"kittens", `Int 1
];
`Assoc [
"name", `String "Fido";
"puppies", `Int 2
]
]
]
])
);
("nested fragments with union and interface", `Quick, fun () ->
let query = "fragment NamedFragment on Named { ... on Dog { name } ... on Cat { name } } fragment PetFragment on Pet { ... NamedFragment ... on Dog { puppies } ... on Cat { kittens } } { pets { ... PetFragment } }" in
test_query query (`Assoc [
"data", `Assoc [
"pets", `List [
`Assoc [
"name", `String "Meow";
"kittens", `Int 1
];
`Assoc [
"name", `String "Fido";
"puppies", `Int 2
]
]
]
])
);
("introspection", `Quick, fun () ->
let query = "{ __schema { types { name kind possibleTypes { name kind } interfaces { name kind } } } }" in
test_query query (`Assoc [
"data", `Assoc [
"__schema", `Assoc [
"types", `List [
`Assoc [
"name", `String "Named";
"kind", `String "INTERFACE";
"possibleTypes", `List [
`Assoc [
"name", `String "Dog";
"kind", `String "OBJECT"
];
`Assoc [
"name", `String "Cat";
"kind", `String "OBJECT"
]
];
"interfaces", `Null
];
`Assoc [
"name", `String "pet_type";
"kind", `String "ENUM";
"possibleTypes", `Null;
"interfaces", `Null
];
`Assoc [
"name", `String "Cat";
"kind", `String "OBJECT";
"possibleTypes", `Null;
"interfaces", `List [
`Assoc [
"name", `String "Named";
"kind", `String "INTERFACE"
]
]
];
`Assoc [
"name", `String "Int";
"kind", `String "SCALAR";
"possibleTypes", `Null;
"interfaces", `Null
];
`Assoc [
"name", `String "String";
"kind", `String "SCALAR";
"possibleTypes", `Null;
"interfaces", `Null
];
`Assoc [
"name", `String "Dog";
"kind", `String "OBJECT";
"possibleTypes", `Null;
"interfaces", `List [
`Assoc [
"name", `String "Named";
"kind", `String "INTERFACE"
]
]
];
`Assoc [
"name", `String "Pet";
"kind", `String "UNION";
"possibleTypes", `List [
`Assoc [
"name", `String "Dog";
"kind", `String "OBJECT"
];
`Assoc [
"name", `String "Cat";
"kind", `String "OBJECT"
]
];
"interfaces", `Null
];
`Assoc [
"name", `String "query";
"kind", `String "OBJECT";
"possibleTypes", `Null;
"interfaces", `List []
]
]
]
]
]
)
);
]