-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathGraphQLObjectTest.java
More file actions
913 lines (734 loc) · 32.9 KB
/
Copy pathGraphQLObjectTest.java
File metadata and controls
913 lines (734 loc) · 32.9 KB
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
/**
* Copyright 2016 Yurii Rashkovskii
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*/
package graphql.annotations;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.Scalars;
import graphql.annotations.annotationTypes.*;
import graphql.annotations.annotationTypes.GraphQLNonNull;
import graphql.annotations.processor.GraphQLAnnotations;
import graphql.annotations.processor.ProcessingElementsContainer;
import graphql.annotations.processor.retrievers.GraphQLExtensionsHandler;
import graphql.annotations.processor.retrievers.GraphQLFieldRetriever;
import graphql.annotations.processor.retrievers.GraphQLObjectInfoRetriever;
import graphql.annotations.processor.searchAlgorithms.BreadthFirstSearch;
import graphql.annotations.processor.searchAlgorithms.ParentalSearch;
import graphql.annotations.processor.typeBuilders.InputObjectBuilder;
import graphql.annotations.processor.typeFunctions.TypeFunction;
import graphql.annotations.processor.util.CodeRegistryUtil;
import graphql.com.google.common.collect.ImmutableList;
import graphql.schema.*;
import graphql.schema.GraphQLType;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.SchemaPrinter;
import graphql.schema.idl.TypeDefinitionRegistry;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.lang.reflect.AnnotatedType;
import java.util.*;
import java.util.function.Supplier;
import static graphql.Scalars.GraphQLString;
import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema;
import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_PREFIX;
import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_SUFFIX;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLSchema.newSchema;
import static org.testng.Assert.*;
@SuppressWarnings("unchecked")
public class GraphQLObjectTest {
private GraphQLAnnotations graphQLAnnotations;
@BeforeMethod
public void init() {
this.graphQLAnnotations = new GraphQLAnnotations();
}
private static GraphQLExtensionsHandler newExtensionsHandler(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever) {
GraphQLExtensionsHandler extensionsHandler = new GraphQLExtensionsHandler();
extensionsHandler.setGraphQLObjectInfoRetriever(graphQLObjectInfoRetriever);
extensionsHandler.setFieldSearchAlgorithm(new ParentalSearch(graphQLObjectInfoRetriever));
extensionsHandler.setMethodSearchAlgorithm(new BreadthFirstSearch(graphQLObjectInfoRetriever));
extensionsHandler.setFieldRetriever(new GraphQLFieldRetriever());
return extensionsHandler;
}
public static class DefaultAValue implements Supplier<Object> {
@Override
public Object get() {
return "default";
}
}
@GraphQLDescription("TestObject object")
@GraphQLName("TestObject")
public static class TestObject {
@GraphQLField
@GraphQLName("field0")
@GraphQLDescription("field")
public
@GraphQLNonNull
String field() {
return "test";
}
@GraphQLField
public String fieldWithArgs(@GraphQLName("a") @GraphQLNonNull String a, @GraphQLName("b") @GraphQLDefaultValue(DefaultAValue.class) @GraphQLDescription("b") String b) {
return b;
}
@GraphQLField
public String fieldWithArgsAndEnvironment(DataFetchingEnvironment env, @GraphQLName("a") String a, @GraphQLName("b") String b) {
return a;
}
@GraphQLField
@Deprecated
public String deprecated() {
return null;
}
@GraphQLField
@GraphQLDeprecate("Reason")
public String deprecate() {
return null;
}
@GraphQLField
public String publicTest = "public";
@GraphQLField
private String privateTest = "private";
public String getPrivateTest() {
return privateTest;
}
public void setPrivateTest(String privateTest) {
this.privateTest = privateTest;
}
@GraphQLNonNull
@GraphQLField
@GraphQLName("z_nonOptionalString")
private String z;
public String getZ() {
return z;
}
public void setZ(String z) {
this.z = z;
}
}
private static class TestDefaults {
}
private static class TestObjectNamedArgs {
@GraphQLField
public String fieldWithNamedArgs(@GraphQLName("namedArg") String firstArgument) {
return firstArgument;
}
}
public static class TestMappedObject {
@GraphQLField
public String name;
@GraphQLField
public String aaa;
}
public static class TestObjectDB {
public String aaa;
private String name;
public String getName() {
return name;
}
public TestObjectDB(String name, String aaa) {
this.name = name;
this.aaa = aaa;
}
}
public static class TestQuery {
@GraphQLField
@GraphQLDataFetcher(ObjectFetcher.class)
public TestMappedObject object;
}
public static class ObjectFetcher implements DataFetcher<TestObjectDB> {
@Override
public TestObjectDB get(DataFetchingEnvironment environment) {
return new TestObjectDB("test", "test");
}
}
public static class NameTest {
@GraphQLField
public Boolean isCool;
@GraphQLField
@GraphQLPrettify
public Boolean isAwesome;
@GraphQLField
@GraphQLPrettify
@GraphQLName("yarinnn")
public Boolean isYarin;
@GraphQLField
public String getX() {
return "Asdf0";
}
@GraphQLField
@GraphQLPrettify
public String getY() {
return "asd";
}
@GraphQLField
@GraphQLPrettify
@GraphQLName("daniel")
public String setM() {
return "Asdf";
}
}
@Test
public void objectCreation_nameIsCorrect() {
// Act
GraphQLObjectType object = this.graphQLAnnotations.object(NameTest.class);
// Assert
assertNotNull(object.getFieldDefinition("awesome"));
assertNotNull(object.getFieldDefinition("isCool"));
assertNotNull(object.getFieldDefinition("yarinnn"));
assertNotNull(object.getFieldDefinition("getX"));
assertNotNull(object.getFieldDefinition("y"));
assertNotNull(object.getFieldDefinition("daniel"));
}
@Test
public void fetchTestMappedObject_assertNameIsMappedFromDBObject() {
GraphQLSchema schema = newAnnotationsSchema().query(TestQuery.class).build();
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{object {name aaa}}");
assertTrue(result.getErrors().isEmpty());
assertEquals(((LinkedHashMap) (((LinkedHashMap) result.getData()).get("object"))).get("name"), "test");
assertEquals(((LinkedHashMap) (((LinkedHashMap) result.getData()).get("object"))).get("aaa"), "test");
}
@Test
public void namedFields() {
GraphQLObjectType object = this.graphQLAnnotations.object(TestObjectNamedArgs.class);
List<GraphQLFieldDefinition> fields = object.getFieldDefinitions();
assertEquals(fields.size(), 1);
List<GraphQLArgument> args = fields.get(0).getArguments();
assertEquals(args.size(), 1);
GraphQLArgument arg = args.get(0);
assertEquals(arg.getName(), "namedArg");
}
@Test
public void metainformation() {
GraphQLObjectType object = this.graphQLAnnotations.object(TestObject.class);
assertEquals(object.getName(), "TestObject");
assertEquals(object.getDescription(), "TestObject object");
}
@Test
public void objectClass() {
GraphQLObjectType object = this.graphQLAnnotations.object(TestObject.class);
assertTrue(object instanceof GraphQLObjectType);
}
@Test
public void testSchema() {
GraphQLObjectType object = this.graphQLAnnotations.object(TestObject.class);
String schema = new SchemaPrinter().print(object);
assertTrue(schema.contains("type TestObject {"));
TypeDefinitionRegistry reg = new SchemaParser().parse(schema);
assertTrue(reg.getType("TestObject").isPresent());
assertEquals(new SchemaParser().parse(schema).getType("TestObject").get().getChildren().size(), 8);
}
@Test
public void fields() {
GraphQLObjectType object = this.graphQLAnnotations.object(TestObject.class);
List<GraphQLFieldDefinition> fields = object.getFieldDefinitions();
assertEquals(fields.size(), 8);
fields = ImmutableList.sortedCopyOf((o1, o2) -> o1.getName().compareTo(o2.getName()), fields);
assertEquals(fields.get(2).getName(), "field0");
assertEquals(fields.get(2).getDescription(), "field");
assertTrue(fields.get(2).getType() instanceof graphql.schema.GraphQLNonNull);
assertEquals(((graphql.schema.GraphQLNonNull) fields.get(2).getType()).getWrappedType(), GraphQLString);
assertEquals(fields.get(3).getName(), "fieldWithArgs");
List<GraphQLArgument> args = fields.get(3).getArguments();
assertEquals(args.size(), 2);
assertEquals(args.get(0).getName(), "a");
assertTrue(args.get(0).getType() instanceof graphql.schema.GraphQLNonNull);
assertEquals(((graphql.schema.GraphQLNonNull) args.get(0).getType()).getWrappedType(), GraphQLString);
assertEquals(args.get(1).getName(), "b");
assertEquals(args.get(1).getType(), GraphQLString);
assertEquals(args.get(1).getDescription(), "b");
assertEquals(fields.get(4).getName(), "fieldWithArgsAndEnvironment");
args = fields.get(4).getArguments();
assertEquals(args.size(), 2);
assertEquals(fields.get(1).getName(), "deprecated");
assertTrue(fields.get(1).isDeprecated());
assertEquals(fields.get(0).getName(), "deprecate");
assertTrue(fields.get(0).isDeprecated());
assertEquals(fields.get(0).getDeprecationReason(), "Reason");
assertEquals(fields.get(5).getName(), "privateTest");
assertEquals(fields.get(6).getName(), "publicTest");
DataFetcher dataFetcher1 = CodeRegistryUtil.getDataFetcher(this.graphQLAnnotations.getContainer().getCodeRegistryBuilder(), object, fields.get(5));
DataFetcher dataFetcher2 = CodeRegistryUtil.getDataFetcher(this.graphQLAnnotations.getContainer().getCodeRegistryBuilder(), object, fields.get(6));
assertEquals(dataFetcher1.getClass(), PropertyDataFetcher.class);
assertEquals(dataFetcher2.getClass(), PropertyDataFetcher.class);
assertEquals(fields.get(7).getName(), "z_nonOptionalString");
assertTrue(fields.get(7).getType() instanceof graphql.schema.GraphQLNonNull);
}
public static class TestObjectInherited extends TestObject {
@Override
@GraphQLName("field1") // Test overriding field
public String field() {
return "inherited";
}
}
@Test
public void methodInheritance() {
GraphQLObjectType object = this.graphQLAnnotations.object(TestObject.class);
GraphQLObjectType objectInherited = this.graphQLAnnotations.object(TestObjectInherited.class);
assertEquals(object.getFieldDefinitions().size(), objectInherited.getFieldDefinitions().size());
GraphQLSchema schema = newAnnotationsSchema().query(TestObject.class).build();
GraphQLSchema schemaInherited = newAnnotationsSchema().query(TestObjectInherited.class).build();
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(GraphQLHelper.createExecutionInput("{field0}", new TestObject()));
assertEquals(((Map<String, Object>) result.getData()).get("field0"), "test");
GraphQL graphQL = GraphQL.newGraphQL(schemaInherited).build();
result = graphQL.execute(GraphQLHelper.createExecutionInput("{field1}", new TestObjectInherited()));
assertEquals(((Map<String, Object>) result.getData()).get("field1"), "inherited");
}
public static class TestObjectBridgMethodParent<Type> {
private final Type id;
public TestObjectBridgMethodParent(Type id) {
this.id = id;
}
public Type id() {
return id;
}
}
public static class TestObjectBridgMethod extends TestObjectBridgMethodParent<String> {
public TestObjectBridgMethod() {
super("1");
}
@Override
@GraphQLField
public String id() {
return super.id();
}
}
@Test
public void methodInheritanceWithGenerics() {
GraphQLSchema schema = newAnnotationsSchema().query(TestObjectBridgMethod.class).build();
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(GraphQLHelper.createExecutionInput("{id}", new TestObjectBridgMethod()));
assertEquals(((Map<String, Object>) result.getData()).get("id"), "1");
}
public interface Iface {
@GraphQLField
default String field() {
return "field";
}
}
public static class IfaceImpl implements Iface {
}
@Test
public void interfaceInheritance() {
GraphQLObjectType object = this.graphQLAnnotations.object(IfaceImpl.class);
assertEquals(object.getFieldDefinitions().size(), 1);
assertEquals(object.getFieldDefinition("field").getType(), GraphQLString);
}
private static class TestAccessors {
@GraphQLField
public String getValue() {
return "hello";
}
@GraphQLField
public String setAnotherValue(String s) {
return s;
}
}
@Test
public void accessors() {
GraphQLObjectType object = this.graphQLAnnotations.object(TestAccessors.class);
List<GraphQLFieldDefinition> fields = object.getFieldDefinitions();
assertEquals(fields.size(), 2);
fields = ImmutableList.sortedCopyOf(Comparator.comparing(GraphQLFieldDefinition::getName), fields);
assertEquals(fields.get(0).getName(), "getValue");
assertEquals(fields.get(1).getName(), "setAnotherValue");
}
@Test
public void defaults() {
GraphQLObjectType object = this.graphQLAnnotations.object(TestDefaults.class);
assertEquals(object.getName(), "TestDefaults");
assertNull(object.getDescription());
}
public static class TestField {
@GraphQLField
@GraphQLName("field1")
public String field = "test";
}
public static class PrivateTestField {
@GraphQLField
@GraphQLName("field1")
private String field = "test";
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
@GraphQLField
private String field2 = "test";
public String field2() {
return field2;
}
public PrivateTestField sfield2(String field2) {
this.field2 = field2;
return this;
}
@GraphQLField
private boolean booleanField = true;
public boolean isBooleanField() {
return booleanField;
}
public void setBooleanField(boolean booleanField) {
this.booleanField = booleanField;
}
}
@Test
public void field() {
GraphQLObjectType object = this.graphQLAnnotations.object(TestField.class);
List<GraphQLFieldDefinition> fields = object.getFieldDefinitions();
assertEquals(fields.size(), 1);
assertEquals(fields.get(0).getName(), "field1");
}
private static class OnMethodTest {
private String value;
@GraphQLField
public String getValue() {
return value;
}
}
@Test
public void onMethod() {
GraphQLObjectType object = this.graphQLAnnotations.object(OnMethodTest.class);
List<GraphQLFieldDefinition> fields = object.getFieldDefinitions();
assertEquals(fields.size(), 1);
assertEquals(fields.get(0).getName(), "getValue");
}
public static class TestFetcher implements DataFetcher {
@Override
public Object get(DataFetchingEnvironment environment) {
return "test";
}
}
private static class TestDataFetcher {
@GraphQLField
@GraphQLDataFetcher(TestFetcher.class)
public String field;
@GraphQLField
@GraphQLDataFetcher(TestFetcher.class)
public String someField() {
return "not test";
}
}
@Test
public void dataFetcher() {
GraphQLSchema schema = newAnnotationsSchema().query(TestDataFetcher.class).build();
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(GraphQLHelper.createExecutionInput("{field someField}", new TestObject()));
assertTrue(result.getErrors().isEmpty());
assertEquals(((Map<String, String>) result.getData()).get("field"), "test");
assertEquals(((Map<String, String>) result.getData()).get("someField"), "test");
}
@Test
public void query() {
GraphQLSchema schema = newAnnotationsSchema().query(TestObject.class).build();
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(GraphQLHelper.createExecutionInput("{field0}", new TestObject()));
assertTrue(result.getErrors().isEmpty());
assertEquals(((Map<String, String>) result.getData()).get("field0"), "test");
result = GraphQL.newGraphQL(schema).build().execute(GraphQLHelper.createExecutionInput("{fieldWithArgs(a: \"test\", b: \"passed\")}", new TestObject()));
assertTrue(result.getErrors().isEmpty());
assertEquals(((Map<String, String>) result.getData()).get("fieldWithArgs"), "passed");
result = GraphQL.newGraphQL(schema).build().execute(GraphQLHelper.createExecutionInput("{fieldWithArgsAndEnvironment(a: \"test\", b: \"passed\")}", new TestObject()));
assertTrue(result.getErrors().isEmpty());
assertEquals(((Map<String, String>) result.getData()).get("fieldWithArgsAndEnvironment"), "test");
}
@Test
public void queryField() {
GraphQLSchema schema = newAnnotationsSchema().query(TestField.class).build();
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(GraphQLHelper.createExecutionInput("{field1}", new TestField()));
assertTrue(result.getErrors().isEmpty());
assertEquals(((Map<String, String>) result.getData()).get("field1"), "test");
}
@Test
public void queryPrivateField() {
GraphQLSchema schema = newAnnotationsSchema().query(PrivateTestField.class).build();
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(GraphQLHelper.createExecutionInput("{field1, field2, booleanField}", new PrivateTestField()));
assertTrue(result.getErrors().isEmpty());
assertEquals(((Map<String, String>) result.getData()).get("field1"), "test");
assertEquals(((Map<String, String>) result.getData()).get("field2"), "test");
assertTrue(((Map<String, Boolean>) result.getData()).get("booleanField"));
}
@Test
public void defaultArg() {
GraphQLSchema schema = newAnnotationsSchema().query(TestObject.class).build();
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(GraphQLHelper.createExecutionInput("{fieldWithArgs(a: \"test\")}", new TestObject()));
assertTrue(result.getErrors().isEmpty());
assertEquals(((Map<String, String>) result.getData()).get("fieldWithArgs"), "default");
}
public static class Class1 {
@GraphQLField
public Class2 class2;
@GraphQLField
public String value;
}
public static class Class2 {
@GraphQLField
public Class1 class1;
@GraphQLField
public String value;
}
@Test
public void recursiveTypes() {
GraphQLAnnotations graphQLAnnotations = new GraphQLAnnotations();
GraphQLObjectType object = graphQLAnnotations.getObjectHandler().getGraphQLType(Class1.class, graphQLAnnotations.getContainer());
GraphQLSchema schema = newSchema().query(object).build();
Class1 class1 = new Class1();
Class2 class2 = new Class2();
class1.class2 = class2;
class2.class1 = class1;
class2.value = "hello";
class1.value = "bye";
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(GraphQLHelper.createExecutionInput( "{ class2 { value } }", class1));
assertTrue(result.getErrors().isEmpty());
Map<String, Object> data = (Map<String, Object>) result.getData();
assertEquals(((Map<String, Object>) data.get("class2")).get("value"), "hello");
result = GraphQL.newGraphQL(schema).build().execute(GraphQLHelper.createExecutionInput("{ class2 { class1 { value } } }", class1));
assertTrue(result.getErrors().isEmpty());
data = (Map<String, Object>) result.getData();
Map<String, Object> k1 = (Map<String, Object>) ((Map<String, Object>) data.get("class2")).get("class1");
assertEquals(k1.get("value"), "bye");
result = GraphQL.newGraphQL(schema).build().execute(GraphQLHelper.createExecutionInput("{ class2 { class1 { class2 { value } } } }", class1));
assertTrue(result.getErrors().isEmpty());
}
private static class TestCustomType {
@GraphQLField
public UUID id() {
return UUID.randomUUID();
}
}
@Test
public void customType() {
this.graphQLAnnotations.registerTypeFunction(new UUIDTypeFunction());
GraphQLObjectType object = this.graphQLAnnotations.object(TestCustomType.class);
assertEquals(object.getFieldDefinition("id").getType(), GraphQLString);
}
private static class TestCustomTypeFunction {
@GraphQLField
@graphql.annotations.annotationTypes.GraphQLType(UUIDTypeFunction.class)
public UUID id() {
return UUID.randomUUID();
}
}
@Test
public void customTypeFunction() {
GraphQLObjectType object = this.graphQLAnnotations.object(TestCustomTypeFunction.class);
assertEquals(object.getFieldDefinition("id").getType(), GraphQLString);
}
public static class TestInputArgument {
@GraphQLField
public String a;
@GraphQLField
public int b;
public TestInputArgument(@GraphQLName("a") String a, @GraphQLName("b") int b) {
this.a = a;
this.b = b;
}
}
public static class TestComplexInputArgument {
public Collection<TestInputArgument> inputs;
public TestComplexInputArgument(@GraphQLName("inputs") Collection<TestInputArgument> inputs) {
this.inputs = inputs;
}
@GraphQLField
public Collection<TestInputArgument> inputs() {
return inputs;
}
}
public static class TestObjectInput {
@GraphQLField
public String test(@GraphQLName("other") int other, @GraphQLName("arg") TestInputArgument arg) {
return arg.a;
}
@GraphQLField
public String test2(@GraphQLName("other") int other, @GraphQLName("arg") TestComplexInputArgument arg) {
return arg.inputs.iterator().next().a;
}
}
public static class InputObject {
@GraphQLField
int a;
@GraphQLField
int b;
}
@Test
public void inputObjectArgument() {
GraphQLObjectType object = this.graphQLAnnotations.object(TestObjectInput.class);
GraphQLArgument argument = object.getFieldDefinition("test").getArgument("arg");
assertTrue(argument.getType() instanceof GraphQLInputObjectType);
assertEquals(argument.getName(), "arg");
GraphQLSchema schema = newAnnotationsSchema().query(TestObjectInput.class).build();
ExecutionResult result = GraphQL.newGraphQL( schema ).build().execute(
GraphQLHelper.createExecutionInput( "{ test( other:0,arg: { a:\"ok\", b:2 }) }", new TestObjectInput() ) );
assertTrue(result.getErrors().isEmpty());
Map<String, Object> v = (Map<String, Object>) result.getData();
assertEquals(v.get("test"), "ok");
}
@Test
public void complexInputObjectArgument() {
GraphQLObjectType object = this.graphQLAnnotations.object(TestObjectInput.class);
GraphQLArgument argument = object.getFieldDefinition("test2").getArgument("arg");
assertTrue(argument.getType() instanceof GraphQLInputObjectType);
assertEquals(argument.getName(), "arg");
GraphQLSchema schema = newAnnotationsSchema().query(TestObjectInput.class).build();
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(GraphQLHelper.createExecutionInput("{ test2(arg: {inputs:[{ a:\"ok\", b:2 }]}, other:0) }", new TestObjectInput()));
assertTrue(result.getErrors().isEmpty());
Map<String, Object> v = result.getData();
assertEquals(v.get("test2"), "ok");
}
@Test
public void inputObject() {
GraphQLObjectInfoRetriever graphQLObjectInfoRetriever = new GraphQLObjectInfoRetriever();
GraphQLInputObjectType type = new InputObjectBuilder(graphQLObjectInfoRetriever, new ParentalSearch(graphQLObjectInfoRetriever),
new BreadthFirstSearch(graphQLObjectInfoRetriever), new GraphQLFieldRetriever(), newExtensionsHandler(graphQLObjectInfoRetriever)).
getInputObjectBuilder(InputObject.class, this.graphQLAnnotations.getContainer()).build();
assertEquals(type.getName(), DEFAULT_INPUT_PREFIX + InputObject.class.getSimpleName(), "Type name prefix did not match expected value");
assertEquals(type.getFields().size(), InputObject.class.getDeclaredFields().length);
}
@Test
public void inputObjectCustomPrefixes() {
GraphQLObjectInfoRetriever graphQLObjectInfoRetriever = new GraphQLObjectInfoRetriever();
ProcessingElementsContainer container = this.graphQLAnnotations.getContainer();
container.setInputPrefix("");
container.setInputSuffix("Input");
GraphQLInputObjectType type = new InputObjectBuilder(graphQLObjectInfoRetriever, new ParentalSearch(graphQLObjectInfoRetriever),
new BreadthFirstSearch(graphQLObjectInfoRetriever), new GraphQLFieldRetriever(), newExtensionsHandler(graphQLObjectInfoRetriever)).
getInputObjectBuilder(InputObject.class, this.graphQLAnnotations.getContainer()).build();
assertEquals(type.getName(), "" + InputObject.class.getSimpleName() + "Input", "Type name prefix did not match expected value");
assertEquals(type.getFields().size(), InputObject.class.getDeclaredFields().length);
container.setInputPrefix(DEFAULT_INPUT_PREFIX);
container.setInputSuffix(DEFAULT_INPUT_SUFFIX);
}
public static class UUIDTypeFunction implements TypeFunction {
@Override
public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
return aClass == UUID.class;
}
@Override
public GraphQLType buildType(boolean input, Class<?> aClass, AnnotatedType annotatedType, ProcessingElementsContainer container) {
return buildType(input, aClass, annotatedType);
}
@Override
public String getTypeName(Class<?> aClass, AnnotatedType annotatedType) {
return "UUID";
}
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
return GraphQLString;
}
}
public static class OptionalTest {
@GraphQLField
public Optional<String> empty = Optional.empty();
@GraphQLField
public Optional<String> nonempty = Optional.of("test");
public OptionalTest() {
}
public OptionalTest(Optional<String> empty, Optional<String> nonempty) {
this.empty = empty;
this.nonempty = nonempty;
}
@Override
public String toString() {
return "OptionalTest" +
"{empty=" + empty +
", nonempty=" + nonempty +
'}';
}
}
@Test
public void queryOptional() {
GraphQLObjectType object = this.graphQLAnnotations.object(OptionalTest.class);
GraphQLSchema schema = newSchema().query(object).build();
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
ExecutionResult result = graphQL.execute( GraphQLHelper.createExecutionInput( "{empty, nonempty}", new OptionalTest() ) );
assertTrue(result.getErrors().isEmpty());
Map<String, Object> v = (Map<String, Object>) result.getData();
assertNull(v.get("empty"));
assertEquals(v.get("nonempty"), "test");
}
@Test
public void optionalInput() {
GraphQLObjectType object = this.graphQLAnnotations.object(OptionalTest.class);
GraphQLObjectInfoRetriever graphQLObjectInfoRetriever = new GraphQLObjectInfoRetriever();
GraphQLInputObjectType inputObject = new InputObjectBuilder(graphQLObjectInfoRetriever, new ParentalSearch(graphQLObjectInfoRetriever),
new BreadthFirstSearch(graphQLObjectInfoRetriever), new GraphQLFieldRetriever(), newExtensionsHandler(graphQLObjectInfoRetriever)).
getInputObjectBuilder(OptionalTest.class, this.graphQLAnnotations.getContainer()).build();
GraphQLObjectType mutation = GraphQLObjectType.newObject().name("mut").field(newFieldDefinition().name("test").type(object).
argument(GraphQLArgument.newArgument().type(inputObject).name("input").build()).dataFetcher(environment -> {
Map<String, String> input = environment.getArgument("input");
return new OptionalTest(Optional.ofNullable(input.get("empty")), Optional.ofNullable(input.get("nonempty")));
}).build()).build();
GraphQLSchema schema = newSchema().query(object).mutation(mutation).build();
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
ExecutionResult result = graphQL.execute(GraphQLHelper.createExecutionInput( "mutation {test(input: {empty: \"test\"}) { empty nonempty } }", new OptionalTest() ));
assertTrue(result.getErrors().isEmpty());
Map<String, Object> v = (Map<String, Object>) ((Map<String, Object>) result.getData()).get("test");
assertEquals(v.get("empty"), "test");
assertNull(v.get("nonempty"));
}
public static class EnumTest {
public enum E {A, B}
@GraphQLField
public E e;
public EnumTest() {
}
public EnumTest(E e) {
this.e = e;
}
@Override
public String toString() {
return "EnumTest{" + "e=" + e + '}';
}
}
@Test
public void queryEnum() {
GraphQLObjectType object = this.graphQLAnnotations.object(EnumTest.class);
GraphQLSchema schema = newSchema().query(object).build();
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
ExecutionResult result = graphQL.execute( GraphQLHelper.createExecutionInput( "{e}", new EnumTest( EnumTest.E.B ) ) );
assertTrue(result.getErrors().isEmpty());
Map<String, Object> v = (Map<String, Object>) result.getData();
assertEquals(v.get("e"), "B");
}
public static class ParametrizedArgsTest {
@GraphQLField
public String first(List<String> l) {
return l.get(0);
}
}
@Test
public void parametrizedArg() {
GraphQLObjectType object = this.graphQLAnnotations.object(ParametrizedArgsTest.class);
GraphQLInputType t = object.getFieldDefinition("first").getArguments().get(0).getType();
assertTrue(t instanceof GraphQLList);
assertEquals(((GraphQLList) t).getWrappedType(), Scalars.GraphQLString);
}
@GraphQLField
public static class InheritGraphQLFieldTest {
public String inheritedOn;
@GraphQLField(false)
public String forcedOff;
public String on() {
return "on";
}
@GraphQLField(false)
public String off() {
return "off";
}
}
@Test
public void inheritGraphQLField() {
GraphQLObjectType object = this.graphQLAnnotations.object(InheritGraphQLFieldTest.class);
assertNotNull(object.getFieldDefinition("on"));
assertNull(object.getFieldDefinition("off"));
assertNotNull(object.getFieldDefinition("inheritedOn"));
assertNull(object.getFieldDefinition("forcedOff"));
}
}