Skip to content

Commit 44bacec

Browse files
committed
added tests for directive on argument + input field
added impl for input directives on fields tests are still not passing
1 parent faa91e2 commit 44bacec

File tree

3 files changed

+87
-9
lines changed

3 files changed

+87
-9
lines changed

src/main/java/graphql/annotations/processor/retrievers/GraphQLFieldRetriever.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,19 @@ public GraphQLInputObjectField getInputField(Method method, ProcessingElementsCo
118118
builder.name(new MethodNameBuilder(method).alwaysPrettify(alwaysPrettify).build());
119119
TypeFunction typeFunction = getTypeFunction(method, container);
120120
GraphQLInputType inputType = (GraphQLInputType) new MethodTypeBuilder(method, typeFunction, container, true).build();
121-
return builder.type(inputType).description(new DescriptionBuilder(method).build()).build();
121+
builder.withDirectives(new DirectivesBuilder(method, container).build());
122+
return (GraphQLInputObjectField) new DirectiveWirer().wire(builder.type(inputType).description(new DescriptionBuilder(method).build()).build(), new DirectiveWiringMapRetriever().getDirectiveWiringMap(method, container));
122123
}
123124

124125
public GraphQLInputObjectField getInputField(Field field, ProcessingElementsContainer container) throws GraphQLAnnotationsException {
125126
GraphQLInputObjectField.Builder builder = newInputObjectField();
126127
builder.name(new FieldNameBuilder(field).alwaysPrettify(alwaysPrettify).build());
127128
TypeFunction typeFunction = getTypeFunction(field, container);
128129
GraphQLType graphQLType = typeFunction.buildType(true, field.getType(), field.getAnnotatedType(), container);
129-
return builder.type((GraphQLInputType) graphQLType).description(new DescriptionBuilder(field).build()).build();
130+
builder.withDirectives(new DirectivesBuilder(field, container).build());
131+
// return builder.type((GraphQLInputType) graphQLType).description(new DescriptionBuilder(field).build()).build();
132+
return (GraphQLInputObjectField) new DirectiveWirer().wire(builder.type((GraphQLInputType) graphQLType).description(new DescriptionBuilder(field).build()).build(),
133+
new DirectiveWiringMapRetriever().getDirectiveWiringMap(field, container));
130134
}
131135

132136
private GraphQLFieldDefinition handleRelayArguments(Method method, ProcessingElementsContainer container, GraphQLFieldDefinition.Builder builder, GraphQLOutputType outputType, List<GraphQLArgument> args) {

src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/ArgumentBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ private GraphQLArgument getArgument(Parameter parameter, graphql.schema.GraphQLI
8181
argumentBuilder.name(toGraphqlName(parameter.getName()));
8282
}
8383
argumentBuilder.withDirectives(new DirectivesBuilder(parameter, container).build());
84-
return (GraphQLArgument) new DirectiveWirer().wire(argumentBuilder.build(), new DirectiveWiringMapRetriever().getDirectiveWiringMap(parameter, container));
84+
return (GraphQLArgument) new DirectiveWirer().wire(argumentBuilder.build(),
85+
new DirectiveWiringMapRetriever().getDirectiveWiringMap(parameter, container));
8586
}
8687

8788
}

src/test/java/graphql/annotations/GraphQLDirectivesTest.java

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2016 Yurii Rashkovskii
3-
*
3+
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -83,6 +83,13 @@ public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment)
8383
}
8484

