Skip to content

Commit 74dd8c8

Browse files
authored
Add an @GraphQLIgnore annotation allowing field parameters to be (#299)
excluded from a schema.
1 parent 0d0aa22 commit 74dd8c8

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
*/
15+
package graphql.annotations.annotationTypes;
16+
17+
import java.lang.annotation.ElementType;
18+
import java.lang.annotation.Retention;
19+
import java.lang.annotation.RetentionPolicy;
20+
import java.lang.annotation.Target;
21+
22+
@Target({ElementType.PARAMETER})
23+
@Retention(RetentionPolicy.RUNTIME)
24+
public @interface GraphQLIgnore {
25+
boolean value() default true;
26+
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import graphql.annotations.annotationTypes.GraphQLDefaultValue;
1616
import graphql.annotations.annotationTypes.GraphQLDescription;
17+
import graphql.annotations.annotationTypes.GraphQLIgnore;
1718
import graphql.annotations.annotationTypes.GraphQLName;
1819
import graphql.annotations.processor.ProcessingElementsContainer;
1920
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
@@ -50,6 +51,7 @@ public List<GraphQLArgument> build() {
5051
TypeFunction finalTypeFunction = typeFunction;
5152
List<GraphQLArgument> args = Arrays.stream(method.getParameters()).
5253
filter(p -> !DataFetchingEnvironment.class.isAssignableFrom(p.getType())).
54+
filter(p -> !p.isAnnotationPresent(GraphQLIgnore.class)).
5355
map(parameter -> {
5456
Class<?> t = parameter.getType();
5557
graphql.schema.GraphQLInputType graphQLType = (GraphQLInputType) finalTypeFunction.buildType(true, t, parameter.getAnnotatedType(), container);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
*/
15+
package graphql.annotations;
16+
17+
import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema;
18+
import static org.hamcrest.CoreMatchers.is;
19+
import static org.hamcrest.CoreMatchers.notNullValue;
20+
import static org.hamcrest.CoreMatchers.nullValue;
21+
import static org.hamcrest.MatcherAssert.assertThat;
22+
23+
import graphql.annotations.annotationTypes.GraphQLField;
24+
import graphql.annotations.annotationTypes.GraphQLIgnore;
25+
import graphql.annotations.annotationTypes.GraphQLName;
26+
import graphql.annotations.processor.GraphQLAnnotations;
27+
import graphql.schema.GraphQLObjectType;
28+
import graphql.schema.GraphQLSchema;
29+
import org.testng.annotations.BeforeMethod;
30+
import org.testng.annotations.Test;
31+
32+
public class GraphQLIgnoredFieldParameterTest {
33+
private AnnotationsSchemaCreator.Builder builder;
34+
private GraphQLAnnotations graphQLAnnotations;
35+
36+
@BeforeMethod
37+
public void setUp() {
38+
graphQLAnnotations = new GraphQLAnnotations();
39+
builder = newAnnotationsSchema().setAnnotationsProcessor(graphQLAnnotations);
40+
}
41+
42+
public static class QueryTest1 {
43+
@GraphQLField
44+
public int foo(@GraphQLIgnore int bar) {
45+
return 5;
46+
}
47+
}
48+
49+
@Test
50+
public void build_Query1IsProvided_SchemaIsCreatedWithoutIgnoredFieldParameters() {
51+
// arrange + act
52+
GraphQLSchema schema = builder.query(QueryTest1.class).build();
53+
54+
// assert
55+
GraphQLObjectType queryType = schema.getQueryType();
56+
assertThat(queryType.getFieldDefinition("foo"), notNullValue());
57+
assertThat(queryType.getFieldDefinition("foo").getArguments().size(), is(0));
58+
}
59+
60+
public static class QueryTest2 {
61+
@GraphQLField
62+
public int foo(int bar, @GraphQLIgnore String baz, @GraphQLName("quuxAlias") long quux) {
63+
return 5;
64+
}
65+
}
66+
67+
@Test
68+
public void build_Query2IsProvided_SchemaIsCreatedWithoutIgnoredFieldParameters() {
69+
// arrange + act
70+
GraphQLSchema schema = builder.query(QueryTest2.class).build();
71+
72+
// assert
73+
GraphQLObjectType queryType = schema.getQueryType();
74+
assertThat(queryType.getFieldDefinition("foo"), notNullValue());
75+
assertThat(queryType.getFieldDefinition("foo").getArguments().size(), is(2));
76+
assertThat(queryType.getFieldDefinition("foo").getArgument("arg0"), notNullValue());
77+
assertThat(queryType.getFieldDefinition("foo").getArgument("baz"), nullValue());
78+
assertThat(queryType.getFieldDefinition("foo").getArgument("quuxAlias"), notNullValue());
79+
}
80+
81+
public static class MutationTest1 {
82+
@GraphQLField
83+
public int foo(@GraphQLIgnore int bar) {
84+
return 4;
85+
}
86+
}
87+
88+
@Test
89+
public void build_Mutation1IsProvided_SchemaIsCreatedWithoutIgnoredFieldParameters() {
90+
// arrange + act
91+
GraphQLSchema schema = builder.query(QueryTest1.class).mutation(MutationTest1.class).build();
92+
93+
/// assert
94+
GraphQLObjectType mutation = schema.getMutationType();
95+
assertThat(mutation.getFieldDefinition("foo"), notNullValue());
96+
assertThat(mutation.getFieldDefinition("foo").getArguments().size(), is(0));
97+
}
98+
99+
public static class MutationTest2 {
100+
@GraphQLField
101+
public int foo(int bar, @GraphQLIgnore String baz, @GraphQLName("quuxAlias") long quux) {
102+
return 4;
103+
}
104+
}
105+
106+
@Test
107+
public void build_Mutation2IsProvided_SchemaIsCreatedWithoutIgnoredFieldParameters() {
108+
// arrange + act
109+
GraphQLSchema schema = builder.query(QueryTest1.class).mutation(MutationTest2.class).build();
110+
111+
/// assert
112+
GraphQLObjectType mutation = schema.getMutationType();
113+
assertThat(mutation.getFieldDefinition("foo"), notNullValue());
114+
assertThat(mutation.getFieldDefinition("foo").getArguments().size(), is(2));
115+
assertThat(mutation.getFieldDefinition("foo").getArgument("arg0"), notNullValue());
116+
assertThat(mutation.getFieldDefinition("foo").getArgument("baz"), nullValue());
117+
assertThat(mutation.getFieldDefinition("foo").getArgument("quuxAlias"), notNullValue());
118+
}
119+
}

0 commit comments

Comments
 (0)