Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jbellenger committed Apr 2, 2024
1 parent 897d8d4 commit d85046e
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 49 deletions.
10 changes: 4 additions & 6 deletions src/main/java/graphql/schema/GraphQLDirective.java
Expand Up @@ -7,15 +7,13 @@
import graphql.util.TraversalControl;
import graphql.util.TraverserContext;

import java.util.Collections;
import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;

import static graphql.Assert.*;
import static graphql.Assert.assertNotEmpty;
import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertValidName;
import static graphql.introspection.Introspection.DirectiveLocation;
import static graphql.util.FpKit.getByName;

Expand Down
32 changes: 27 additions & 5 deletions src/test/groovy/graphql/TestUtil.groovy
Expand Up @@ -3,10 +3,32 @@ package graphql
import graphql.execution.MergedField
import graphql.execution.MergedSelectionSet
import graphql.introspection.Introspection.DirectiveLocation
import graphql.language.*
import graphql.language.Document
import graphql.language.Field
import graphql.language.NullValue
import graphql.language.ObjectTypeDefinition
import graphql.language.OperationDefinition
import graphql.language.ScalarTypeDefinition
import graphql.language.Type
import graphql.parser.Parser
import graphql.schema.*
import graphql.schema.idl.*
import graphql.schema.Coercing
import graphql.schema.DataFetcher
import graphql.schema.GraphQLAppliedDirective
import graphql.schema.GraphQLAppliedDirectiveArgument
import graphql.schema.GraphQLArgument
import graphql.schema.GraphQLDirective
import graphql.schema.GraphQLInputType
import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLScalarType
import graphql.schema.GraphQLSchema
import graphql.schema.GraphQLType
import graphql.schema.TypeResolver
import graphql.schema.idl.RuntimeWiring
import graphql.schema.idl.SchemaGenerator
import graphql.schema.idl.SchemaParser
import graphql.schema.idl.TestMockedWiringFactory
import graphql.schema.idl.TypeRuntimeWiring
import graphql.schema.idl.WiringFactory
import graphql.schema.idl.errors.SchemaProblem
import groovy.json.JsonOutput

Expand Down Expand Up @@ -176,13 +198,13 @@ class TestUtil {
.replaceDirectives(
definition.getDirectives()
.stream()
.map({ mockDirective(it.getName(), DirectiveLocation.SCALAR) })
.map({ mkDirective(it.getName(), DirectiveLocation.SCALAR) })
.collect(Collectors.toList()))
.definition(definition)
.build()
}