8585
public static class SuffixWiring implements AnnotationsDirectiveWiring {
86+
@Override
87+
public GraphQLInputObjectField onInputObjectField(AnnotationsWiringEnvironment environment) {
88+
GraphQLInputObjectField field = (GraphQLInputObjectField) environment.getElement();
89+
String suffix = (String) environment.getDirective().getArgument("suffix").getValue();
90+
return field.transform(builder -> builder.name(field.getName()+suffix));
91+
}
92+
8693
@Override
8794
public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment) {
8895
GraphQLFieldDefinition field = (GraphQLFieldDefinition) environment.getElement();
@@ -95,6 +102,20 @@ public GraphQLFieldDefinition onField(AnnotationsWiringEnvironment environment)
95102
})));
96103
return field.transform(builder -> builder.dataFetcher(dataFetcher));
97104
}
105+
106+
@Override
107+
public GraphQLArgument onArgument(AnnotationsWiringEnvironment environment) {
108+
GraphQLArgument element = (GraphQLArgument) environment.getElement();
109+
String suffix = (String) environment.getDirective().getArgument("suffix").getValue();
110+
return element.transform(builder -> builder.name(element.getName() + suffix));
111+
}
112+
113+
@Override
114+
public GraphQLInputObjectType onInputObjectType(AnnotationsWiringEnvironment environment) {
115+
GraphQLInputObjectType element = (GraphQLInputObjectType) environment.getElement();
116+
String suffix = (String) environment.getDirective().getArgument("suffix").getValue();
117+
return element;
118+
}
98119
}
99120

100121
public static class Query {
@@ -128,6 +149,58 @@ public static String name() {
128149
}
129150
}
130151

152+
public static class Query4 {
153+
@GraphQLField
154+
public static String nameWithArgument(@GraphQLDirectives({@Directive(name = "suffix",
155+
wiringClass = SuffixWiring.class, argumentsValues = {"coolSuffixForArg"})})
156+
@GraphQLName("extensionArg") String extensionArg) {
157+
return "yarin" + extensionArg;
158+
}
159+
}
160+
161+
public static class InputObject {
162+
@GraphQLField
163+
@GraphQLDirectives({@Directive(name = "suffix", wiringClass = SuffixWiring.class, argumentsValues = {"coolSuffix"})})
164+
private String a;
165+
166+
@GraphQLField
167+
private int b;
168+
169+
public InputObject(@GraphQLName("a") String a,@GraphQLName("b") int b) {
170+
this.a = a;
171+
this.b = b;
172+
}
173+
}
174+
175+
public static class Query5 {
176+
@GraphQLField
177+
public static String nameWithInputObject(@GraphQLName("inputObject") InputObject input) {
178+
return "yarin";
179+
}
180+
}
181+
182+
@Test
183+
public void queryNameWithInputObject_directivesProvidedToRegistry_wiringOfInputObjectIsActivated() {
184+
GraphQLDirective suffixDirective = GraphQLDirective.newDirective().name("suffix").argument(builder -> builder.name("suffix").type(GraphQLString))
185+
.validLocations(Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION, Introspection.DirectiveLocation.FIELD_DEFINITION).build();
186+
GraphQLObjectType object = GraphQLAnnotations.object(Query5.class, suffixDirective);
187+
GraphQLSchema schema = newSchema().query(object).build();
188+
189+
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { nameWithInputObject" +
190+
"(inputObject: {acoolSuffix: \"magnificent\", b: 5}) }");
191+
assertTrue(result.getErrors().isEmpty());
192+
}
193+
194+
@Test
195+
public void queryNameWithArgument_directivesProvidedToRegistry_wiringOfArgumentIsActivated() {
196+
GraphQLDirective suffixDirective = GraphQLDirective.newDirective().name("suffix").argument(builder -> builder.name("suffix").type(GraphQLString))
197+
.validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION, Introspection.DirectiveLocation.ARGUMENT_DEFINITION).build();
198+
GraphQLObjectType object = GraphQLAnnotations.object(Query4.class, suffixDirective);
199+
GraphQLSchema schema = newSchema().query(object).build();
200+
201+
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { nameWithArgument(extensionArgcoolSuffixForArg: \"ext\") }");
202+
assertTrue(result.getErrors().isEmpty());
203+
}
131204

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

140213
@GraphQLName("upperCase")
141214
@DirectiveLocations(Introspection.DirectiveLocation.FIELD_DEFINITION)
142-
public static class UpperCase{
215+
public static class UpperCase {
143216
boolean isActive;
144217
}
145218

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

0 commit comments

Comments
 (0)