File tree Expand file tree Collapse file tree 2 files changed +62
-2
lines changed
Expand file tree Collapse file tree 2 files changed +62
-2
lines changed Original file line number Diff line number Diff line change @@ -295,7 +295,43 @@ GraphQLObjectType person = newObject()
295295 .build();
296296
297297```
298-
298+
299+ ##### Specific Recursive Types
300+
301+ Certain situations require specific type classes as input. For example the possible union types inside a `GraphQLUnionType ` are required
302+ to be `GraphQLObjectType ` objects. You can' t use a `GraphQLTypeReference` in this case as it is not specifically that type.
303+
304+ You can use a `reference` method on each specific type to create a reference to that type.
305+
306+ ```java
307+ GraphQLObjectType objectReference = GraphQLObjectType.reference("ObjectReference");
308+ GraphQLInterfaceType interfaceReference = GraphQLInterfaceType.reference("InterfaceReference");
309+ GraphQLInputObjectType inputObjectReference = GraphQLInputObjectType.reference("InputObjectReference");
310+
311+ ```
312+
313+ you would use this say like this :
314+
315+ ```java
316+ GraphQLUnionType PetType = newUnionType()
317+ .name("Pet")
318+ .possibleType(GraphQLObjectType.reference("Cat"))
319+ .possibleType(DogType)
320+ .typeResolver(new TypeResolver() {
321+ @Override
322+ public GraphQLObjectType getType(Object object) {
323+ if (object instanceof GarfieldSchema.Cat) {
324+ return CatType;
325+ }
326+ if (object instanceof GarfieldSchema.Dog) {
327+ return DogType;
328+ }
329+ return null;
330+ }
331+ })
332+ .build();
333+
334+ ```
299335
300336#### Data fetching
301337
Original file line number Diff line number Diff line change @@ -131,7 +131,6 @@ public GraphQLObjectType getType(Object object) {
131131 .build ();
132132 }
133133
134-
135134 void recursiveTypes () {
136135
137136 GraphQLObjectType person = newObject ()
@@ -142,6 +141,31 @@ void recursiveTypes() {
142141 .build ();
143142 }
144143
144+ void recursiveTypesSpecifically () {
145+
146+ GraphQLObjectType objectReference = GraphQLObjectType .reference ("ObjectReference" );
147+ GraphQLInterfaceType interfaceReference = GraphQLInterfaceType .reference ("InterfaceReference" );
148+ GraphQLInputObjectType inputObjectReference = GraphQLInputObjectType .reference ("InputObjectReference" );
149+
150+ GraphQLUnionType PetType = newUnionType ()
151+ .name ("Pet" )
152+ .possibleType (GraphQLObjectType .reference ("Cat" ))
153+ .possibleType (DogType )
154+ .typeResolver (new TypeResolver () {
155+ @ Override
156+ public GraphQLObjectType getType (Object object ) {
157+ if (object instanceof GarfieldSchema .Cat ) {
158+ return CatType ;
159+ }
160+ if (object instanceof GarfieldSchema .Dog ) {
161+ return DogType ;
162+ }
163+ return null ;
164+ }
165+ })
166+ .build ();
167+ }
168+
145169 void executionStrategies () {
146170
147171 ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor (
You can’t perform that action at this time.
0 commit comments