| 1 |
from functools import partial |
| 2 |
|
| 3 |
from graphql.utilities import build_schema |
| 4 |
from graphql.validation import KnownArgumentNamesRule |
| 5 |
from graphql.validation.rules.known_argument_names import ( |
| 6 |
KnownArgumentNamesOnDirectivesRule, |
| 7 |
) |
| 8 |
|
| 9 |
from .harness import assert_validation_errors, assert_sdl_validation_errors |
| 10 |
|
| 11 |
assert_errors = partial(assert_validation_errors, KnownArgumentNamesRule) |
| 12 |
|
| 13 |
assert_valid = partial(assert_errors, errors=[]) |
| 14 |
|
| 15 |
assert_sdl_errors = partial( |
| 16 |
assert_sdl_validation_errors, KnownArgumentNamesOnDirectivesRule |
| 17 |
) |
| 18 |
|
| 19 |
assert_sdl_valid = partial(assert_sdl_errors, errors=[]) |
| 20 |
|
| 21 |
|
| 22 |
def describe_validate_known_argument_names(): |
| 23 |
def single_arg_is_known(): |
| 24 |
assert_valid(""" |
| 25 |
fragment argOnRequiredArg on Dog { |
| 26 |
doesKnowCommand(dogCommand: SIT) |
| 27 |
} |
| 28 |
""") |
| 29 |
|
| 30 |
def multiple_args_are_known(): |
| 31 |
assert_valid(""" |
| 32 |
fragment multipleArgs on ComplicatedArgs { |
| 33 |
multipleReqs(req1: 1, req2: 2) |
| 34 |
} |
| 35 |
""") |
| 36 |
|
| 37 |
def ignore_args_of_unknown_fields(): |
| 38 |
assert_valid(""" |
| 39 |
fragment argOnUnknownField on Dog { |
| 40 |
unknownField(unknownArg: SIT) |
| 41 |
} |
| 42 |
""") |
| 43 |
|
| 44 |
def multiple_args_in_reverse_order_are_known(): |
| 45 |
assert_valid(""" |
| 46 |
fragment multipleArgsReverseOrder on ComplicatedArgs { |
| 47 |
multipleReqs(req2: 2, req1: 1) |
| 48 |
} |
| 49 |
""") |
| 50 |
|
| 51 |
def no_args_on_optional_arg(): |
| 52 |
assert_valid(""" |
| 53 |
fragment noArgOnOptionalArg on Dog { |
| 54 |
isHouseTrained |
| 55 |
} |
| 56 |
""") |
| 57 |
|
| 58 |
def args_are_known_deeply(): |
| 59 |
assert_valid(""" |
| 60 |
{ |
| 61 |
dog { |
| 62 |
doesKnowCommand(dogCommand: SIT) |
| 63 |
} |
| 64 |
human { |
| 65 |
pet { |
| 66 |
... on Dog { |
| 67 |
doesKnowCommand(dogCommand: SIT) |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
""") |
| 73 |
|
| 74 |
def directive_args_are_known(): |
| 75 |
assert_valid(""" |
| 76 |
{ |
| 77 |
dog @skip(if: true) |
| 78 |
} |
| 79 |
""") |
| 80 |
|
| 81 |
def field_args_are_invalid(): |
| 82 |
assert_errors( |
| 83 |
""" |
| 84 |
{ |
| 85 |
dog @skip(unless: true) |
| 86 |
} |
| 87 |
""", |
| 88 |
[ |
| 89 |
{ |
| 90 |
"message": "Unknown argument 'unless' on directive '@skip'.", |
| 91 |
"locations": [(3, 25)], |
| 92 |
} |
| 93 |
], |
| 94 |
) |
| 95 |
|
| 96 |
def directive_without_args_is_valid(): |
| 97 |
assert_valid(""" |
| 98 |
{ |
| 99 |
dog @onField |
| 100 |
} |
| 101 |
""") |
| 102 |
|
| 103 |
def arg_passed_to_directive_without_args_is_reported(): |
| 104 |
assert_errors( |
| 105 |
""" |
| 106 |
{ |
| 107 |
dog @onField(if: true) |
| 108 |
} |
| 109 |
""", |
| 110 |
[ |
| 111 |
{ |
| 112 |
"message": "Unknown argument 'if' on directive '@onField'.", |
| 113 |
"locations": [(3, 30)], |
| 114 |
} |
| 115 |
], |
| 116 |
) |
| 117 |
|
| 118 |
def misspelled_directive_args_are_reported(): |
| 119 |
assert_errors( |
| 120 |
""" |
| 121 |
{ |
| 122 |
dog @skip(iff: true) |
| 123 |
} |
| 124 |
""", |
| 125 |
[ |
| 126 |
{ |
| 127 |
"message": "Unknown argument 'iff' on directive '@skip'." |
| 128 |
" Did you mean 'if'?", |
| 129 |
"locations": [(3, 25)], |
| 130 |
} |
| 131 |
], |
| 132 |
) |
| 133 |
|
| 134 |
def invalid_arg_name(): |
| 135 |
assert_errors( |
| 136 |
""" |
| 137 |
fragment invalidArgName on Dog { |
| 138 |
doesKnowCommand(unknown: true) |
| 139 |
} |
| 140 |
""", |
| 141 |
[ |
| 142 |
{ |
| 143 |
"message": "Unknown argument 'unknown'" |
| 144 |
" on field 'Dog.doesKnowCommand'.", |
| 145 |
"locations": [(3, 31)], |
| 146 |
}, |
| 147 |
], |
| 148 |
) |
| 149 |
|
| 150 |
def misspelled_arg_name_is_reported(): |
| 151 |
assert_errors( |
| 152 |
""" |
| 153 |
fragment invalidArgName on Dog { |
| 154 |
doesKnowCommand(DogCommand: true) |
| 155 |
} |
| 156 |
""", |
| 157 |
[ |
| 158 |
{ |
| 159 |
"message": "Unknown argument 'DogCommand'" |
| 160 |
" on field 'Dog.doesKnowCommand'." |
| 161 |
" Did you mean 'dogCommand'?", |
| 162 |
"locations": [(3, 31)], |
| 163 |
} |
| 164 |
], |
| 165 |
) |
| 166 |
|
| 167 |
def unknown_args_amongst_known_args(): |
| 168 |
assert_errors( |
| 169 |
""" |
| 170 |
fragment oneGoodArgOneInvalidArg on Dog { |
| 171 |
doesKnowCommand(whoKnows: 1, dogCommand: SIT, unknown: true) |
| 172 |
} |
| 173 |
""", |
| 174 |
[ |
| 175 |
{ |
| 176 |
"message": "Unknown argument 'whoKnows'" |
| 177 |
" on field 'Dog.doesKnowCommand'.", |
| 178 |
"locations": [(3, 31)], |
| 179 |
}, |
| 180 |
{ |
| 181 |
"message": "Unknown argument 'unknown'" |
| 182 |
" on field 'Dog.doesKnowCommand'.", |
| 183 |
"locations": [(3, 61)], |
| 184 |
}, |
| 185 |
], |
| 186 |
) |
| 187 |
|
| 188 |
def unknown_args_deeply(): |
| 189 |
assert_errors( |
| 190 |
""" |
| 191 |
{ |
| 192 |
dog { |
| 193 |
doesKnowCommand(unknown: true) |
| 194 |
} |
| 195 |
human { |
| 196 |
pet { |
| 197 |
... on Dog { |
| 198 |
doesKnowCommand(unknown: true) |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
""", |
| 204 |
[ |
| 205 |
{ |
| 206 |
"message": "Unknown argument 'unknown'" |
| 207 |
" on field 'Dog.doesKnowCommand'.", |
| 208 |
"locations": [(4, 33)], |
| 209 |
}, |
| 210 |
{ |
| 211 |
"message": "Unknown argument 'unknown'" |
| 212 |
" on field 'Dog.doesKnowCommand'.", |
| 213 |
"locations": [(9, 37)], |
| 214 |
}, |
| 215 |
], |
| 216 |
) |
| 217 |
|
| 218 |
def describe_within_sdl(): |
| 219 |
def known_arg_on_directive_inside_sdl(): |
| 220 |
assert_sdl_valid(""" |
| 221 |
type Query { |
| 222 |
foo: String @test(arg: "") |
| 223 |
} |
| 224 |
|
| 225 |
directive @test(arg: String) on FIELD_DEFINITION |
| 226 |
""") |
| 227 |
|
| 228 |
def unknown_arg_on_directive_defined_inside_sdl(): |
| 229 |
assert_sdl_errors( |
| 230 |
""" |
| 231 |
type Query { |
| 232 |
foo: String @test(unknown: "") |
| 233 |
} |
| 234 |
|
| 235 |
directive @test(arg: String) on FIELD_DEFINITION |
| 236 |
""", |
| 237 |
[ |
| 238 |
{ |
| 239 |
"message": "Unknown argument 'unknown' on directive '@test'.", |
| 240 |
"locations": [(3, 37)], |
| 241 |
}, |
| 242 |
], |
| 243 |
) |
| 244 |
|
| 245 |
def misspelled_arg_name_is_reported_on_directive_defined_inside_sdl(): |
| 246 |
assert_sdl_errors( |
| 247 |
""" |
| 248 |
type Query { |
| 249 |
foo: String @test(agr: "") |
| 250 |
} |
| 251 |
|
| 252 |
directive @test(arg: String) on FIELD_DEFINITION |
| 253 |
""", |
| 254 |
[ |
| 255 |
{ |
| 256 |
"message": "Unknown argument 'agr' on directive '@test'." |
| 257 |
" Did you mean 'arg'?", |
| 258 |
"locations": [(3, 37)], |
| 259 |
}, |
| 260 |
], |
| 261 |
) |
| 262 |
|
| 263 |
def unknown_arg_on_standard_directive(): |
| 264 |
assert_sdl_errors( |
| 265 |
""" |
| 266 |
type Query { |
| 267 |
foo: String @deprecated(unknown: "") |
| 268 |
} |
| 269 |
""", |
| 270 |
[ |
| 271 |
{ |
| 272 |
"message": "Unknown argument 'unknown'" |
| 273 |
" on directive '@deprecated'.", |
| 274 |
"locations": [(3, 43)], |
| 275 |
}, |
| 276 |
], |
| 277 |
) |
| 278 |
|
| 279 |
def unknown_arg_on_overridden_standard_directive(): |
| 280 |
assert_sdl_errors( |
| 281 |
""" |
| 282 |
type Query { |
| 283 |
foo: String @deprecated(reason: "") |
| 284 |
} |
| 285 |
directive @deprecated(arg: String) on FIELD |
| 286 |
""", |
| 287 |
[ |
| 288 |
{ |
| 289 |
"message": "Unknown argument 'reason'" |
| 290 |
" on directive '@deprecated'.", |
| 291 |
"locations": [(3, 43)], |
| 292 |
}, |
| 293 |
], |
| 294 |
) |
| 295 |
|
| 296 |
def unknown_arg_on_directive_defined_in_schema_extension(): |
| 297 |
schema = build_schema(""" |
| 298 |
type Query { |
| 299 |
foo: String |
| 300 |
} |
| 301 |
""") |
| 302 |
assert_sdl_errors( |
| 303 |
""" |
| 304 |
directive @test(arg: String) on OBJECT |
| 305 |
|
| 306 |
extend type Query @test(unknown: "") |
| 307 |
""", |
| 308 |
[ |
| 309 |
{ |
| 310 |
"message": "Unknown argument 'unknown' on directive '@test'.", |
| 311 |
"locations": [(4, 42)], |
| 312 |
}, |
| 313 |
], |
| 314 |
schema, |
| 315 |
) |
| 316 |
|
| 317 |
def unknown_arg_on_directive_used_in_schema_extension(): |
| 318 |
schema = build_schema(""" |
| 319 |
directive @test(arg: String) on OBJECT |
| 320 |
|
| 321 |
type Query { |
| 322 |
foo: String |
| 323 |
} |
| 324 |
""") |
| 325 |
assert_sdl_errors( |
| 326 |
""" |
| 327 |
extend type Query @test(unknown: "") |
| 328 |
""", |
| 329 |
[ |
| 330 |
{ |
| 331 |
"message": "Unknown argument 'unknown' on directive '@test'.", |
| 332 |
"locations": [(2, 41)], |
| 333 |
}, |
| 334 |
], |
| 335 |
schema, |
| 336 |
) |