static GraphQLDirective mockDirective(String name, DirectiveLocation location, GraphQLArgument arg = null) {
static GraphQLDirective mkDirective(String name, DirectiveLocation location, GraphQLArgument arg = null) {
def b = newDirective().name(name).description(name).validLocation(location)
if (arg != null) b.argument(arg)
b.build()
Expand Down
20 changes: 10 additions & 10 deletions src/test/groovy/graphql/schema/GraphQLArgumentTest.groovy
Expand Up @@ -13,7 +13,7 @@ import static graphql.schema.GraphQLArgument.newArgument
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition
import static graphql.schema.GraphQLObjectType.newObject
import static graphql.schema.GraphQLSchema.newSchema
import static graphql.TestUtil.mockDirective
import static graphql.TestUtil.mkDirective

class GraphQLArgumentTest extends Specification {

Expand All @@ -23,15 +23,15 @@ class GraphQLArgumentTest extends Specification {
.description("A1_description")
.type(GraphQLInt)
.deprecate("custom reason")
.withDirective(mockDirective("directive1", ARGUMENT_DEFINITION))
.withDirective(mkDirective("directive1", ARGUMENT_DEFINITION))
.build()
when:
def transformedArgument = startingArgument.transform({
it
.name("A2")
.description("A2_description")
.type(GraphQLString)
.withDirective(mockDirective("directive3", ARGUMENT_DEFINITION))
.withDirective(mkDirective("directive3", ARGUMENT_DEFINITION))
.value("VALUE") // Retain deprecated for test coverage
.deprecate(null)
.defaultValue("DEFAULT") // Retain deprecated for test coverage
Expand Down Expand Up @@ -82,7 +82,7 @@ class GraphQLArgumentTest extends Specification {
given:
def builder = newArgument().name("A1")
.type(GraphQLInt)
.withDirective(mockDirective("directive1", ARGUMENT_DEFINITION))
.withDirective(mkDirective("directive1", ARGUMENT_DEFINITION))

when:
argument = builder.build()
Expand All @@ -97,8 +97,8 @@ class GraphQLArgumentTest extends Specification {
when:
argument = builder
.clearDirectives()
.withDirective(mockDirective("directive2", ARGUMENT_DEFINITION))
.withDirective(mockDirective("directive3", ARGUMENT_DEFINITION))
.withDirective(mkDirective("directive2", ARGUMENT_DEFINITION))
.withDirective(mkDirective("directive3", ARGUMENT_DEFINITION))
.build()

then:
Expand All @@ -110,9 +110,9 @@ class GraphQLArgumentTest extends Specification {
when:
argument = builder
.replaceDirectives([
mockDirective("directive1", ARGUMENT_DEFINITION),
mockDirective("directive2", ARGUMENT_DEFINITION),
mockDirective("directive3", ARGUMENT_DEFINITION)]) // overwrite
mkDirective("directive1", ARGUMENT_DEFINITION),
mkDirective("directive2", ARGUMENT_DEFINITION),
mkDirective("directive3", ARGUMENT_DEFINITION)]) // overwrite
.build()

then:
Expand Down Expand Up @@ -199,7 +199,7 @@ class GraphQLArgumentTest extends Specification {
def "Applied schema directives arguments are validated for programmatic schemas"() {
given:
def arg = newArgument().name("arg").type(GraphQLInt).valueProgrammatic(ImmutableKit.emptyMap()).build() // Retain for test coverage
def directive = mockDirective("cached", ARGUMENT_DEFINITION, arg)
def directive = mkDirective("cached", ARGUMENT_DEFINITION, arg)
def field = newFieldDefinition()
.name("hello")
.type(GraphQLString)
Expand Down
Expand Up @@ -4,22 +4,22 @@ import static graphql.introspection.Introspection.DirectiveLocation
import spock.lang.Specification

import static graphql.schema.GraphQLEnumValueDefinition.newEnumValueDefinition
import static graphql.TestUtil.mockDirective
import static graphql.TestUtil.mkDirective

class GraphQLEnumValueDefinitionTest extends Specification {
def "object can be transformed"() {
given:
def startEnumValue = newEnumValueDefinition().name("EV1")
.description("EV1_description")
.value("A")
.withDirective(mockDirective("directive1", DirectiveLocation.ENUM_VALUE))
.withDirective(mkDirective("directive1", DirectiveLocation.ENUM_VALUE))
.build()
when:
def transformedEnumValue = startEnumValue.transform({
it
.name("EV2")
.value("X")
.withDirective(mockDirective("directive2", DirectiveLocation.ENUM_VALUE))
.withDirective(mkDirective("directive2", DirectiveLocation.ENUM_VALUE))
})

then:
Expand Down
Expand Up @@ -11,7 +11,7 @@ import static graphql.Scalars.GraphQLFloat
import static graphql.Scalars.GraphQLInt
import static graphql.Scalars.GraphQLString
import static graphql.TestUtil.mockArguments
import static graphql.TestUtil.mockDirective
import static graphql.TestUtil.mkDirective
import static graphql.schema.DefaultGraphqlTypeComparatorRegistry.newComparators
import static graphql.schema.GraphQLArgument.newArgument
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition
Expand All @@ -36,8 +36,8 @@ class GraphQLFieldDefinitionTest extends Specification {
.deprecate("F1_deprecated")
.argument(newArgument().name("argStr").type(GraphQLString))
.argument(newArgument().name("argInt").type(GraphQLInt))
.withDirective(mockDirective("directive1", Introspection.DirectiveLocation.FIELD_DEFINITION))
.withDirective(mockDirective("directive2", Introspection.DirectiveLocation.FIELD_DEFINITION))
.withDirective(mkDirective("directive1", Introspection.DirectiveLocation.FIELD_DEFINITION))
.withDirective(mkDirective("directive2", Introspection.DirectiveLocation.FIELD_DEFINITION))
.build()

when:
Expand All @@ -48,7 +48,7 @@ class GraphQLFieldDefinitionTest extends Specification {
.argument(newArgument().name("argStr").type(GraphQLString))
.argument(newArgument().name("argInt").type(GraphQLBoolean))
.argument(newArgument().name("argIntAdded").type(GraphQLInt))
.withDirective(mockDirective("directive3", Introspection.DirectiveLocation.FIELD_DEFINITION))
.withDirective(mkDirective("directive3", Introspection.DirectiveLocation.FIELD_DEFINITION))
})

then:
Expand Down
Expand Up @@ -7,7 +7,7 @@ import spock.lang.Specification
import static graphql.Scalars.GraphQLFloat
import static graphql.Scalars.GraphQLInt
import static graphql.schema.GraphQLInputObjectField.newInputObjectField
import static graphql.TestUtil.mockDirective
import static graphql.TestUtil.mkDirective

class GraphQLInputObjectFieldTest extends Specification {

Expand All @@ -17,8 +17,8 @@ class GraphQLInputObjectFieldTest extends Specification {
.name("F1")
.type(GraphQLFloat)
.description("F1_description")
.withDirective(mockDirective("directive1", Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION))
.withDirective(mockDirective("directive2", Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION))
.withDirective(mkDirective("directive1", Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION))
.withDirective(mkDirective("directive2", Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION))
.deprecate("No longer useful")
.build()

Expand All @@ -27,7 +27,7 @@ class GraphQLInputObjectFieldTest extends Specification {
builder.name("F2")
.type(GraphQLInt)
.deprecate(null)
.withDirective(mockDirective("directive3", Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION))
.withDirective(mkDirective("directive3", Introspection.DirectiveLocation.INPUT_FIELD_DEFINITION))
})

then:
Expand Down
8 changes: 4 additions & 4 deletions src/test/groovy/graphql/schema/GraphQLScalarTypeTest.groovy
Expand Up @@ -3,7 +3,7 @@ package graphql.schema
import graphql.introspection.Introspection
import spock.lang.Specification

import static graphql.TestUtil.mockDirective
import static graphql.TestUtil.mkDirective

class GraphQLScalarTypeTest extends Specification {
Coercing<String, String> coercing = new Coercing<String, String>() {
Expand All @@ -29,14 +29,14 @@ class GraphQLScalarTypeTest extends Specification {
.name("S1")
.description("S1_description")
.coercing(coercing)
.withDirective(mockDirective("directive1", Introspection.DirectiveLocation.SCALAR))
.withDirective(mockDirective("directive2", Introspection.DirectiveLocation.SCALAR))
.withDirective(mkDirective("directive1", Introspection.DirectiveLocation.SCALAR))
.withDirective(mkDirective("directive2", Introspection.DirectiveLocation.SCALAR))
.build()
when:
def transformedScalar = startingScalar.transform({ builder ->
builder.name("S2")
.description("S2_description")
.withDirective(mockDirective("directive3", Introspection.DirectiveLocation.SCALAR))
.withDirective(mkDirective("directive3", Introspection.DirectiveLocation.SCALAR))
})

then:
Expand Down
22 changes: 11 additions & 11 deletions src/test/groovy/graphql/schema/SchemaTraverserTest.groovy
Expand Up @@ -10,7 +10,7 @@ import static graphql.introspection.Introspection.DirectiveLocation
import static graphql.schema.GraphQLArgument.newArgument
import static graphql.schema.GraphQLTypeReference.typeRef
import static graphql.schema.GraphqlTypeComparatorRegistry.BY_NAME_REGISTRY
import static graphql.TestUtil.mockDirective
import static graphql.TestUtil.mkDirective

class SchemaTraverserTest extends Specification {

Expand Down Expand Up @@ -202,7 +202,7 @@ class SchemaTraverserTest extends Specification {
def scalarType = GraphQLScalarType.newScalar()
.name("foo")
.coercing(coercing)
.withDirective(mockDirective("bar", DirectiveLocation.SCALAR))
.withDirective(mkDirective("bar", DirectiveLocation.SCALAR))
.withAppliedDirective(GraphQLAppliedDirective.newDirective()
.name("barApplied"))
.build()
Expand All @@ -218,7 +218,7 @@ class SchemaTraverserTest extends Specification {
def visitor = new GraphQLTestingVisitor()
def objectType = GraphQLObjectType.newObject()
.name("foo")
.withDirective(mockDirective("bar", DirectiveLocation.OBJECT))
.withDirective(mkDirective("bar", DirectiveLocation.OBJECT))
.withAppliedDirective(GraphQLAppliedDirective.newDirective()
.name("barApplied"))
.build()
Expand All @@ -235,7 +235,7 @@ class SchemaTraverserTest extends Specification {
def fieldDefinition = GraphQLFieldDefinition.newFieldDefinition()
.name("foo")
.type(Scalars.GraphQLString)
.withDirective(mockDirective("bar", DirectiveLocation.FIELD_DEFINITION))
.withDirective(mkDirective("bar", DirectiveLocation.FIELD_DEFINITION))
.withAppliedDirective(GraphQLAppliedDirective.newDirective()
.name("barApplied"))
.build()
Expand All @@ -252,7 +252,7 @@ class SchemaTraverserTest extends Specification {
def argument = newArgument()
.name("foo")
.type(Scalars.GraphQLString)
.withDirective(mockDirective("bar", DirectiveLocation.ARGUMENT_DEFINITION))
.withDirective(mkDirective("bar", DirectiveLocation.ARGUMENT_DEFINITION))
.withAppliedDirective(GraphQLAppliedDirective.newDirective()
.name("barApplied"))
.build()
Expand All @@ -268,7 +268,7 @@ class SchemaTraverserTest extends Specification {
def visitor = new GraphQLTestingVisitor()
def interfaceType = GraphQLInterfaceType.newInterface()
.name("foo")
.withDirective(mockDirective("bar", DirectiveLocation.INTERFACE))
.withDirective(mkDirective("bar", DirectiveLocation.INTERFACE))
.withAppliedDirective(GraphQLAppliedDirective.newDirective()
.name("barApplied"))
.build()
Expand All @@ -285,7 +285,7 @@ class SchemaTraverserTest extends Specification {
def unionType = GraphQLUnionType.newUnionType()
.name("foo")
.possibleType(GraphQLObjectType.newObject().name("dummy").build())
.withDirective(mockDirective("bar", DirectiveLocation.UNION))
.withDirective(mkDirective("bar", DirectiveLocation.UNION))
.build()
new SchemaTraverser().depthFirst(visitor, unionType)
then:
Expand All @@ -298,7 +298,7 @@ class SchemaTraverserTest extends Specification {
def enumType = GraphQLEnumType.newEnum()
.name("foo")
.value("dummy")
.withDirective(mockDirective("bar", DirectiveLocation.ENUM))
.withDirective(mkDirective("bar", DirectiveLocation.ENUM))
.build()
new SchemaTraverser().depthFirst(visitor, enumType)
then:
Expand All @@ -310,7 +310,7 @@ class SchemaTraverserTest extends Specification {
def visitor = new GraphQLTestingVisitor()
def enumValue = GraphQLEnumValueDefinition.newEnumValueDefinition()
.name("foo")
.withDirective(mockDirective("bar", DirectiveLocation.ENUM_VALUE))
.withDirective(mkDirective("bar", DirectiveLocation.ENUM_VALUE))
.build()
new SchemaTraverser().depthFirst(visitor, enumValue)
then:
Expand All @@ -322,7 +322,7 @@ class SchemaTraverserTest extends Specification {
def visitor = new GraphQLTestingVisitor()
def inputObjectType = GraphQLInputObjectType.newInputObject()
.name("foo")
.withDirective(mockDirective("bar", DirectiveLocation.INPUT_OBJECT))
.withDirective(mkDirective("bar", DirectiveLocation.INPUT_OBJECT))
.build()
new SchemaTraverser().depthFirst(visitor, inputObjectType)
then:
Expand All @@ -335,7 +335,7 @@ class SchemaTraverserTest extends Specification {
def inputField = GraphQLInputObjectField.newInputObjectField()
.name("foo")
.type(Scalars.GraphQLString)
.withDirective(mockDirective("bar", DirectiveLocation.INPUT_FIELD_DEFINITION))
.withDirective(mkDirective("bar", DirectiveLocation.INPUT_FIELD_DEFINITION))
.build()
new SchemaTraverser().depthFirst(visitor, inputField)
then:
Expand Down
2 changes: 0 additions & 2 deletions src/test/groovy/graphql/schema/idl/SchemaGeneratorTest.groovy
Expand Up @@ -8,7 +8,6 @@ import graphql.schema.DataFetcherFactory
import graphql.schema.DataFetcherFactoryEnvironment
import graphql.schema.DataFetchingEnvironment
import graphql.schema.GraphQLAppliedDirective
import graphql.schema.GraphQLArgument
import graphql.schema.GraphQLCodeRegistry
import graphql.schema.GraphQLDirectiveContainer
import graphql.schema.GraphQLEnumType
Expand Down Expand Up @@ -42,7 +41,6 @@ import static graphql.language.AstPrinter.printAst
import static graphql.schema.GraphQLCodeRegistry.newCodeRegistry
import static graphql.schema.idl.SchemaGenerator.Options.defaultOptions
import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring
import static graphql.TestUtil.mockDirective

class SchemaGeneratorTest extends Specification {

Expand Down

0 comments on commit d85046e

Please sign in to comment.