-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
GraphQLArgumentTest.groovy
221 lines (184 loc) · 8.26 KB
/
GraphQLArgumentTest.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package graphql.schema
import graphql.collect.ImmutableKit
import static graphql.introspection.Introspection.DirectiveLocation.ARGUMENT_DEFINITION
import graphql.language.FloatValue
import graphql.schema.validation.InvalidSchemaException
import spock.lang.Specification
import static graphql.Scalars.GraphQLFloat
import static graphql.Scalars.GraphQLInt
import static graphql.Scalars.GraphQLString
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.mkDirective
class GraphQLArgumentTest extends Specification {
def "object can be transformed"() {
given:
def startingArgument = GraphQLArgument.newArgument().name("A1")
.description("A1_description")
.type(GraphQLInt)
.deprecate("custom reason")
.withDirective(mkDirective("directive1", ARGUMENT_DEFINITION))
.build()
when:
def transformedArgument = startingArgument.transform({
it
.name("A2")
.description("A2_description")
.type(GraphQLString)
.withDirective(mkDirective("directive3", ARGUMENT_DEFINITION))
.value("VALUE") // Retain deprecated for test coverage
.deprecate(null)
.defaultValue("DEFAULT") // Retain deprecated for test coverage
})
then:
startingArgument.name == "A1"
startingArgument.description == "A1_description"
startingArgument.type == GraphQLInt
startingArgument.argumentDefaultValue.value == null
startingArgument.deprecationReason == "custom reason"
startingArgument.isDeprecated()
startingArgument.getDirectives().size() == 1
startingArgument.getDirective("directive1") != null
transformedArgument.name == "A2"
transformedArgument.description == "A2_description"
transformedArgument.type == GraphQLString
transformedArgument.argumentValue.value == "VALUE" // Retain deprecated for test coverage
transformedArgument.argumentDefaultValue.value == "DEFAULT"
transformedArgument.deprecationReason == null
!transformedArgument.isDeprecated()
transformedArgument.getDirectives().size() == 2
transformedArgument.getDirective("directive1") != null
transformedArgument.getDirective("directive3") != null
}
def "object can be transformed without setting the default value"() {
given:
def startingArgument = GraphQLArgument.newArgument().name("A1")
.type(GraphQLInt)
.build()
when:
def transformedArgument = startingArgument.transform({
it.name("A2")
})
then:
transformedArgument.name == "A2"
!transformedArgument.hasSetDefaultValue()
}
def "directive support on arguments via builder"() {
def argument
given:
def builder = newArgument().name("A1")
.type(GraphQLInt)
.withDirective(mkDirective("directive1", ARGUMENT_DEFINITION))
when:
argument = builder.build()
then:
argument.getDirectives().size() == 1
argument.getDirective("directive1") != null
argument.getDirective("directive2") == null
argument.getDirective("directive3") == null
when:
argument = builder
.clearDirectives()
.withDirective(mkDirective("directive2", ARGUMENT_DEFINITION))
.withDirective(mkDirective("directive3", ARGUMENT_DEFINITION))
.build()
then:
argument.getDirectives().size() == 2
argument.getDirective("directive1") == null
argument.getDirective("directive2") != null
argument.getDirective("directive3") != null
when:
argument = builder
.replaceDirectives([
mkDirective("directive1", ARGUMENT_DEFINITION),
mkDirective("directive2", ARGUMENT_DEFINITION),
mkDirective("directive3", ARGUMENT_DEFINITION)]) // overwrite
.build()
then:
argument.getDirectives().size() == 3
argument.getDirective("directive1") != null
argument.getDirective("directive2") != null
argument.getDirective("directive3") != null
}
def "can get values statically"() {
// Retain deprecated API usages in this test for test coverage
when:
GraphQLArgument startingArg = GraphQLArgument.newArgument()
.name("F1")
.type(GraphQLFloat)
.description("F1_description")
.valueProgrammatic(4.56d) // Retain deprecated for test coverage
.defaultValueProgrammatic(1.23d)
.build()
def inputValue = startingArg.getArgumentValue() // Retain deprecated for test coverage
def resolvedValue = GraphQLArgument.getArgumentValue(startingArg) // Retain deprecated for test coverage
def inputDefaultValue = startingArg.getArgumentDefaultValue()
def resolvedDefaultValue = GraphQLArgument.getArgumentDefaultValue(startingArg)
then:
inputValue.isExternal()
inputValue.getValue() == 4.56d
resolvedValue == 4.56d
inputDefaultValue.isExternal()
inputDefaultValue.getValue() == 1.23d
resolvedDefaultValue == 1.23d
when:
startingArg = GraphQLArgument.newArgument()
.name("F1")
.type(GraphQLFloat)
.description("F1_description")
.valueLiteral(FloatValue.newFloatValue().value(4.56d).build()) // Retain deprecated for test coverage
.defaultValueLiteral(FloatValue.newFloatValue().value(1.23d).build())
.build()
inputValue = startingArg.getArgumentValue() // Retain deprecated for test coverage
resolvedValue = GraphQLArgument.getArgumentValue(startingArg) // Retain deprecated for test coverage
inputDefaultValue = startingArg.getArgumentDefaultValue()
resolvedDefaultValue = GraphQLArgument.getArgumentDefaultValue(startingArg)
then:
inputValue.isLiteral()
(inputValue.getValue() as FloatValue).getValue().toDouble() == 4.56d
resolvedValue == 4.56d
inputDefaultValue.isLiteral()
(inputDefaultValue.getValue() as FloatValue).getValue().toDouble() == 1.23d
resolvedDefaultValue == 1.23d
when:
startingArg = GraphQLArgument.newArgument()
.name("F1")
.type(GraphQLFloat)
.description("F1_description")
.build()
inputValue = startingArg.getArgumentValue() // Retain deprecated for test coverage
resolvedValue = GraphQLArgument.getArgumentValue(startingArg) // Retain deprecated for test coverage
inputDefaultValue = startingArg.getArgumentDefaultValue()
resolvedDefaultValue = GraphQLArgument.getArgumentDefaultValue(startingArg)
then:
inputValue.isNotSet()
inputValue.getValue() == null
resolvedValue == null
inputDefaultValue.isNotSet()
inputDefaultValue.getValue() == null
resolvedDefaultValue == null
}
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 = mkDirective("cached", ARGUMENT_DEFINITION, arg)
def field = newFieldDefinition()
.name("hello")
.type(GraphQLString)
.argument(arg)
.withDirective(directive)
.build()
when:
newSchema().query(
newObject()
.name("Query")
.field(field)
.build())
.build()
then:
def e = thrown(InvalidSchemaException)
e.message.contains("Invalid argument 'arg' for applied directive of name 'cached'")
}
}