Skip to content
Open
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 @@ -20,6 +20,7 @@
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
import graphql.annotations.processor.searchAlgorithms.SearchAlgorithm;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLInputObjectField;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferencePolicy;
Expand Down Expand Up @@ -77,6 +78,41 @@ private void addExtensionField(GraphQLFieldDefinition gqlField, List<GraphQLFiel
}
}

public List<GraphQLInputObjectField> getExtensionInputFields(Class<?> object, List<String> definedFields, ProcessingElementsContainer container) throws CannotCastMemberException {
List<GraphQLInputObjectField> fields = new ArrayList<>();
String typeName = container.getInputPrefix() + graphQLObjectInfoRetriever.getTypeName(object) + container.getInputSuffix();
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)) {
addExtensionInputField(fieldRetriever.getInputField(method, container, typeName), fields, definedFields);
}
}
for (Field field : getAllFields(aClass).values()) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
if (fieldSearchAlgorithm.isFound(field)) {
addExtensionInputField(fieldRetriever.getInputField(field, container, typeName), fields, definedFields);
}
}
}
}
return fields;
}

private void addExtensionInputField(GraphQLInputObjectField gqlField, List<GraphQLInputObjectField> fields, List<String> definedFields) {
if (!definedFields.contains(gqlField.getName())) {
definedFields.add(gqlField.getName());
fields.add(gqlField);
} else {
throw new GraphQLAnnotationsException("Duplicate field found in extension : " + gqlField.getName(), null);
}
}


