| 1 |
from functools import partial |
| 2 |
|
| 3 |
from graphql.utilities import build_schema |
| 4 |
from graphql.validation import ProvidedRequiredArgumentsRule |
| 5 |
from graphql.validation.rules.provided_required_arguments import ( |
| 6 |
ProvidedRequiredArgumentsOnDirectivesRule, |
| 7 |
) |
| 8 |
|
| 9 |
from .harness import assert_validation_errors, assert_sdl_validation_errors |
| 10 |
|
| 11 |
assert_errors = partial(assert_validation_errors, ProvidedRequiredArgumentsRule) |
| 12 |
|
| 13 |
assert_valid = partial(assert_errors, errors=[]) |
| 14 |
|
| 15 |
assert_sdl_errors = partial( |
| 16 |
assert_sdl_validation_errors, ProvidedRequiredArgumentsOnDirectivesRule |
| 17 |
) |
| 18 |
|
| 19 |
assert_sdl_valid = partial(assert_sdl_errors, errors=[]) |
| 20 |
|
| 21 |
|
| 22 |
def describe_validate_provided_required_arguments(): |
| 23 |
def ignores_unknown_arguments(): |
| 24 |
assert_valid(""" |
| 25 |
{ |
| 26 |
dog { |
| 27 |
isHouseTrained(unknownArgument: true) |
| 28 |
} |
| 29 |
}""") |
| 30 |
|
| 31 |
def describe_valid_non_nullable_value(): |
| 32 |
def arg_on_optional_arg(): |
| 33 |
assert_valid(""" |
| 34 |
{ |
| 35 |
dog { |
| 36 |
isHouseTrained(atOtherHomes: true) |
| 37 |
} |
| 38 |
}""") |
| 39 |
|
| 40 |
def no_arg_on_optional_arg(): |
| 41 |
assert_valid(""" |
| 42 |
{ |
| 43 |
dog { |
| 44 |
isHouseTrained |
| 45 |
} |
| 46 |
}""") |
| 47 |
|
| 48 |
def no_arg_on_non_null_field_with_default(): |
| 49 |
assert_valid(""" |
| 50 |
{ |
| 51 |
complicatedArgs { |
| 52 |
nonNullFieldWithDefault |
| 53 |
} |
| 54 |
}""") |
| 55 |
|
| 56 |
def multiple_args(): |
| 57 |
assert_valid(""" |
| 58 |
{ |
| 59 |
complicatedArgs { |
| 60 |
multipleReqs(req1: 1, req2: 2) |
| 61 |
} |
| 62 |
} |
| 63 |
""") |
| 64 |
|
| 65 |
def multiple_args_reverse_order(): |
| 66 |
assert_valid(""" |
| 67 |
{ |
| 68 |
complicatedArgs { |
| 69 |
multipleReqs(req2: 2, req1: 1) |
| 70 |
} |
| 71 |
} |
| 72 |
""") |
| 73 |
|
| 74 |
def no_args_on_multiple_optional(): |
| 75 |
assert_valid(""" |
| 76 |
{ |
| 77 |
complicatedArgs { |
| 78 |
multipleOpts |
| 79 |
} |
| 80 |
} |
| 81 |
""") |
| 82 |
|
| 83 |
def one_arg_on_multiple_optional(): |
| 84 |
assert_valid(""" |
| 85 |
{ |
| 86 |
complicatedArgs { |
| 87 |
multipleOpts(opt1: 1) |
| 88 |
} |
| 89 |
} |
| 90 |
""") |
| 91 |
|
| 92 |
def second_arg_on_multiple_optional(): |
| 93 |
assert_valid(""" |
| 94 |
{ |
| 95 |
complicatedArgs { |
| 96 |
multipleOpts(opt2: 1) |
| 97 |
} |
| 98 |
} |
| 99 |
""") |
| 100 |
|
| 101 |
def multiple_required_args_on_mixed_list(): |
| 102 |
assert_valid(""" |
| 103 |
{ |
| 104 |
complicatedArgs { |
| 105 |
multipleOptAndReq(req1: 3, req2: 4) |
| 106 |
} |
| 107 |
} |
| 108 |
""") |
| 109 |
|
| 110 |
def multiple_required_and_one_optional_arg_on_mixed_list(): |
| 111 |
assert_valid(""" |
| 112 |
{ |
| 113 |
complicatedArgs { |
| 114 |
multipleOptAndReq(req1: 3, req2: 4, opt1: 5) |
| 115 |
} |
| 116 |
} |
| 117 |
""") |
| 118 |
|
| 119 |
def all_required_and_optional_args_on_mixed_list(): |
| 120 |
assert_valid(""" |
| 121 |
{ |
| 122 |
complicatedArgs { |
| 123 |
multipleOptAndReq(req1: 3, req2: 4, opt1: 5, opt2: 6) |
| 124 |
} |
| 125 |
} |
| 126 |
""") |
| 127 |
|
| 128 |
def describe_invalid_non_nullable_value(): |
| 129 |
def missing_one_non_nullable_argument(): |
| 130 |
assert_errors( |
| 131 |
""" |
| 132 |
{ |
| 133 |
complicatedArgs { |
| 134 |
multipleReqs(req2: 2) |
| 135 |
} |
| 136 |
} |
| 137 |
""", |
| 138 |
[ |
| 139 |
{ |
| 140 |
"message": "Field 'multipleReqs' argument 'req1'" |
| 141 |
" of type 'Int!' is required, but it was not provided.", |
| 142 |
"locations": [(4, 21)], |
| 143 |
}, |
| 144 |
], |
| 145 |
) |
| 146 |
|
| 147 |
def missing_multiple_non_nullable_arguments(): |
| 148 |
assert_errors( |
| 149 |
""" |
| 150 |
{ |
| 151 |
complicatedArgs { |
| 152 |
multipleReqs |
| 153 |
} |
| 154 |
} |
| 155 |
""", |
| 156 |
[ |
| 157 |
{ |
| 158 |
"message": "Field 'multipleReqs' argument 'req1'" |
| 159 |
" of type 'Int!' is required, but it was not provided.", |
| 160 |
"locations": [(4, 21)], |
| 161 |
}, |
| 162 |
{ |
| 163 |
"message": "Field 'multipleReqs' argument 'req2'" |
| 164 |
" of type 'Int!' is required, but it was not provided.", |
| 165 |
"locations": [(4, 21)], |
| 166 |
}, |
| 167 |
], |
| 168 |
) |
| 169 |
|
| 170 |
def incorrect_value_and_missing_argument(): |
| 171 |
assert_errors( |
| 172 |
""" |
| 173 |
{ |
| 174 |
complicatedArgs { |
| 175 |
multipleReqs(req1: "one") |
| 176 |
} |
| 177 |
} |
| 178 |
""", |
| 179 |
[ |
| 180 |
{ |
| 181 |
"message": "Field 'multipleReqs' argument 'req2'" |
| 182 |
" of type 'Int!' is required, but it was not provided.", |
| 183 |
"locations": [(4, 21)], |
| 184 |
}, |
| 185 |
], |
| 186 |
) |
| 187 |
|
| 188 |
def describe_directive_arguments(): |
| 189 |
def ignores_unknown_directives(): |
| 190 |
assert_valid(""" |
| 191 |
{ |
| 192 |
dog @unknown |
| 193 |
} |
| 194 |
""") |
| 195 |
|
| 196 |
def with_directives_of_valid_type(): |
| 197 |
assert_valid(""" |
| 198 |
{ |
| 199 |
dog @include(if: true) { |
| 200 |
name |
| 201 |
} |
| 202 |
human @skip(if: false) { |
| 203 |
name |
| 204 |
} |
| 205 |
} |
| 206 |
""") |
| 207 |
|
| 208 |
def with_directive_with_missing_types(): |
| 209 |
assert_errors( |
| 210 |
""" |
| 211 |
{ |
| 212 |
dog @include { |
| 213 |
name @skip |
| 214 |
} |
| 215 |
} |
| 216 |
""", |
| 217 |
[ |
| 218 |
{ |
| 219 |
"message": "Directive '@include' argument 'if' of type" |
| 220 |
" 'Boolean!' is required, but it was not provided.", |
| 221 |
"locations": [(3, 23)], |
| 222 |
}, |
| 223 |
{ |
| 224 |
"message": "Directive '@skip' argument 'if' of type" |
| 225 |
" 'Boolean!' is required, but it was not provided.", |
| 226 |
"locations": [(4, 26)], |
| 227 |
}, |
| 228 |
], |
| 229 |
) |
| 230 |
|
| 231 |
def describe_within_sdl(): |
| 232 |
def missing_optional_args_on_directive_defined_inside_sdl(): |
| 233 |
assert_sdl_valid(""" |
| 234 |
type Query { |
| 235 |
foo: String @test |
| 236 |
} |
| 237 |
|
| 238 |
directive @test(arg1: String, arg2: String! = "") on FIELD_DEFINITION |
| 239 |
""") |
| 240 |
|
| 241 |
def missing_arg_on_directive_defined_inside_sdl(): |
| 242 |
assert_sdl_errors( |
| 243 |
""" |
| 244 |
type Query { |
| 245 |
foo: String @test |
| 246 |
} |
| 247 |
|
| 248 |
directive @test(arg: String!) on FIELD_DEFINITION |
| 249 |
""", |
| 250 |
[ |
| 251 |
{ |
| 252 |
"message": "Directive '@test' argument 'arg' of type" |
| 253 |
" 'String!' is required, but it was not provided.", |
| 254 |
"locations": [(3, 31)], |
| 255 |
}, |
| 256 |
], |
| 257 |
) |
| 258 |
|
| 259 |
def missing_arg_on_standard_directive(): |
| 260 |
assert_sdl_errors( |
| 261 |
""" |
| 262 |
type Query { |
| 263 |
foo: String @include |
| 264 |
} |
| 265 |
""", |
| 266 |
[ |
| 267 |
{ |
| 268 |
"message": "Directive '@include' argument 'if' of type" |
| 269 |
" 'Boolean!' is required, but it was not provided.", |
| 270 |
"locations": [(3, 31)], |
| 271 |
}, |
| 272 |
], |
| 273 |
) |
| 274 |
|
| 275 |
def missing_arg_on_overridden_standard_directive(): |
| 276 |
assert_sdl_errors( |
| 277 |
""" |
| 278 |
type Query { |
| 279 |
foo: String @deprecated |
| 280 |
} |
| 281 |
directive @deprecated(reason: String!) on FIELD |
| 282 |
""", |
| 283 |
[ |
| 284 |
{ |
| 285 |
"message": "Directive '@deprecated' argument 'reason' of type" |
| 286 |
" 'String!' is required, but it was not provided.", |
| 287 |
"locations": [(3, 31)], |
| 288 |
}, |
| 289 |
], |
| 290 |
) |
| 291 |
|
| 292 |
def missing_arg_on_directive_defined_in_schema_extension(): |
| 293 |
schema = build_schema(""" |
| 294 |
type Query { |
| 295 |
foo: String |
| 296 |
} |
| 297 |
""") |
| 298 |
assert_sdl_errors( |
| 299 |
""" |
| 300 |
directive @test(arg: String!) on OBJECT |
| 301 |
|
| 302 |
extend type Query @test |
| 303 |
""", |
| 304 |
[ |
| 305 |
{ |
| 306 |
"message": "Directive '@test' argument 'arg' of type" |
| 307 |
" 'String!' is required, but it was not provided.", |
| 308 |
"locations": [(4, 36)], |
| 309 |
}, |
| 310 |
], |
| 311 |
schema, |
| 312 |
) |
| 313 |
|
| 314 |
def missing_arg_on_directive_used_in_schema_extension(): |
| 315 |
schema = build_schema(""" |
| 316 |
directive @test(arg: String!) on OBJECT |
| 317 |
|
| 318 |
type Query { |
| 319 |
foo: String |
| 320 |
} |
| 321 |
""") |
| 322 |
assert_sdl_errors( |
| 323 |
""" |
| 324 |
extend type Query @test |
| 325 |
""", |
| 326 |
[ |
| 327 |
{ |
| 328 |
"message": "Directive '@test' argument 'arg' of type" |
| 329 |
" 'String!' is required, but it was not provided.", |
| 330 |
"locations": [(2, 36)], |
| 331 |
}, |
| 332 |
], |
| 333 |
schema, |
| 334 |
) |