33
44import graphql .schema .*;
55
6+ import java .util .Arrays ;
7+ import java .util .Collections ;
8+ import java .util .List ;
9+
610import static graphql .Scalars .GraphQLBoolean ;
711import static graphql .Scalars .GraphQLString ;
812import static graphql .schema .GraphQLFieldDefinition .newFieldDefinition ;
1115
1216public class GarfieldSchema {
1317
14- public static GraphQLInterfaceType Named = newInterface ()
18+ public interface Named {
19+ String getName ();
20+ }
21+
22+ public static class Dog implements Named {
23+ private final String name ;
24+ private final boolean barks ;
25+
26+ public Dog (String name , boolean barks ) {
27+ this .name = name ;
28+ this .barks = barks ;
29+ }
30+
31+ @ Override
32+ public String getName () {
33+ return name ;
34+ }
35+ }
36+
37+ public static class Cat implements Named {
38+ private final String name ;
39+ private final boolean meows ;
40+
41+ public Cat (String name , boolean meows ) {
42+ this .name = name ;
43+ this .meows = meows ;
44+ }
45+
46+ @ Override
47+ public String getName () {
48+ return name ;
49+ }
50+ }
51+
52+ public static class Person implements Named {
53+ private final String name ;
54+ private List <Dog > dogs ;
55+ private List <Cat > cats ;
56+ private List <Named > friends ;
57+
58+ public Person (String name ) {
59+ this (name , Collections .<Cat >emptyList (), Collections .<Dog >emptyList (), Collections .<Named >emptyList ());
60+ }
61+
62+ public Person (String name , List <Cat > cats , List <Dog > dogs , List <Named > friends ) {
63+ this .name = name ;
64+ this .dogs = dogs ;
65+ this .cats = cats ;
66+ this .friends = friends ;
67+ }
68+
69+ @ Override
70+ public String getName () {
71+ return name ;
72+ }
73+ }
74+
75+ public static Cat garfield = new Cat ("Garfield" , false );
76+ public static Dog odie = new Dog ("Odie" , true );
77+ public static Person liz = new Person ("Liz" );
78+ public static Person john = new Person ("John" , Arrays .asList (garfield ), Arrays .asList (odie ), Arrays .asList (liz , odie ));
79+
80+ public static GraphQLInterfaceType NamedType = newInterface ()
1581 .name ("Named" )
1682 .field (newFieldDefinition ()
1783 .name ("name" )
@@ -29,7 +95,7 @@ public class GarfieldSchema {
2995 .name ("barks" )
3096 .type (GraphQLBoolean )
3197 .build ())
32- .withInterface (Named )
98+ .withInterface (NamedType )
3399 .build ();
34100
35101 public static GraphQLObjectType CatType = newObject ()
@@ -39,18 +105,25 @@ public class GarfieldSchema {
39105 .type (GraphQLString )
40106 .build ())
41107 .field (newFieldDefinition ()
42- .name ("barks " )
108+ .name ("meows " )
43109 .type (GraphQLBoolean )
44110 .build ())
45- .withInterface (Named )
111+ .withInterface (NamedType )
46112 .build ();
47113
48114 public static GraphQLUnionType PetType = GraphQLUnionType .newUnionType ()
49- .possibleType ( DogType )
115+ .name ( "Pet" )
50116 .possibleType (CatType )
117+ .possibleType (DogType )
51118 .typeResolver (new TypeResolver () {
52119 @ Override
53120 public GraphQLObjectType getType (Object object ) {
121+ if (object instanceof Cat ) {
122+ return CatType ;
123+ }
124+ if (object instanceof Dog ){
125+ return DogType ;
126+ }
54127 return null ;
55128 }
56129 })
@@ -68,9 +141,9 @@ public GraphQLObjectType getType(Object object) {
68141 .build ())
69142 .field (newFieldDefinition ()
70143 .name ("friends" )
71- .type (new GraphQLList (Named ))
144+ .type (new GraphQLList (NamedType ))
72145 .build ())
73- .withInterface (Named )
146+ .withInterface (NamedType )
74147 .build ();
75148
76149 public static GraphQLSchema GarfieldSchema = GraphQLSchema .newSchema ()
0 commit comments