File: util-buildschema.t

package info (click to toggle)
libgraphql-perl 0.54-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 712 kB
  • sloc: perl: 5,094; makefile: 2
file content (629 lines) | stat: -rw-r--r-- 11,933 bytes parent folder | download
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
use lib 't/lib';
use GQLTest;

my $JSON = JSON::MaybeXS->new->allow_nonref->canonical;

BEGIN {
  use_ok( 'GraphQL::Schema' ) || print "Bail out!\n";
  use_ok( 'GraphQL::Execution', qw(execute) ) || print "Bail out!\n";
  use_ok( 'GraphQL::Language::Parser', qw(parse) ) || print "Bail out!\n";
}

subtest 'can use built schema for limited execution' => sub {
  my $ast = parse(<<'EOF');
schema { query: Query }
type Query {
  str: String
}
EOF
  my $schema = GraphQL::Schema->from_ast($ast);
  run_test(
    [$schema, '{ str }', { str => 123 }],
    { data => { str => '123' } },
  );
};

subtest 'can build a schema directly from the source' => sub {
  my $doc = <<'EOF';
schema { query: Query }
type Query {
  add(x: Int, y: Int): Int
}
EOF
  my $schema = GraphQL::Schema->from_doc($doc);
  run_test(
    [$schema, '{ add(x: 34, y: 55) }', {add => sub {$_[0]->{x} + $_[0]->{y}}}],
    { data => { add => 89 } },
  );
};

