ClassNameTypeResolver resolves to concrete types only

See gh-924
This commit is contained in:
rstoyanchev
2024-03-18 16:32:07 +00:00
parent e902b829e5
commit 7e2a671ca5
2 changed files with 28 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import java.util.function.Function;
import graphql.TypeResolutionEnvironment;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLType;
import graphql.schema.TypeResolver;
import org.springframework.lang.Nullable;
@@ -97,8 +98,9 @@ public class ClassNameTypeResolver implements TypeResolver {
}
name = this.classNameExtractor.apply(clazz);
if (schema.containsType(name)) {
return schema.getObjectType(name);
GraphQLType type = schema.getType(name);
if (type instanceof GraphQLObjectType objectType) {
return objectType;
}
for (Class<?> interfaceType : clazz.getInterfaces()) {

View File

@@ -97,6 +97,7 @@ public class ClassNameTypeResolverTests {
graphQlSetup.queryFetcher("animals", env -> animalList).toGraphQlService();
ResponseHelper response = ResponseHelper.forResponse(service.execute(document));
assertThat(response.errorCount()).isEqualTo(0);
Mammal mammal = response.toEntity("animals[0]", Dog.class);
assertThat(mammal.isHerbivore()).isEqualTo(false);
@@ -134,6 +135,7 @@ public class ClassNameTypeResolverTests {
.toGraphQlService();
ResponseHelper response = ResponseHelper.forResponse(service.execute(document));
assertThat(response.errorCount()).isEqualTo(0);
Animal animal = response.toEntity("sightings[0]", GrayWolf.class);
assertThat(animal.getName()).isEqualTo("Gray Wolf");
@@ -142,6 +144,27 @@ public class ClassNameTypeResolverTests {
assertThat(tree.getFamily()).isEqualTo("Redwood");
}
@Test
void javaTypeResolvesToSchemaInterfaceOnly() {
String document = """
query Animals {
animals {
__typename
name
}
}
""";
TestExecutionGraphQlService service =
graphQlSetup.queryFetcher("animals", env -> List.of(new BaseAnimal("Fox"))).toGraphQlService();
ResponseHelper response = ResponseHelper.forResponse(service.execute(document));
assertThat(response.errorCount()).isEqualTo(1);
assertThat(response.error(0).message()).contains("Could not determine the exact type of 'Animal'");
}
interface Animal {