From f05fd3550abab84ad60f35239b6b13ef5396694b Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 15 Oct 2021 17:32:41 +0100 Subject: [PATCH] Polishing in ClassNameTypeResolver --- .../execution/ClassNameTypeResolver.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/ClassNameTypeResolver.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/ClassNameTypeResolver.java index 73c73295..c6881521 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/execution/ClassNameTypeResolver.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/ClassNameTypeResolver.java @@ -29,8 +29,8 @@ import org.springframework.util.Assert; /** * {@link TypeResolver} that tries to find a GraphQL Object type based on the - * class name of an Object. If necessary, it walks up the base class and - * interface hierarchy to find a match. + * class name of a value returned from a {@code DataFetcher}. If necessary, it + * walks up the base class and interface hierarchy to find a match. * * @author Rossen Stoyanchev * @since 1.0.0 @@ -43,10 +43,10 @@ public class ClassNameTypeResolver implements TypeResolver { /** - * Customize how the name of a class, or base class/interface, is determined. + * Customize how the name of a class, or a base class/interface, is determined. * An application can use this to adapt to a common naming convention, e.g. - * removing "Impl" as a suffix, or "Base" as prefix, and so on. - *

By default, this is {@link Class#getSimpleName()}. + * remove an "Impl" suffix or a "Base" prefix, and so on. + *

By default, this is just {@link Class#getSimpleName()}. * @param classNameExtractor the function to use */ public void setClassNameExtractor(Function, String> classNameExtractor) { @@ -55,9 +55,9 @@ public class ClassNameTypeResolver implements TypeResolver { } /** - * Add a mapping from a Java {@link Class} to a GraphQL Object type. The - * given class can be a base class or an interface, in which case the mapping - * applies to sub-classes too. + * Add a mapping from a Java {@link Class} to a GraphQL Object type name. + * The mapping applies to the given type and to all of its sub-classes + * (for a base class) or implementations (for an interface). * @param clazz the Java class to map * @param graphQlTypeName the matching GraphQL object type */ @@ -67,11 +67,16 @@ public class ClassNameTypeResolver implements TypeResolver { @Override + @Nullable public GraphQLObjectType getType(TypeResolutionEnvironment environment) { + Class clazz = environment.getObject().getClass(); - GraphQLObjectType type = getTypeForClass(clazz, environment.getSchema()); - Assert.state(type != null, "No GraphQL Object type for class: " + clazz.getName()); - return type; + GraphQLSchema schema = environment.getSchema(); + + // We don't assert "not null" since GraphQL Java will do that anyway. + // Leaving the method nullable provides option for delegation. + + return getTypeForClass(clazz, schema); } @Nullable