Skip to content

Add a @GraphQLIgnore annotation allowing field parameters to be ignored/excluded from a schema #299

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 1 commit into from
Dec 12, 2023
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
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import graphql.annotations.annotationTypes.GraphQLDefaultValue;
import graphql.annotations.annotationTypes.GraphQLDescription;
import graphql.annotations.annotationTypes.GraphQLIgnore;
import graphql.annotations.annotationTypes.GraphQLName;
import graphql.annotations.processor.ProcessingElementsContainer;
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
Expand Down Expand Up @@ -50,6 +51,7 @@ public List<GraphQLArgument> build() {
TypeFunction finalTypeFunction = typeFunction;
List<GraphQLArgument> args = Arrays.stream(method.getParameters()).
filter(p -> !DataFetchingEnvironment.class.isAssignableFrom(p.getType())).
filter(p -> !p.isAnnotationPresent(GraphQLIgnore.class)).
map(parameter -> {
Class<?> t = parameter.getType();
graphql.schema.GraphQLInputType graphQLType = (GraphQLInputType) finalTypeFunction.buildType(true, t, parameter.getAnnotatedType(), container);
Expand Down
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());
}
}