|
16 | 16 |
|
17 | 17 | import graphql.ExecutionResult; |
18 | 18 | import graphql.GraphQL; |
19 | | -import graphql.annotations.annotationTypes.GraphQLDataFetcher; |
20 | | -import graphql.annotations.annotationTypes.GraphQLField; |
21 | | -import graphql.annotations.annotationTypes.GraphQLUnion; |
| 19 | +import graphql.TypeResolutionEnvironment; |
| 20 | +import graphql.annotations.annotationTypes.*; |
| 21 | +import graphql.annotations.annotationTypes.GraphQLNonNull; |
22 | 22 | import graphql.annotations.processor.GraphQLAnnotations; |
23 | 23 | import graphql.annotations.processor.retrievers.GraphQLInterfaceRetriever; |
24 | 24 | import graphql.annotations.typeResolvers.UnionTypeResolver; |
@@ -103,6 +103,28 @@ public void unionQuery_returnTypeIsScreen_getScreen() { |
103 | 103 | assertEquals(((Map<String, Map<String, String>>) result.getData()).get("hardwareScreen").get("resolution"), 10); |
104 | 104 | } |
105 | 105 |
|
| 106 | + @Test |
| 107 | + public void unionQueryWithCustomTypeResolver_askForDog_getDog() { |
| 108 | + GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(Query.class)).build(); |
| 109 | + |
| 110 | + GraphQL graphQL = GraphQL.newGraphQL(schema).build(); |
| 111 | + String query = "{ pet(kindOfPet:\"dog\"){ ... on Cat {mew}, ... on Dog{waf}} }"; |
| 112 | + ExecutionResult result = graphQL.execute(query); |
| 113 | + assertTrue(result.getErrors().isEmpty()); |
| 114 | + assertEquals(((Map<String, Map<String, String>>) result.getData()).get("pet").get("waf"), "waf"); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void unionQueryWithCustomTypeResolver_askForCat_getCat() { |
| 119 | + GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(Query.class)).build(); |
| 120 | + |
| 121 | + GraphQL graphQL = GraphQL.newGraphQL(schema).build(); |
| 122 | + String query = "{ pet(kindOfPet:\"cat\"){ ... on Cat {mew}, ... on Dog{waf}} }"; |
| 123 | + ExecutionResult result = graphQL.execute(query); |
| 124 | + assertTrue(result.getErrors().isEmpty()); |
| 125 | + assertEquals(((Map<String, Map<String, String>>) result.getData()).get("pet").get("mew"), "mew"); |
| 126 | + } |
| 127 | + |
106 | 128 | static class Screen implements Hardware { |
107 | 129 | @GraphQLField |
108 | 130 | int resolution; |
@@ -136,25 +158,73 @@ public Screen get(DataFetchingEnvironment environment) { |
136 | 158 | } |
137 | 159 |
|
138 | 160 | class Query { |
| 161 | + |
139 | 162 | @GraphQLField |
140 | 163 | @GraphQLDataFetcher(ComputerFetcher.class) |
141 | | - public Hardware getHardwareComputer() { |
142 | | - return null; |
143 | | - } |
144 | | - |
| 164 | + public Hardware getHardwareComputer; |
145 | 165 | @GraphQLField |
146 | 166 | @GraphQLDataFetcher(ScreenFetcher.class) |
147 | | - public Hardware getHardwareScreen() { |
148 | | - return null; |
149 | | - } |
150 | | - } |
| 167 | + public Hardware getHardwareScreen; |
| 168 | + |
| 169 | + @GraphQLField |
| 170 | + @GraphQLDataFetcher(PetDataFetcher.class) |
| 171 | + public Pet getPet(@GraphQLName("kindOfPet") @GraphQLNonNull String kindOfPet){return null;} |
151 | 172 |
|
| 173 | + } |
152 | 174 | static class Computer implements Hardware { |
| 175 | + |
153 | 176 | @GraphQLField |
154 | 177 | String name; |
155 | | - |
156 | 178 | public Computer(String name) { |
157 | 179 | this.name = name; |
158 | 180 | } |
| 181 | + |
| 182 | + } |
| 183 | + @GraphQLUnion(typeResolver = PetResolver.class, possibleTypes = {Cat.class, Dog.class}) |
| 184 | + interface Pet { |
| 185 | + } |
| 186 | + |
| 187 | + static class Cat implements Pet{ |
| 188 | + @GraphQLField |
| 189 | + String mew; |
| 190 | + |
| 191 | + public Cat(String mew) { |
| 192 | + this.mew = mew; |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + static class Dog implements Pet{ |
| 197 | + @GraphQLField |
| 198 | + String waf; |
| 199 | + |
| 200 | + public Dog(String waf) { |
| 201 | + this.waf = waf; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + static class PetResolver implements TypeResolver { |
| 206 | + @Override |
| 207 | + public GraphQLObjectType getType(TypeResolutionEnvironment env) { |
| 208 | + Object object = env.getObject(); |
| 209 | + if(object instanceof Dog) { |
| 210 | + return env.getSchema().getObjectType("Dog"); |
| 211 | + } |
| 212 | + else { |
| 213 | + return env.getSchema().getObjectType("Cat"); |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + public static class PetDataFetcher implements DataFetcher<Pet> { |
| 219 | + @Override |
| 220 | + public Pet get(DataFetchingEnvironment environment) { |
| 221 | + String nameOfPet = environment.getArgument("kindOfPet"); |
| 222 | + if(nameOfPet.toLowerCase().equals("dog")) { |
| 223 | + return new Dog("waf"); |
| 224 | + } |
| 225 | + else { |
| 226 | + return new Cat("mew"); |
| 227 | + } |
| 228 | + } |
159 | 229 | } |
160 | 230 | } |
0 commit comments