|
5 | 5 | import graphql.language.Definition; |
6 | 6 | import graphql.language.EnumTypeExtensionDefinition; |
7 | 7 | import graphql.language.InputObjectTypeExtensionDefinition; |
| 8 | +import graphql.language.InterfaceTypeDefinition; |
8 | 9 | import graphql.language.InterfaceTypeExtensionDefinition; |
| 10 | +import graphql.language.ObjectTypeDefinition; |
9 | 11 | import graphql.language.ObjectTypeExtensionDefinition; |
10 | 12 | import graphql.language.ScalarTypeDefinition; |
11 | 13 | import graphql.language.ScalarTypeExtensionDefinition; |
12 | 14 | import graphql.language.SchemaDefinition; |
13 | 15 | import graphql.language.Type; |
14 | 16 | import graphql.language.TypeDefinition; |
15 | 17 | import graphql.language.TypeName; |
| 18 | +import graphql.language.UnionTypeDefinition; |
16 | 19 | import graphql.language.UnionTypeExtensionDefinition; |
17 | 20 | import graphql.schema.idl.errors.SchemaProblem; |
18 | 21 | import graphql.schema.idl.errors.SchemaRedefinitionError; |
19 | 22 | import graphql.schema.idl.errors.TypeRedefinitionError; |
| 23 | +import graphql.util.FpKit; |
20 | 24 |
|
21 | 25 | import java.util.ArrayList; |
22 | 26 | import java.util.LinkedHashMap; |
23 | 27 | import java.util.List; |
24 | 28 | import java.util.Map; |
25 | 29 | import java.util.Optional; |
26 | 30 | import java.util.function.Function; |
| 31 | +import java.util.stream.Collectors; |
27 | 32 |
|
28 | 33 | import static java.util.Optional.ofNullable; |
29 | 34 |
|
@@ -261,4 +266,117 @@ public <T extends TypeDefinition> Optional<T> getType(String typeName, Class<T> |
261 | 266 | } |
262 | 267 | return Optional.empty(); |
263 | 268 | } |
| 269 | + |
| 270 | + /** |
| 271 | + * Returns true if the specified type exists in the registry and is an abstract (Interface or Union) type |
| 272 | + * |
| 273 | + * @param type the type to check |
| 274 | + * |
| 275 | + * @return true if its abstract |
| 276 | + */ |
| 277 | + public boolean isAbstractType(Type type) { |
| 278 | + Optional<TypeDefinition> typeDefinition = getType(type); |
| 279 | + if (typeDefinition.isPresent()) { |
| 280 | + TypeDefinition definition = typeDefinition.get(); |
| 281 | + return definition instanceof UnionTypeDefinition || definition instanceof InterfaceTypeDefinition; |
| 282 | + } |
| 283 | + return false; |
| 284 | + } |
| 285 | + |
| 286 | + /** |
| 287 | + * Returns true if the specified type exists in the registry and is an object type |
| 288 | + * |
| 289 | + * @param type the type to check |
| 290 | + * |
| 291 | + * @return true if its an object type |
| 292 | + */ |
| 293 | + public boolean isObjectType(Type type) { |
| 294 | + return getType(type, ObjectTypeDefinition.class).isPresent(); |
| 295 | + } |
| 296 | + |
| 297 | + /** |
| 298 | + * Returns a list of types in the registry of that specified class |
| 299 | + * |
| 300 | + * @param targetClass the class to search for |
| 301 | + * @param <T> must extend TypeDefinition |
| 302 | + * |
| 303 | + * @return a list of types of the target class |
| 304 | + */ |
| 305 | + public <T extends TypeDefinition> List<T> getTypes(Class<T> targetClass) { |
| 306 | + return types.values().stream() |
| 307 | + .filter(targetClass::isInstance) |
| 308 | + .map(targetClass::cast) |
| 309 | + .collect(Collectors.toList()); |
| 310 | + } |
| 311 | + |
| 312 | + /** |
| 313 | + * Returns a map of types in the registry of that specified class keyed by name |
| 314 | + * |
| 315 | + * @param targetClass the class to search for |
| 316 | + * @param <T> must extend TypeDefinition |
| 317 | + * |
| 318 | + * @return a map of types |
| 319 | + */ |
| 320 | + public <T extends TypeDefinition> Map<String, T> getTypesMap(Class<T> targetClass) { |
| 321 | + List<T> list = getTypes(targetClass); |
| 322 | + return FpKit.getByName(list, TypeDefinition::getName, FpKit.mergeFirst()); |
| 323 | + } |
| 324 | + |
| 325 | + /** |
| 326 | + * Returns the list of object types that implement the given interface type |
| 327 | + * |
| 328 | + * @param targetInterface the target to search for |
| 329 | + * |
| 330 | + * @return the list of object types that implement the given interface type |
| 331 | + */ |
| 332 | + public List<ObjectTypeDefinition> getImplementationsOf(InterfaceTypeDefinition targetInterface) { |
| 333 | + List<ObjectTypeDefinition> objectTypeDefinitions = getTypes(ObjectTypeDefinition.class); |
| 334 | + return objectTypeDefinitions.stream().filter(objectTypeDefinition -> { |
| 335 | + List<Type> implementsList = objectTypeDefinition.getImplements(); |
| 336 | + for (Type iFace : implementsList) { |
| 337 | + Optional<InterfaceTypeDefinition> interfaceTypeDef = getType(iFace, InterfaceTypeDefinition.class); |
| 338 | + if (interfaceTypeDef.isPresent()) { |
| 339 | + boolean equals = interfaceTypeDef.get().getName().equals(targetInterface.getName()); |
| 340 | + if (equals) { |
| 341 | + return true; |
| 342 | + } |
| 343 | + } |
| 344 | + } |
| 345 | + return false; |
| 346 | + }).collect(Collectors.toList()); |
| 347 | + } |
| 348 | + |
| 349 | + /** |
| 350 | + * Returns true of the abstract type is in implemented by the object type |
| 351 | + * |
| 352 | + * @param abstractType the abstract type to check (interface or union) |
| 353 | + * @param possibleObjectType the object type to check |
| 354 | + * |
| 355 | + * @return true if the object type implements the abstract type |
| 356 | + */ |
| 357 | + @SuppressWarnings("ConstantConditions") |
| 358 | + public boolean isPossibleType(Type abstractType, Type possibleObjectType) { |
| 359 | + if (!isAbstractType(abstractType)) { |
| 360 | + return false; |
| 361 | + } |
| 362 | + if (!isObjectType(possibleObjectType)) { |
| 363 | + return false; |
| 364 | + } |
| 365 | + ObjectTypeDefinition targetObjectTypeDef = getType(possibleObjectType, ObjectTypeDefinition.class).get(); |
| 366 | + TypeDefinition abstractTypeDef = getType(abstractType).get(); |
| 367 | + if (abstractTypeDef instanceof UnionTypeDefinition) { |
| 368 | + List<Type> memberTypes = ((UnionTypeDefinition) abstractTypeDef).getMemberTypes(); |
| 369 | + for (Type memberType : memberTypes) { |
| 370 | + if (getType(memberType, ObjectTypeDefinition.class).isPresent()) { |
| 371 | + return true; |
| 372 | + } |
| 373 | + } |
| 374 | + return false; |
| 375 | + } else { |
| 376 | + InterfaceTypeDefinition iFace = (InterfaceTypeDefinition) abstractTypeDef; |
| 377 | + List<ObjectTypeDefinition> objectTypeDefinitions = getImplementationsOf(iFace); |
| 378 | + return objectTypeDefinitions.stream() |
| 379 | + .anyMatch(od -> od.getName().equals(targetObjectTypeDef.getName())); |
| 380 | + } |
| 381 | + } |
264 | 382 | } |
0 commit comments