From 2e0f87a7392e825f59d0162bd870699dd191c99b Mon Sep 17 00:00:00 2001 From: geichelberger <35195803+geichelberger@users.noreply.github.com> Date: Mon, 10 Jun 2024 11:15:18 +0200 Subject: [PATCH] Use actual graphql name on extending type (#301) * Use actual graphql name on extending type If a type is extended via a GraphQLTypeExtension annotation, the simple class name is used instead of checking whether a GraphQLName annotation is available; if the GraphQL name differs from the class name, the field cannot be fetched. * Add different graphql name extension test case --- .../retrievers/GraphQLExtensionsHandler.java | 5 +- .../annotations/GraphQLExtensionsTest.java | 57 +++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/main/java/graphql/annotations/processor/retrievers/GraphQLExtensionsHandler.java b/src/main/java/graphql/annotations/processor/retrievers/GraphQLExtensionsHandler.java index 19363971..a2f49c65 100644 --- a/src/main/java/graphql/annotations/processor/retrievers/GraphQLExtensionsHandler.java +++ b/src/main/java/graphql/annotations/processor/retrievers/GraphQLExtensionsHandler.java @@ -44,6 +44,7 @@ public class GraphQLExtensionsHandler { public List getExtensionFields(Class object, List definedFields, ProcessingElementsContainer container) throws CannotCastMemberException { List fields = new ArrayList<>(); + String typeName = graphQLObjectInfoRetriever.getTypeName(object); if (container.getExtensionsTypeRegistry().containsKey(object)) { for (Class aClass : container.getExtensionsTypeRegistry().get(object)) { for (Method method : graphQLObjectInfoRetriever.getOrderedMethods(aClass)) { @@ -51,7 +52,7 @@ public List getExtensionFields(Class object, List getExtensionFields(Class object, List fields = object.getFieldDefinitions(); + assertEquals(fields.size(), 2); + + fields = ImmutableList.sortedCopyOf(Comparator.comparing(GraphQLFieldDefinition::getName), fields); + + assertEquals(fields.get(0).getName(), "field"); + assertEquals(fields.get(1).getName(), "field2"); + assertEquals(fields.get(0).getType(), GraphQLString); + assertEquals(fields.get(1).getType(), GraphQLString); + } + @Test public void values() { GraphQLSchema schema = newAnnotationsSchema().query(TestObject.class).typeExtension(TestObjectExtension.class).build(); @@ -127,6 +170,19 @@ public void values() { assertEquals(data.get("field5"), "test test5"); } + @Test + public void valuesDifferentGraphQLName() { + GraphQLSchema schema = newAnnotationsSchema().query(DifferentNameTestObject.class).typeExtension(DifferentNameTestObjectExtension.class).build(); + + assertTrue(schema.getCodeRegistry().hasDataFetcher(FieldCoordinates.coordinates("DifferentObject", "field2"))); + + ExecutionResult result = GraphQL.newGraphQL( schema ).build().execute( + GraphQLHelper.createExecutionInput( "{field field2}", new GraphQLExtensionsTest.DifferentNameTestObject() ) ); + Map data = result.getData(); + assertEquals(data.get("field"), "different"); + assertEquals(data.get("field2"), "different field2"); + } + @Test public void testDuplicateField() { GraphQLAnnotations instance = new GraphQLAnnotations(); @@ -135,4 +191,5 @@ public void testDuplicateField() { GraphQLAnnotationsException e = expectThrows(GraphQLAnnotationsException.class, () -> graphQLObjectHandler.getGraphQLType(TestObject.class, instance.getContainer())); assertTrue(e.getMessage().startsWith("Duplicate field")); } + }