Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use actual graphql name on extending type #301

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
}

}
Loading