public void registerTypeExtension(Class<?> objectClass, ProcessingElementsContainer container) {
GraphQLTypeExtension typeExtension = objectClass.getAnnotation(GraphQLTypeExtension.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public GraphQLType getGraphQLType(Class<?> object, ProcessingElementsContainer c
} else {
if (isInput) {
type = new InputObjectBuilder(graphQLObjectInfoRetriever, fieldSearchAlgorithm, methodSearchAlgorithm,
graphQLFieldRetriever).getInputObjectBuilder(object, container).build();
graphQLFieldRetriever, extensionsHandler).getInputObjectBuilder(object, container).build();
} else {
type = new OutputObjectBuilder(graphQLObjectInfoRetriever, fieldSearchAlgorithm, methodSearchAlgorithm,
graphQLFieldRetriever, graphQLInterfaceRetriever, extensionsHandler).getOutputObjectBuilder(object, container).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import graphql.annotations.annotationTypes.GraphQLDescription;
import graphql.annotations.processor.ProcessingElementsContainer;
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
import graphql.annotations.processor.retrievers.GraphQLExtensionsHandler;
import graphql.annotations.processor.retrievers.GraphQLFieldRetriever;
import graphql.annotations.processor.retrievers.GraphQLObjectInfoRetriever;
import graphql.annotations.processor.searchAlgorithms.SearchAlgorithm;
Expand All @@ -36,12 +37,14 @@ public class InputObjectBuilder {
private SearchAlgorithm fieldSearchAlgorithm;
private SearchAlgorithm methodSearchAlgorithm;
private GraphQLFieldRetriever graphQLFieldRetriever;
private GraphQLExtensionsHandler extensionsHandler;

public InputObjectBuilder(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever, SearchAlgorithm fieldSearchAlgorithm, SearchAlgorithm methodSearchAlgorithm, GraphQLFieldRetriever graphQLFieldRetriever) {
public InputObjectBuilder(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever, SearchAlgorithm fieldSearchAlgorithm, SearchAlgorithm methodSearchAlgorithm, GraphQLFieldRetriever graphQLFieldRetriever, GraphQLExtensionsHandler extensionsHandler) {
this.graphQLObjectInfoRetriever = graphQLObjectInfoRetriever;
this.methodSearchAlgorithm = methodSearchAlgorithm;
this.fieldSearchAlgorithm = fieldSearchAlgorithm;
this.graphQLFieldRetriever = graphQLFieldRetriever;
this.extensionsHandler = extensionsHandler;
}

/**
Expand Down Expand Up @@ -85,6 +88,9 @@ public GraphQLInputObjectType.Builder getInputObjectBuilder(Class<?> object, Pro
builder.field(gqlField);
}
}

builder.fields(extensionsHandler.getExtensionInputFields(object, definedFields, container));

return builder;
}

Expand Down
32 changes: 32 additions & 0 deletions src/test/java/graphql/annotations/GraphQLExtensionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,36 @@ public void testDuplicateField() {
assertTrue(e.getMessage().startsWith("Duplicate field"));
}

public static class TestInput {
@GraphQLField
public String field;
}

@GraphQLTypeExtension(GraphQLExtensionsTest.TestInput.class)
public static class TestInputExtension {
@GraphQLField
public String field2;
}

public static class QueryWithInput {
@GraphQLField
public String echo(TestInput input) {
return input.field;
}
}

@Test
public void inputFields() {
GraphQLSchema schema = newAnnotationsSchema().query(QueryWithInput.class).typeExtension(TestInputExtension.class).build();
GraphQLInputObjectType object = (GraphQLInputObjectType) schema.getType("InputTestInput");

List<GraphQLInputObjectField> fields = object.getFields();
assertEquals(fields.size(), 2);

fields = ImmutableList.sortedCopyOf(Comparator.comparing(GraphQLInputObjectField::getName), fields);
assertEquals(fields.get(0).getName(), "field");
assertEquals(fields.get(1).getName(), "field2");
assertEquals(fields.get(1).getType(), GraphQLString);
}

}
16 changes: 13 additions & 3 deletions src/test/java/graphql/annotations/GraphQLObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import graphql.annotations.annotationTypes.GraphQLNonNull;
import graphql.annotations.processor.GraphQLAnnotations;
import graphql.annotations.processor.ProcessingElementsContainer;
import graphql.annotations.processor.retrievers.GraphQLExtensionsHandler;
import graphql.annotations.processor.retrievers.GraphQLFieldRetriever;
import graphql.annotations.processor.retrievers.GraphQLObjectInfoRetriever;
import graphql.annotations.processor.searchAlgorithms.BreadthFirstSearch;
Expand Down Expand Up @@ -59,6 +60,15 @@ public void init() {
this.graphQLAnnotations = new GraphQLAnnotations();
}

private static GraphQLExtensionsHandler newExtensionsHandler(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever) {
GraphQLExtensionsHandler extensionsHandler = new GraphQLExtensionsHandler();
extensionsHandler.setGraphQLObjectInfoRetriever(graphQLObjectInfoRetriever);
extensionsHandler.setFieldSearchAlgorithm(new ParentalSearch(graphQLObjectInfoRetriever));
extensionsHandler.setMethodSearchAlgorithm(new BreadthFirstSearch(graphQLObjectInfoRetriever));
extensionsHandler.setFieldRetriever(new GraphQLFieldRetriever());
return extensionsHandler;
}

public static class DefaultAValue implements Supplier<Object> {

@Override
Expand Down Expand Up @@ -723,7 +733,7 @@ public void complexInputObjectArgument() {
public void inputObject() {
GraphQLObjectInfoRetriever graphQLObjectInfoRetriever = new GraphQLObjectInfoRetriever();
GraphQLInputObjectType type = new InputObjectBuilder(graphQLObjectInfoRetriever, new ParentalSearch(graphQLObjectInfoRetriever),
new BreadthFirstSearch(graphQLObjectInfoRetriever), new GraphQLFieldRetriever()).
new BreadthFirstSearch(graphQLObjectInfoRetriever), new GraphQLFieldRetriever(), newExtensionsHandler(graphQLObjectInfoRetriever)).
getInputObjectBuilder(InputObject.class, this.graphQLAnnotations.getContainer()).build();

assertEquals(type.getName(), DEFAULT_INPUT_PREFIX + InputObject.class.getSimpleName(), "Type name prefix did not match expected value");
Expand All @@ -737,7 +747,7 @@ public void inputObjectCustomPrefixes() {
container.setInputPrefix("");
container.setInputSuffix("Input");
GraphQLInputObjectType type = new InputObjectBuilder(graphQLObjectInfoRetriever, new ParentalSearch(graphQLObjectInfoRetriever),
new BreadthFirstSearch(graphQLObjectInfoRetriever), new GraphQLFieldRetriever()).
new BreadthFirstSearch(graphQLObjectInfoRetriever), new GraphQLFieldRetriever(), newExtensionsHandler(graphQLObjectInfoRetriever)).
getInputObjectBuilder(InputObject.class, this.graphQLAnnotations.getContainer()).build();

assertEquals(type.getName(), "" + InputObject.class.getSimpleName() + "Input", "Type name prefix did not match expected value");
Expand Down Expand Up @@ -808,7 +818,7 @@ public void optionalInput() {
GraphQLObjectType object = this.graphQLAnnotations.object(OptionalTest.class);
GraphQLObjectInfoRetriever graphQLObjectInfoRetriever = new GraphQLObjectInfoRetriever();
GraphQLInputObjectType inputObject = new InputObjectBuilder(graphQLObjectInfoRetriever, new ParentalSearch(graphQLObjectInfoRetriever),
new BreadthFirstSearch(graphQLObjectInfoRetriever), new GraphQLFieldRetriever()).
new BreadthFirstSearch(graphQLObjectInfoRetriever), new GraphQLFieldRetriever(), newExtensionsHandler(graphQLObjectInfoRetriever)).
getInputObjectBuilder(OptionalTest.class, this.graphQLAnnotations.getContainer()).build();

GraphQLObjectType mutation = GraphQLObjectType.newObject().name("mut").field(newFieldDefinition().name("test").type(object).
Expand Down
Loading