-
Notifications
You must be signed in to change notification settings - Fork 531
/
Copy pathmanipulation_test.exs
354 lines (298 loc) · 8.43 KB
/
manipulation_test.exs
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
defmodule Absinthe.Schema.ManipulationTest do
use Absinthe.Case, async: true
alias Absinthe.Phase.Schema.Validation.TypeNamesAreReserved
defmodule ExtTypes do
use Absinthe.Schema.Notation
object :some_dyn_obj do
field :some_dyn_integer, :integer do
meta :some_string_meta, "some_dyn_integer meta"
end
field :some_dyn_string, :string do
meta :some_string_meta, "some_dyn_string meta"
resolve fn _, _ -> {:ok, "some_string_val"} end
end
end
end
defmodule CustomIntrospectionTypes do
use Absinthe.Schema.Notation
object :custom_introspection_helper do
description "Simple Helper Object used to define blueprint fields"
field :simple_string, :string do
description "custom introspection field"
resolve fn _, %{schema: schema} ->
{:ok, "This is a new introspection type on #{inspect(schema)}"}
end
end
field :some_string_meta, :string do
description "Expose some_string_meta"
resolve fn _,
%{
source: source
} ->
private = source.__private__ || []
meta_items = private[:meta] || []
{:ok, meta_items[:some_string_meta]}
end
end
end
end
defmodule MyAppWeb.CustomSchemaPhase do
alias Absinthe.{Phase, Pipeline, Blueprint}
# Add this module to the pipeline of phases
# to run on the schema
def pipeline(pipeline) do
Pipeline.insert_after(pipeline, Phase.Schema.TypeImports, __MODULE__)
end
# Here's the blueprint of the schema, let's do whatever we want with it.
def run(blueprint = %Blueprint{}, _) do
custom_introspection_types = Blueprint.types_by_name(CustomIntrospectionTypes)
custom_introspection_fields = custom_introspection_types["CustomIntrospectionHelper"]
simple_string_field =
Blueprint.find_field(custom_introspection_fields, "simple_string")
|> TypeNamesAreReserved.make_reserved()
some_string_meta_field =
Blueprint.find_field(custom_introspection_fields, "some_string_meta")
|> TypeNamesAreReserved.make_reserved()
blueprint =
blueprint
|> Blueprint.extend_fields(ExtTypes)
|> Blueprint.add_field("__Type", simple_string_field)
|> Blueprint.add_field("__Field", simple_string_field)
|> Blueprint.add_field("__Field", some_string_meta_field)
{:ok, blueprint}
end
end
defmodule MyAppWeb.CustomSchemaEnumTypes do
alias Absinthe.Blueprint.Schema
alias Absinthe.Schema.Notation
alias Absinthe.{Blueprint, Pipeline, Phase}
def pipeline(pipeline) do
Pipeline.insert_after(pipeline, Phase.Schema.TypeImports, __MODULE__)
end
def run(blueprint = %Blueprint{}, _) do
%{schema_definitions: [schema]} = blueprint
new_enum = build_dynamic_enum()
schema =
Map.update!(schema, :type_definitions, fn type_definitions ->
[new_enum | type_definitions]
end)
{:ok, %{blueprint | schema_definitions: [schema]}}
end
def build_dynamic_enum() do
%Schema.EnumTypeDefinition{
name: "Categories",
identifier: :categories,
module: __MODULE__,
__reference__: Notation.build_reference(__ENV__),
values: [
%Schema.EnumValueDefinition{
identifier: :foo,
value: :foo,
name: "FOO",
module: __MODULE__,
__reference__: Notation.build_reference(__ENV__)
},
%Schema.EnumValueDefinition{
identifier: :bar,
value: :bar,
name: "BAR",
module: __MODULE__,
__reference__: Notation.build_reference(__ENV__)
}
]
}
end
end
defmodule MyAppWeb.Schema do
use Absinthe.Schema
@pipeline_modifier MyAppWeb.CustomSchemaPhase
@pipeline_modifier MyAppWeb.CustomSchemaEnumTypes
object :some_obj do
field :some_integer, :integer do
meta :some_string_meta, "some_integer meta"
end
field :some_string, :string do
meta :some_string_meta, "some_string meta"
resolve fn _, _ -> {:ok, "some_string_val"} end
end
end
object :some_dyn_obj do
field :non_dyn_integer, :integer do
meta :some_string_meta, "non_dyn_integer meta"
end
field :non_dyn_string, :string, meta: [some_string_meta: "non_dyn_string meta"] do
resolve fn _, _ -> {:ok, "some_string_val"} end
end
end
query do
field :some_field, :some_obj do
meta :some_field_meta, "some field meta"
resolve fn _, _ -> {:ok, %{some_integer: 1}} end
end
end
end
test "Schema works" do
q = """
query {
some_field {
some_integer
some_string
}
}
"""
expected = %{
data: %{"some_field" => %{"some_integer" => 1, "some_string" => "some_string_val"}}
}
actual = Absinthe.run!(q, MyAppWeb.Schema)
assert expected == actual
end
test "enum types work" do
q = """
query {
__type(name: "Categories") {
enumValues {
name
}
}
}
"""
expected = %{data: %{"__type" => %{"enumValues" => [%{"name" => "BAR"}, %{"name" => "FOO"}]}}}
actual = Absinthe.run!(q, MyAppWeb.Schema)
assert expected == actual
end
test "Introspection works" do
q = """
query {
__type(name: "SomeObj") {
fields {
name
type {
name
}
}
}
}
"""
expected = %{
data: %{
"__type" => %{
"fields" => [
%{"name" => "someInteger", "type" => %{"name" => "Int"}},
%{"name" => "someString", "type" => %{"name" => "String"}}
]
}
}
}
actual = Absinthe.run!(q, MyAppWeb.Schema)
assert expected == actual
end
test "Custom introspection works" do
q = """
query {
__type(name: "SomeObj") {
__simple_string
fields {
name
type {
name
}
}
}
}
"""
expected = %{
data: %{
"__type" => %{
"__simple_string" =>
"This is a new introspection type on Absinthe.Schema.ManipulationTest.MyAppWeb.Schema",
"fields" => [
%{"name" => "someInteger", "type" => %{"name" => "Int"}},
%{"name" => "someString", "type" => %{"name" => "String"}}
]
}
}
}
actual = Absinthe.run!(q, MyAppWeb.Schema)
assert expected == actual
end
test "Exposing meta data via introspection works" do
q = """
query {
__type(name: "SomeObj") {
fields {
name
type {
name
}
__some_string_meta
}
}
}
"""
expected = %{
data: %{
"__type" => %{
"fields" => [
%{
"name" => "someInteger",
"type" => %{"name" => "Int"},
"__some_string_meta" => "some_integer meta"
},
%{
"name" => "someString",
"type" => %{"name" => "String"},
"__some_string_meta" => "some_string meta"
}
]
}
}
}
actual = Absinthe.run!(q, MyAppWeb.Schema)
assert expected == actual
end
test "Extending Objects works" do
q = """
query {
__type(name: "SomeDynObj") {
fields {
name
type {
name
}
__some_string_meta
}
}
}
"""
expected = %{
data: %{
"__type" => %{
"fields" => [
%{
"name" => "nonDynInteger",
"type" => %{"name" => "Int"},
"__some_string_meta" => "non_dyn_integer meta"
},
%{
"name" => "nonDynString",
"type" => %{"name" => "String"},
"__some_string_meta" => "non_dyn_string meta"
},
%{
"name" => "someDynInteger",
"type" => %{"name" => "Int"},
"__some_string_meta" => "some_dyn_integer meta"
},
%{
"name" => "someDynString",
"type" => %{"name" => "String"},
"__some_string_meta" => "some_dyn_string meta"
}
]
}
}
}
actual = Absinthe.run!(q, MyAppWeb.Schema)
assert expected == actual
end
end