Skip to content

Directives on input #203

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

Merged
merged 12 commits into from
Feb 25, 2019
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 @@ -118,15 +118,18 @@ public GraphQLInputObjectField getInputField(Method method, ProcessingElementsCo
builder.name(new MethodNameBuilder(method).alwaysPrettify(alwaysPrettify).build());
TypeFunction typeFunction = getTypeFunction(method, container);
GraphQLInputType inputType = (GraphQLInputType) new MethodTypeBuilder(method, typeFunction, container, true).build();
return builder.type(inputType).description(new DescriptionBuilder(method).build()).build();
builder.withDirectives(new DirectivesBuilder(method, container).build());
return (GraphQLInputObjectField) new DirectiveWirer().wire(builder.type(inputType).description(new DescriptionBuilder(method).build()).build(), new DirectiveWiringMapRetriever().getDirectiveWiringMap(method, container));
}

public GraphQLInputObjectField getInputField(Field field, ProcessingElementsContainer container) throws GraphQLAnnotationsException {
GraphQLInputObjectField.Builder builder = newInputObjectField();
builder.name(new FieldNameBuilder(field).alwaysPrettify(alwaysPrettify).build());
TypeFunction typeFunction = getTypeFunction(field, container);
GraphQLType graphQLType = typeFunction.buildType(true, field.getType(), field.getAnnotatedType(), container);
return builder.type((GraphQLInputType) graphQLType).description(new DescriptionBuilder(field).build()).build();
builder.withDirectives(new DirectivesBuilder(field, container).build());
return (GraphQLInputObjectField) new DirectiveWirer().wire(builder.type((GraphQLInputType) graphQLType).description(new DescriptionBuilder(field).build()).build(),
new DirectiveWiringMapRetriever().getDirectiveWiringMap(field, container));
}

private GraphQLFieldDefinition handleRelayArguments(Method method, ProcessingElementsContainer container, GraphQLFieldDefinition.Builder builder, GraphQLOutputType outputType, List<GraphQLArgument> args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ private GraphQLArgument getArgument(Parameter parameter, graphql.schema.GraphQLI
argumentBuilder.name(toGraphqlName(parameter.getName()));
}
argumentBuilder.withDirectives(new DirectivesBuilder(parameter, container).build());
return (GraphQLArgument) new DirectiveWirer().wire(argumentBuilder.build(), new DirectiveWiringMapRetriever().getDirectiveWiringMap(parameter, container));
return (GraphQLArgument) new DirectiveWirer().wire(argumentBuilder.build(),
new DirectiveWiringMapRetriever().getDirectiveWiringMap(parameter, container));
}

}
77 changes: 75 additions & 2 deletions src/test/java/graphql/annotations/GraphQLDirectivesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import static graphql.schema.GraphQLDirective.newDirective;
import static graphql.schema.GraphQLSchema.newSchema;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

public class GraphQLDirectivesTest {
Expand Down Expand Up @@ -83,6 +84,13 @@ public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment)
}

public static class SuffixWiring implements AnnotationsDirectiveWiring {
@Override
public GraphQLInputObjectField onInputObjectField(AnnotationsWiringEnvironment environment) {
GraphQLInputObjectField field = (GraphQLInputObjectField) environment.getElement();
String suffix = (String) environment.getDirective().getArgument("suffix").getValue();
return field.transform(builder -> builder.name(field.getName()+suffix));
}

@Override
public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment) {
GraphQLFieldDefinition field = (GraphQLFieldDefinition) environment.getElement();
Expand All @@ -95,6 +103,20 @@ public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment)
})));
return field.transform(builder -> builder.dataFetcher(dataFetcher));
}

@Override
public GraphQLArgument onArgument(AnnotationsWiringEnvironment environment) {
GraphQLArgument element = (GraphQLArgument) environment.getElement();
String suffix = (String) environment.getDirective().getArgument("suffix").getValue();
return element.transform(builder -> builder.name(element.getName() + suffix));
}

