-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
GraphQLFieldDefinitionTest.groovy
98 lines (85 loc) · 4.17 KB
/
GraphQLFieldDefinitionTest.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package graphql.schema
import graphql.AssertException
import graphql.TestUtil
import graphql.introspection.Introspection
import graphql.schema.idl.SchemaPrinter
import spock.lang.Specification
import static graphql.Scalars.GraphQLBoolean
import static graphql.Scalars.GraphQLFloat
import static graphql.Scalars.GraphQLInt
import static graphql.Scalars.GraphQLString
import static graphql.TestUtil.mockArguments
import static graphql.TestUtil.mkDirective
import static graphql.schema.DefaultGraphqlTypeComparatorRegistry.newComparators
import static graphql.schema.GraphQLArgument.newArgument
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition
import static graphql.schema.idl.SchemaPrinter.Options.defaultOptions
class GraphQLFieldDefinitionTest extends Specification {
def "dataFetcher can't be null"() {
when:
newFieldDefinition().dataFetcher(null) // Retain for test coverage
then:
def exception = thrown(AssertException)
exception.getMessage().contains("dataFetcher")
}
def "object can be transformed"() {
given:
def startingField = newFieldDefinition()
.name("F1")
.type(GraphQLFloat)
.description("F1_description")
.deprecate("F1_deprecated")
.argument(newArgument().name("argStr").type(GraphQLString))
.argument(newArgument().name("argInt").type(GraphQLInt))
.withDirective(mkDirective("directive1", Introspection.DirectiveLocation.FIELD_DEFINITION))
.withDirective(mkDirective("directive2", Introspection.DirectiveLocation.FIELD_DEFINITION))
.build()
when:
def transformedField = startingField.transform({ builder ->
builder.name("F2")
.type(GraphQLInt)
.deprecate(null)
.argument(newArgument().name("argStr").type(GraphQLString))
.argument(newArgument().name("argInt").type(GraphQLBoolean))
.argument(newArgument().name("argIntAdded").type(GraphQLInt))
.withDirective(mkDirective("directive3", Introspection.DirectiveLocation.FIELD_DEFINITION))
})
then:
startingField.name == "F1"
startingField.type == GraphQLFloat
startingField.description == "F1_description"
startingField.deprecated
startingField.deprecationReason == "F1_deprecated"
startingField.getArguments().size() == 2
startingField.getArgument("argStr").type == GraphQLString
startingField.getArgument("argInt").type == GraphQLInt
startingField.getDirectives().size() == 2
startingField.getDirective("directive1") != null
startingField.getDirective("directive2") != null
transformedField.name == "F2"
transformedField.type == GraphQLInt
transformedField.description == "F1_description" // left alone
!transformedField.deprecated
transformedField.deprecationReason == null
transformedField.getArguments().size() == 3
transformedField.getArgument("argStr").type == GraphQLString
transformedField.getArgument("argInt").type == GraphQLBoolean
transformedField.getArgument("argIntAdded").type == GraphQLInt
transformedField.getDirectives().size() == 3
transformedField.getDirective("directive1") != null
transformedField.getDirective("directive2") != null
transformedField.getDirective("directive3") != null
}
def "test deprecated argument builder for list"() {
given:
def field = newFieldDefinition().name("field").type(GraphQLInt).argument(mockArguments("a", "bb")).build() // Retain for test coverage
when:
def registry = newComparators()
.addComparator({ it.parentType(GraphQLFieldDefinition.class).elementType(GraphQLArgument.class) }, GraphQLArgument.class, TestUtil.byGreatestLength)
.build()
def options = defaultOptions().setComparators(registry)
def printer = new SchemaPrinter(options)
then:
printer.argsString(GraphQLFieldDefinition.class, field.arguments) == '''(bb: Int, a: Int)'''
}
}