| 1 |
from functools import partial |
| 2 |
|
| 3 |
from graphql.utilities import build_schema |
| 4 |
from graphql.validation.rules.unique_field_definition_names import ( |
| 5 |
UniqueFieldDefinitionNamesRule, |
| 6 |
) |
| 7 |
|
| 8 |
from .harness import assert_sdl_validation_errors |
| 9 |
|
| 10 |
assert_errors = partial(assert_sdl_validation_errors, UniqueFieldDefinitionNamesRule) |
| 11 |
|
| 12 |
assert_valid = partial(assert_errors, errors=[]) |
| 13 |
|
| 14 |
|
| 15 |
def describe_validate_unique_field_definition_names(): |
| 16 |
def no_fields(): |
| 17 |
assert_valid(""" |
| 18 |
type SomeObject |
| 19 |
interface SomeInterface |
| 20 |
input SomeInputObject |
| 21 |
""") |
| 22 |
|
| 23 |
def one_field(): |
| 24 |
assert_valid(""" |
| 25 |
type SomeObject { |
| 26 |
foo: String |
| 27 |
} |
| 28 |
|
| 29 |
interface SomeInterface { |
| 30 |
foo: String |
| 31 |
} |
| 32 |
|
| 33 |
input SomeInputObject { |
| 34 |
foo: String |
| 35 |
} |
| 36 |
""") |
| 37 |
|
| 38 |
def multiple_fields(): |
| 39 |
assert_valid(""" |
| 40 |
type SomeObject { |
| 41 |
foo: String |
| 42 |
bar: String |
| 43 |
} |
| 44 |
|
| 45 |
interface SomeInterface { |
| 46 |
foo: String |
| 47 |
bar: String |
| 48 |
} |
| 49 |
|
| 50 |
input SomeInputObject { |
| 51 |
foo: String |
| 52 |
bar: String |
| 53 |
} |
| 54 |
""") |
| 55 |
|
| 56 |
def duplicate_fields_inside_the_same_type_definition(): |
| 57 |
assert_errors( |
| 58 |
""" |
| 59 |
type SomeObject { |
| 60 |
foo: String |
| 61 |
bar: String |
| 62 |
foo: String |
| 63 |
} |
| 64 |
|
| 65 |
interface SomeInterface { |
| 66 |
foo: String |
| 67 |
bar: String |
| 68 |
foo: String |
| 69 |
} |
| 70 |
|
| 71 |
input SomeInputObject { |
| 72 |
foo: String |
| 73 |
bar: String |
| 74 |
foo: String |
| 75 |
} |
| 76 |
""", |
| 77 |
[ |
| 78 |
{ |
| 79 |
"message": "Field 'SomeObject.foo' can only be defined once.", |
| 80 |
"locations": [(3, 15), (5, 15)], |
| 81 |
}, |
| 82 |
{ |
| 83 |
"message": "Field 'SomeInterface.foo' can only be defined once.", |
| 84 |
"locations": [(9, 15), (11, 15)], |
| 85 |
}, |
| 86 |
{ |
| 87 |
"message": "Field 'SomeInputObject.foo' can only be defined once.", |
| 88 |
"locations": [(15, 15), (17, 15)], |
| 89 |
}, |
| 90 |
], |
| 91 |
) |
| 92 |
|
| 93 |
def extend_type_with_new_field(): |
| 94 |
assert_valid(""" |
| 95 |
type SomeObject { |
| 96 |
foo: String |
| 97 |
} |
| 98 |
extend type SomeObject { |
| 99 |
bar: String |
| 100 |
} |
| 101 |
extend type SomeObject { |
| 102 |
baz: String |
| 103 |
} |
| 104 |
|
| 105 |
interface SomeInterface { |
| 106 |
foo: String |
| 107 |
} |
| 108 |
extend interface SomeInterface { |
| 109 |
bar: String |
| 110 |
} |
| 111 |
extend interface SomeInterface { |
| 112 |
baz: String |
| 113 |
} |
| 114 |
|
| 115 |
input SomeInputObject { |
| 116 |
foo: String |
| 117 |
} |
| 118 |
extend input SomeInputObject { |
| 119 |
bar: String |
| 120 |
} |
| 121 |
extend input SomeInputObject { |
| 122 |
baz: String |
| 123 |
} |
| 124 |
""") |
| 125 |
|
| 126 |
def extend_type_with_duplicate_field(): |
| 127 |
assert_errors( |
| 128 |
""" |
| 129 |
extend type SomeObject { |
| 130 |
foo: String |
| 131 |
} |
| 132 |
type SomeObject { |
| 133 |
foo: String |
| 134 |
} |
| 135 |
|
| 136 |
extend interface SomeInterface { |
| 137 |
foo: String |
| 138 |
} |
| 139 |
interface SomeInterface { |
| 140 |
foo: String |
| 141 |
} |
| 142 |
|
| 143 |
extend input SomeInputObject { |
| 144 |
foo: String |
| 145 |
} |
| 146 |
input SomeInputObject { |
| 147 |
foo: String |
| 148 |
} |
| 149 |
""", |
| 150 |
[ |
| 151 |
{ |
| 152 |
"message": "Field 'SomeObject.foo' can only be defined once.", |
| 153 |
"locations": [(3, 15), (6, 15)], |
| 154 |
}, |
| 155 |
{ |
| 156 |
"message": "Field 'SomeInterface.foo' can only be defined once.", |
| 157 |
"locations": [(10, 15), (13, 15)], |
| 158 |
}, |
| 159 |
{ |
| 160 |
"message": "Field 'SomeInputObject.foo' can only be defined once.", |
| 161 |
"locations": [(17, 15), (20, 15)], |
| 162 |
}, |
| 163 |
], |
| 164 |
) |
| 165 |
|
| 166 |
def duplicate_field_inside_extension(): |
| 167 |
assert_errors( |
| 168 |
""" |
| 169 |
type SomeObject |
| 170 |
extend type SomeObject { |
| 171 |
foo: String |
| 172 |
bar: String |
| 173 |
foo: String |
| 174 |
} |
| 175 |
|
| 176 |
interface SomeInterface |
| 177 |
extend interface SomeInterface { |
| 178 |
foo: String |
| 179 |
bar: String |
| 180 |
foo: String |
| 181 |
} |
| 182 |
|
| 183 |
input SomeInputObject |
| 184 |
extend input SomeInputObject { |
| 185 |
foo: String |
| 186 |
bar: String |
| 187 |
foo: String |
| 188 |
} |
| 189 |
""", |
| 190 |
[ |
| 191 |
{ |
| 192 |
"message": "Field 'SomeObject.foo' can only be defined once.", |
| 193 |
"locations": [(4, 15), (6, 15)], |
| 194 |
}, |
| 195 |
{ |
| 196 |
"message": "Field 'SomeInterface.foo' can only be defined once.", |
| 197 |
"locations": [(11, 15), (13, 15)], |
| 198 |
}, |
| 199 |
{ |
| 200 |
"message": "Field 'SomeInputObject.foo' can only be defined once.", |
| 201 |
"locations": [(18, 15), (20, 15)], |
| 202 |
}, |
| 203 |
], |
| 204 |
) |
| 205 |
|
| 206 |
def duplicate_field_inside_different_extension(): |
| 207 |
assert_errors( |
| 208 |
""" |
| 209 |
type SomeObject |
| 210 |
extend type SomeObject { |
| 211 |
foo: String |
| 212 |
} |
| 213 |
extend type SomeObject { |
| 214 |
foo: String |
| 215 |
} |
| 216 |
|
| 217 |
interface SomeInterface |
| 218 |
extend interface SomeInterface { |
| 219 |
foo: String |
| 220 |
} |
| 221 |
extend interface SomeInterface { |
| 222 |
foo: String |
| 223 |
} |
| 224 |
|
| 225 |
input SomeInputObject |
| 226 |
extend input SomeInputObject { |
| 227 |
foo: String |
| 228 |
} |
| 229 |
extend input SomeInputObject { |
| 230 |
foo: String |
| 231 |
} |
| 232 |
""", |
| 233 |
[ |
| 234 |
{ |
| 235 |
"message": "Field 'SomeObject.foo' can only be defined once.", |
| 236 |
"locations": [(4, 15), (7, 15)], |
| 237 |
}, |
| 238 |
{ |
| 239 |
"message": "Field 'SomeInterface.foo' can only be defined once.", |
| 240 |
"locations": [(12, 15), (15, 15)], |
| 241 |
}, |
| 242 |
{ |
| 243 |
"message": "Field 'SomeInputObject.foo' can only be defined once.", |
| 244 |
"locations": [(20, 15), (23, 15)], |
| 245 |
}, |
| 246 |
], |
| 247 |
) |
| 248 |
|
| 249 |
def adding_new_field_to_the_type_inside_existing_schema(): |
| 250 |
schema = build_schema(""" |
| 251 |
type SomeObject |
| 252 |
interface SomeInterface |
| 253 |
input SomeInputObject |
| 254 |
""") |
| 255 |
sdl = """ |
| 256 |
extend type SomeObject { |
| 257 |
foo: String |
| 258 |
} |
| 259 |
|
| 260 |
extend interface SomeInterface { |
| 261 |
foo: String |
| 262 |
} |
| 263 |
|
| 264 |
extend input SomeInputObject { |
| 265 |
foo: String |
| 266 |
} |
| 267 |
""" |
| 268 |
|
| 269 |
assert_valid(sdl, schema=schema) |
| 270 |
|
| 271 |
def adding_conflicting_fields_to_existing_schema_twice(): |
| 272 |
schema = build_schema(""" |
| 273 |
type SomeObject { |
| 274 |
foo: String |
| 275 |
} |
| 276 |
|
| 277 |
interface SomeInterface { |
| 278 |
foo: String |
| 279 |
} |
| 280 |
|
| 281 |
input SomeInputObject { |
| 282 |
foo: String |
| 283 |
} |
| 284 |
""") |
| 285 |
sdl = """ |
| 286 |
extend type SomeObject { |
| 287 |
foo: String |
| 288 |
} |
| 289 |
extend interface SomeInterface { |
| 290 |
foo: String |
| 291 |
} |
| 292 |
extend input SomeInputObject { |
| 293 |
foo: String |
| 294 |
} |
| 295 |
|
| 296 |
extend type SomeObject { |
| 297 |
foo: String |
| 298 |
} |
| 299 |
extend interface SomeInterface { |
| 300 |
foo: String |
| 301 |
} |
| 302 |
extend input SomeInputObject { |
| 303 |
foo: String |
| 304 |
} |
| 305 |
""" |
| 306 |
|
| 307 |
assert_errors( |
| 308 |
sdl, |
| 309 |
[ |
| 310 |
{ |
| 311 |
"message": "Field 'SomeObject.foo' already exists in the schema." |
| 312 |
" It cannot also be defined in this type extension.", |
| 313 |
"locations": [(3, 15)], |
| 314 |
}, |
| 315 |
{ |
| 316 |
"message": "Field 'SomeInterface.foo' already exists in the schema." |
| 317 |
" It cannot also be defined in this type extension.", |
| 318 |
"locations": [(6, 15)], |
| 319 |
}, |
| 320 |
{ |
| 321 |
"message": "Field 'SomeInputObject.foo'" |
| 322 |
" already exists in the schema." |
| 323 |
" It cannot also be defined in this type extension.", |
| 324 |
"locations": [(9, 15)], |
| 325 |
}, |
| 326 |
{ |
| 327 |
"message": "Field 'SomeObject.foo' already exists in the schema." |
| 328 |
" It cannot also be defined in this type extension.", |
| 329 |
"locations": [(13, 15)], |
| 330 |
}, |
| 331 |
{ |
| 332 |
"message": "Field 'SomeInterface.foo'" |
| 333 |
" already exists in the schema." |
| 334 |
" It cannot also be defined in this type extension.", |
| 335 |
"locations": [(16, 15)], |
| 336 |
}, |
| 337 |
{ |
| 338 |
"message": "Field 'SomeInputObject.foo'" |
| 339 |
" already exists in the schema." |
| 340 |
" It cannot also be defined in this type extension.", |
| 341 |
"locations": [(19, 15)], |
| 342 |
}, |
| 343 |
], |
| 344 |
schema, |
| 345 |
) |
| 346 |
|
| 347 |
def adding_fields_to_existing_schema_twice(): |
| 348 |
schema = build_schema(""" |
| 349 |
type SomeObject |
| 350 |
interface SomeInterface |
| 351 |
input SomeInputObject |
| 352 |
""") |
| 353 |
sdl = """ |
| 354 |
extend type SomeObject { |
| 355 |
foo: String |
| 356 |
} |
| 357 |
extend type SomeObject { |
| 358 |
foo: String |
| 359 |
} |
| 360 |
|
| 361 |
extend interface SomeInterface { |
| 362 |
foo: String |
| 363 |
} |
| 364 |
extend interface SomeInterface { |
| 365 |
foo: String |
| 366 |
} |
| 367 |
|
| 368 |
extend input SomeInputObject { |
| 369 |
foo: String |
| 370 |
} |
| 371 |
extend input SomeInputObject { |
| 372 |
foo: String |
| 373 |
} |
| 374 |
""" |
| 375 |
|
| 376 |
assert_errors( |
| 377 |
sdl, |
| 378 |
[ |
| 379 |
{ |
| 380 |
"message": "Field 'SomeObject.foo' can only be defined once.", |
| 381 |
"locations": [(3, 15), (6, 15)], |
| 382 |
}, |
| 383 |
{ |
| 384 |
"message": "Field 'SomeInterface.foo' can only be defined once.", |
| 385 |
"locations": [(10, 15), (13, 15)], |
| 386 |
}, |
| 387 |
{ |
| 388 |
"message": "Field 'SomeInputObject.foo' can only be defined once.", |
| 389 |
"locations": [(17, 15), (20, 15)], |
| 390 |
}, |
| 391 |
], |
| 392 |
schema, |
| 393 |
) |