@Override
public GraphQLInputObjectType onInputObjectType(AnnotationsWiringEnvironment environment) {
GraphQLInputObjectType element = (GraphQLInputObjectType) environment.getElement();
String suffix = (String) environment.getDirective().getArgument("suffix").getValue();
return element;
}
}

public static class Query {
Expand Down Expand Up @@ -128,6 +150,57 @@ public static String name() {
}
}

public static class Query4 {
@GraphQLField
public static String nameWithArgument(@GraphQLDirectives({@Directive(name = "suffix",
wiringClass = SuffixWiring.class, argumentsValues = {"coolSuffixForArg"})})
@GraphQLName("extensionArg") String extensionArg) {
return "yarin" + extensionArg;
}
}

public static class InputObject {
@GraphQLField
@GraphQLDirectives({@Directive(name = "suffix", wiringClass = SuffixWiring.class, argumentsValues = {"coolSuffix"})})
private String a;

@GraphQLField
private int b;

public InputObject(@GraphQLName("a") String a,@GraphQLName("b") int b) {
this.a = a;
this.b = b;
}
}

public static class Query5 {
@GraphQLField
public static String nameWithInputObject(@GraphQLName("inputObject") InputObject input) {
return "yarin";
}
}

@Test
public void queryNameWithInputObject_directivesProvidedToRegistry_wiringOfInputObjectIsActivated() {
GraphQLDirective suffixDirective = GraphQLDirective.newDirective().name("suffix").argument(builder -> builder.name("suffix").type(GraphQLString))
.validLocations(Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION, Introspection.DirectiveLocation.FIELD_DEFINITION).build();
GraphQLObjectType object = GraphQLAnnotations.object(Query5.class, suffixDirective);
GraphQLSchema schema = newSchema().query(object).build();
GraphQLFieldDefinition nameWithInputObject = schema.getQueryType().getFieldDefinition("nameWithInputObject");
GraphQLInputObjectField field = ((GraphQLInputObjectType) nameWithInputObject.getArgument("inputObject").getType()).getField("acoolSuffix");
assertNotNull(field);
}

@Test
public void queryNameWithArgument_directivesProvidedToRegistry_wiringOfArgumentIsActivated() {
GraphQLDirective suffixDirective = GraphQLDirective.newDirective().name("suffix").argument(builder -> builder.name("suffix").type(GraphQLString))
.validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION, Introspection.DirectiveLocation.ARGUMENT_DEFINITION).build();
GraphQLObjectType object = GraphQLAnnotations.object(Query4.class, suffixDirective);
GraphQLSchema schema = newSchema().query(object).build();

ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { nameWithArgument(extensionArgcoolSuffixForArg: \"ext\") }");
assertTrue(result.getErrors().isEmpty());
}

@Test(expectedExceptions = GraphQLAnnotationsException.class)
public void queryName_noDirectivesProvidedToRegistry_exceptionIsThrown() throws Exception {
Expand All @@ -139,7 +212,7 @@ public void queryName_noDirectivesProvidedToRegistry_exceptionIsThrown() throws

@GraphQLName("upperCase")
@DirectiveLocations(Introspection.DirectiveLocation.FIELD_DEFINITION)
public static class UpperCase{
public static class UpperCase {
boolean isActive;
}

Expand Down Expand Up @@ -193,7 +266,7 @@ public void queryName_chainedDirectives_wiringIsActivatedInCorrectOrder() throws
GraphQLDirective upperCase = newDirective().name("upperCase").argument(builder -> builder.name("isActive").type(GraphQLBoolean).defaultValue(true))
.validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION).build();
GraphQLDirective suffixDirective = GraphQLDirective.newDirective().name("suffix").argument(builder -> builder.name("suffix").type(GraphQLString))
.validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION).build();
.validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION, Introspection.DirectiveLocation.ARGUMENT_DEFINITION).build();
GraphQLObjectType object = GraphQLAnnotations.object(Query3.class, upperCase, suffixDirective);
GraphQLSchema schema = newSchema().query(object).build();

Expand Down