-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an @GraphQLIgnore annotation allowing field parameters to be (#299)
excluded from a schema.
- Loading branch information
1 parent
0d0aa22
commit 74dd8c8
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/graphql/annotations/annotationTypes/GraphQLIgnore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Copyright 2016 Yurii Rashkovskii | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
*/ | ||
package graphql.annotations.annotationTypes; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.PARAMETER}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface GraphQLIgnore { | ||
boolean value() default true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/test/java/graphql/annotations/GraphQLIgnoredFieldParameterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* Copyright 2016 Yurii Rashkovskii | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
*/ | ||
package graphql.annotations; | ||
|
||
import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import graphql.annotations.annotationTypes.GraphQLField; | ||
import graphql.annotations.annotationTypes.GraphQLIgnore; | ||
import graphql.annotations.annotationTypes.GraphQLName; | ||
import graphql.annotations.processor.GraphQLAnnotations; | ||
import graphql.schema.GraphQLObjectType; | ||
import graphql.schema.GraphQLSchema; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
public class GraphQLIgnoredFieldParameterTest { | ||
private AnnotationsSchemaCreator.Builder builder; | ||
private GraphQLAnnotations graphQLAnnotations; | ||
|
||
@BeforeMethod | ||
public void setUp() { | ||
graphQLAnnotations = new GraphQLAnnotations(); | ||
builder = newAnnotationsSchema().setAnnotationsProcessor(graphQLAnnotations); | ||
} | ||
|
||
public static class QueryTest1 { | ||
@GraphQLField | ||
public int foo(@GraphQLIgnore int bar) { | ||
return 5; | ||
} | ||
} | ||
|
||
@Test | ||
public void build_Query1IsProvided_SchemaIsCreatedWithoutIgnoredFieldParameters() { | ||
// arrange + act | ||
GraphQLSchema schema = builder.query(QueryTest1.class).build(); | ||
|
||
// assert | ||
GraphQLObjectType queryType = schema.getQueryType(); | ||
assertThat(queryType.getFieldDefinition("foo"), notNullValue()); | ||
assertThat(queryType.getFieldDefinition("foo").getArguments().size(), is(0)); | ||
} | ||
|
||
public static class QueryTest2 { | ||
@GraphQLField | ||
public int foo(int bar, @GraphQLIgnore String baz, @GraphQLName("quuxAlias") long quux) { | ||
return 5; | ||
} | ||
} | ||
|
||
@Test | ||
public void build_Query2IsProvided_SchemaIsCreatedWithoutIgnoredFieldParameters() { | ||
// arrange + act | ||
GraphQLSchema schema = builder.query(QueryTest2.class).build(); | ||
|
||
// assert | ||
GraphQLObjectType queryType = schema.getQueryType(); | ||
assertThat(queryType.getFieldDefinition("foo"), notNullValue()); | ||
assertThat(queryType.getFieldDefinition("foo").getArguments().size(), is(2)); | ||
assertThat(queryType.getFieldDefinition("foo").getArgument("arg0"), notNullValue()); | ||
assertThat(queryType.getFieldDefinition("foo").getArgument("baz"), nullValue()); | ||
assertThat(queryType.getFieldDefinition("foo").getArgument("quuxAlias"), notNullValue()); | ||
} | ||
|
||
public static class MutationTest1 { | ||
@GraphQLField | ||
public int foo(@GraphQLIgnore int bar) { | ||
return 4; | ||
} | ||
} | ||
|
||
@Test | ||
public void build_Mutation1IsProvided_SchemaIsCreatedWithoutIgnoredFieldParameters() { | ||
// arrange + act | ||
GraphQLSchema schema = builder.query(QueryTest1.class).mutation(MutationTest1.class).build(); | ||
|
||
/// assert | ||
GraphQLObjectType mutation = schema.getMutationType(); | ||
assertThat(mutation.getFieldDefinition("foo"), notNullValue()); | ||
assertThat(mutation.getFieldDefinition("foo").getArguments().size(), is(0)); | ||
} | ||
|
||
public static class MutationTest2 { | ||
@GraphQLField | ||
public int foo(int bar, @GraphQLIgnore String baz, @GraphQLName("quuxAlias") long quux) { | ||
return 4; | ||
} | ||
} | ||
|
||
@Test | ||
public void build_Mutation2IsProvided_SchemaIsCreatedWithoutIgnoredFieldParameters() { | ||
// arrange + act | ||
GraphQLSchema schema = builder.query(QueryTest1.class).mutation(MutationTest2.class).build(); | ||
|
||
/// assert | ||
GraphQLObjectType mutation = schema.getMutationType(); | ||
assertThat(mutation.getFieldDefinition("foo"), notNullValue()); | ||
assertThat(mutation.getFieldDefinition("foo").getArguments().size(), is(2)); | ||
assertThat(mutation.getFieldDefinition("foo").getArgument("arg0"), notNullValue()); | ||
assertThat(mutation.getFieldDefinition("foo").getArgument("baz"), nullValue()); | ||
assertThat(mutation.getFieldDefinition("foo").getArgument("quuxAlias"), notNullValue()); | ||
} | ||
} |