Skip to content

Commit

Permalink
Use actual graphql name on extending type (#301)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
geichelberger authored Jun 10, 2024
1 parent 95ef03c commit 2e0f87a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,23 @@ public class GraphQLExtensionsHandler {

public List<GraphQLFieldDefinition> getExtensionFields(Class<?> object, List<String> definedFields, ProcessingElementsContainer container) throws CannotCastMemberException {
List<GraphQLFieldDefinition> 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)) {
if (method.isBridge() || method.isSynthetic()) {
continue;
}
if (methodSearchAlgorithm.isFound(method)) {
addExtensionField(fieldRetriever.getField(object.getSimpleName(), method, container), fields, definedFields);
addExtensionField(fieldRetriever.getField(typeName, method, container), fields, definedFields);
}
}
for (Field field : getAllFields(aClass).values()) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
if (fieldSearchAlgorithm.isFound(field)) {
addExtensionField(fieldRetriever.getField(object.getSimpleName(), field, container), fields, definedFields);
addExtensionField(fieldRetriever.getField(typeName, field, container), fields, definedFields);
}
}
}
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/graphql/annotations/GraphQLExtensionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,31 @@ public Object get(DataFetchingEnvironment environment) {
}
}

@GraphQLDescription("Object with different GraphQL name")
@GraphQLName("DifferentObject")
public static class DifferentNameTestObject {
@GraphQLField
public String field() {
return "different";
}

}

@GraphQLTypeExtension(GraphQLExtensionsTest.DifferentNameTestObject.class)
public static class DifferentNameTestObjectExtension {
private final DifferentNameTestObject obj;

public DifferentNameTestObjectExtension(DifferentNameTestObject obj) {
this.obj = obj;
}

@GraphQLField
public String field2() {
return obj.field() + " field2";
}

}

@Test
public void fields() {
GraphQLAnnotations instance = new GraphQLAnnotations();
Expand All @@ -113,6 +138,24 @@ public void fields() {
assertEquals(fields.get(2).getType(), GraphQLString);
}

@Test
public void fieldsDifferentGraphQLName() {
GraphQLAnnotations instance = new GraphQLAnnotations();
instance.registerTypeExtension(DifferentNameTestObjectExtension.class);
GraphQLObjectHandler graphQLObjectHandler = instance.getObjectHandler();
GraphQLObjectType object = graphQLObjectHandler.getGraphQLType(GraphQLExtensionsTest.DifferentNameTestObject.class, instance.getContainer());

List<GraphQLFieldDefinition> 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();
Expand All @@ -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<String, Object> data = result.getData();
assertEquals(data.get("field"), "different");
assertEquals(data.get("field2"), "different field2");
}

@Test
public void testDuplicateField() {
GraphQLAnnotations instance = new GraphQLAnnotations();
Expand All @@ -135,4 +191,5 @@ public void testDuplicateField() {
GraphQLAnnotationsException e = expectThrows(GraphQLAnnotationsException.class, () -> graphQLObjectHandler.getGraphQLType(TestObject.class, instance.getContainer()));
assertTrue(e.getMessage().startsWith("Duplicate field"));
}

}

0 comments on commit 2e0f87a

Please sign in to comment.