subtest 'Simple type' => sub {
  my $doc = <<'EOF';
schema {
  query: HelloScalars
}

type HelloScalars {
  bool: Boolean
  float: Float
  id: ID
  int: Int
  str: String
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'With directives' => sub {
  my $doc = <<'EOF';
schema {
  query: Hello
}

directive @foo(arg: Int) on FIELD

type Hello {
  str: String
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Supports descriptions' => sub {
  my $doc = <<'EOF';
schema {
  query: Hello
}

"This is a directive"
directive @foo(
  "It has an argument"
  arg: Int
) on FIELD

"With an enum"
enum Color {
  BLUE
  "Not a creative color"
  GREEN
  RED
}

"What a great type"
type Hello {
  "And a field to boot"
  str: String
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Maintains @skip & @include' => sub {
  my $doc = <<'EOF';
schema {
  query: Hello
}
type Hello {
  str: String
}
EOF
  my $schema = GraphQL::Schema->from_doc($doc);
  is_deeply $schema->directives, \@GraphQL::Directive::SPECIFIED_DIRECTIVES;
};

subtest 'Overriding directives excludes specified' => sub {
  my $doc = <<'EOF';
schema {
  query: Hello
}
directive @skip on FIELD
directive @include on FIELD
directive @deprecated on FIELD_DEFINITION
type Hello {
  str: String
}
EOF
  my $schema = GraphQL::Schema->from_doc($doc);
  is keys %{ $schema->name2directive }, 3;
  isnt $schema->name2directive->{skip}, $GraphQL::Directive::SKIP;
  isnt $schema->name2directive->{include}, $GraphQL::Directive::INCLUDE;
  isnt $schema->name2directive->{deprecated}, $GraphQL::Directive::DEPRECATED;
};

subtest 'Adding directives maintains @skip & @include' => sub {
  my $doc = <<'EOF';
schema {
  query: Hello
}
directive @foo(arg: Int) on FIELD
type Hello {
  str: String
}
EOF
  my $schema = GraphQL::Schema->from_doc($doc);
  is keys %{ $schema->name2directive }, 4;
  is $schema->name2directive->{skip}, $GraphQL::Directive::SKIP;
  is $schema->name2directive->{include}, $GraphQL::Directive::INCLUDE;
  is $schema->name2directive->{deprecated}, $GraphQL::Directive::DEPRECATED;
};

subtest 'Type modifiers' => sub {
  my $doc = <<'EOF';
schema {
  query: HelloScalars
}

type HelloScalars {
  listOfNonNullStrs: [String!]
  listOfStrs: [String]
  nonNullListOfNonNullStrs: [String!]!
  nonNullListOfStrs: [String]!
  nonNullStr: String!
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Recursive type' => sub {
  my $doc = <<'EOF';
schema {
  query: Recurse
}

type Recurse {
  recurse: Recurse
  str: String
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Two types circular' => sub {
  my $doc = <<'EOF';
schema {
  query: TypeOne
}

type TypeOne {
  str: String
  typeTwo: TypeTwo
}

type TypeTwo {
  str: String
  typeOne: TypeOne
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Single argument field' => sub {
  my $doc = <<'EOF';
schema {
  query: Hello
}

type Hello {
  booleanToStr(bool: Boolean): String
  floatToStr(float: Float): String
  idToStr(id: ID): String
  str(int: Int): String
  strToStr(bool: String): String
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Simple type with multiple arguments' => sub {
  my $doc = <<'EOF';
schema {
  query: Hello
}

type Hello {
  str(bool: Boolean, int: Int): String
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Simple type with interface' => sub {
  my $doc = <<'EOF';
schema {
  query: Hello
}

type Hello implements WorldInterface {
  str: String
}

interface WorldInterface {
  str: String
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Simple output enum' => sub {
  my $doc = <<'EOF';
schema {
  query: OutputEnumRoot
}

enum Hello {
  WORLD
}

type OutputEnumRoot {
  hello: Hello
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Simple input enum' => sub {
  my $doc = <<'EOF';
schema {
  query: InputEnumRoot
}

enum Hello {
  WORLD
}

type InputEnumRoot {
  str(hello: Hello): String
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Multiple value enum' => sub {
  my $doc = <<'EOF';
schema {
  query: OutputEnumRoot
}

enum Hello {
  RLD
  WO
}

type OutputEnumRoot {
  hello: Hello
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Simple Union' => sub {
  my $doc = <<'EOF';
schema {
  query: Root
}

union Hello = World

type Root {
  hello: Hello
}

type World {
  str: String
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Multiple Union' => sub {
  my $doc = <<'EOF';
schema {
  query: Root
}

union Hello = WorldOne | WorldTwo

type Root {
  hello: Hello
}

type WorldOne {
  str: String
}

type WorldTwo {
  str: String
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Custom Scalar' => sub {
  my $doc = <<'EOF';
schema {
  query: Root
}

scalar CustomScalar

type Root {
  customScalar: CustomScalar
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Input Object' => sub {
  my $doc = <<'EOF';
schema {
  query: Root
}

input Input {
  int: Int
}

type Root {
  field(in: Input): String
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Simple argument field with default' => sub {
  my $doc = <<'EOF';
schema {
  query: Hello
}

type Hello {
  str(int: Int = 2): String
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Simple type with mutation' => sub {
  my $doc = <<'EOF';
schema {
  query: HelloScalars
  mutation: Mutation
}

type HelloScalars {
  bool: Boolean
  int: Int
  str: String
}

type Mutation {
  addHelloScalars(bool: Boolean, int: Int, str: String): HelloScalars
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

# typo faithfully preserved
subtest 'Simple type with subscription' => sub {
  my $doc = <<'EOF';
schema {
  query: HelloScalars
  subscription: Subscription
}

type HelloScalars {
  bool: Boolean
  int: Int
  str: String
}

type Subscription {
  sbscribeHelloScalars(bool: Boolean, int: Int, str: String): HelloScalars
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Unreferenced type implementing referenced interface' => sub {
  my $doc = <<'EOF';
type Concrete implements Iface {
  key: String
}

interface Iface {
  key: String
}

type Query {
  iface: Iface
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Unreferenced type implementing referenced union' => sub {
  my $doc = <<'EOF';
type Concrete {
  key: String
}

type Query {
  union: Union
}

union Union = Concrete
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Supports @deprecated' => sub {
  my $doc = <<'EOF';
enum MyEnum {
  OLD_VALUE @deprecated
  OTHER_VALUE @deprecated(reason: "Terrible reasons")
  VALUE
}

type Query {
  enum: MyEnum
  field1: String @deprecated
  field2: Int @deprecated(reason: "Because I said so")
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Supports directives on field' => sub {
  my $doc = <<'EOF';
input DirectiveInput {
  field: String @directive
}

interface DirectiveInterface {
  field: String @directive
}

type Query {
  field1: String @directive1
  field2: Int @directive2(arg: true)
}
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

# except it doesn't - just round-trip
subtest 'Correctly assign AST nodes' => sub {
  my $doc = <<'EOF';
directive @test(arg: Int) on FIELD

type Query {
  testField(testArg: TestInput): TestUnion
}

enum TestEnum {
  TEST_VALUE
}

input TestInput {
  testInputField: TestEnum
}

interface TestInterface {
  interfaceField: String
}

type TestType implements TestInterface {
  interfaceField: String
}

union TestUnion = TestType
EOF
  is(GraphQL::Schema->from_doc($doc)->to_doc, $doc);
};

subtest 'Requires a schema definition or Query type' => sub {
  throws_ok { GraphQL::Schema->from_doc(<<'EOF') } qr/Must provide schema definition with query type or a type named Query./;
type Hello {
  bar: Bar
}
EOF
};

subtest 'Allows only a single schema definition' => sub {
  throws_ok { GraphQL::Schema->from_doc(<<'EOF') } qr/Must provide only one schema definition./;
schema { query: Hello }
schema { query: Hello }
type Hello { bar: Bar }
EOF
};

subtest 'Requires a query type' => sub {
  throws_ok { GraphQL::Schema->from_doc(<<'EOF') } qr/Must provide schema definition with query type or a type named Query./;
schema { mutation: Hello }
type Hello { bar: Bar }
EOF
};

subtest 'Allows only a single query type' => sub {
  throws_ok { GraphQL::Schema->from_doc(<<'EOF') } qr/Must provide only one query type in schema/;
schema { query: Hello query: Yellow }
type Hello { bar: Bar }
type Yellow { isColor: Boolean }
EOF
};

subtest 'Allows only a single mutation type' => sub {
  throws_ok { GraphQL::Schema->from_doc(<<'EOF') } qr/Must provide only one mutation type in schema/;
schema { query: Query mutation: Hello mutation: Yellow }
type Hello { bar: Bar }
type Yellow { isColor: Boolean }
EOF
};

subtest 'Allows only a single subscription type' => sub {
  throws_ok { GraphQL::Schema->from_doc(<<'EOF') } qr/Must provide only one subscription type in schema/;
schema { query: Query subscription: Hello subscription: Yellow }
type Hello { bar: Bar }
type Yellow { isColor: Boolean }
EOF
};

subtest 'Unknown type referenced' => sub {
  throws_ok { GraphQL::Schema->from_doc(<<'EOF') } qr/Unknown type 'Bar'/;
schema { query: Hello }
type Hello { bar: Bar }
EOF
};

subtest 'Unknown type in interface list' => sub {
  throws_ok { GraphQL::Schema->from_doc(<<'EOF') } qr/Unknown type 'Bar'/;
schema { query: Hello }
type Hello implements Bar { bar: String }
EOF
};

subtest 'Unknown type in union list' => sub {
  throws_ok { GraphQL::Schema->from_doc(<<'EOF') } qr/Unknown type 'Bar'/;
schema { query: Hello }
union TestUnion = Bar
type Hello { testUnion: TestUnion }
EOF
};

subtest 'Unknown query type' => sub {
  throws_ok { GraphQL::Schema->from_doc(<<'EOF') } qr/Specified query type 'Wat' not found/;
schema { query: Wat }
type Hello { str: String }
EOF
};

subtest 'Unknown mutation|subscription type' => sub {
  throws_ok { GraphQL::Schema->from_doc(<<EOF) } qr/Specified $_ type 'Wat' not found/ for qw(mutation subscription);
schema { query: Hello $_: Wat }
type Hello { str: String }
EOF
};

subtest 'Does not consider operation names' => sub {
  throws_ok { GraphQL::Schema->from_doc(<<'EOF') } qr/Specified query type 'Foo' not found/;
schema { query: Foo }
query Foo { field }
EOF
};

subtest 'Does not consider fragment names' => sub {
  throws_ok { GraphQL::Schema->from_doc(<<'EOF') } qr/Specified query type 'Foo' not found/;
schema { query: Foo }
fragment Foo on Type { field }
EOF
};

subtest 'Forbids duplicate type definitions' => sub {
  throws_ok { GraphQL::Schema->from_doc(<<'EOF') } qr/Type 'Repeated' was defined more than once/;
schema { query: Repeated }
type Repeated { id: Int }
type Repeated { id: String }
EOF
};

done_testing;