| 1 |
from graphql.type import ( |
| 2 |
GraphQLSchema, |
| 3 |
GraphQLDeprecatedDirective, |
| 4 |
GraphQLIncludeDirective, |
| 5 |
GraphQLSkipDirective, |
| 6 |
GraphQLSpecifiedByDirective, |
| 7 |
GraphQLOneOfDirective, |
| 8 |
) |
| 9 |
from graphql.utilities import ( |
| 10 |
BreakingChangeType, |
| 11 |
DangerousChangeType, |
| 12 |
build_schema, |
| 13 |
find_breaking_changes, |
| 14 |
find_dangerous_changes, |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
def describe_find_breaking_changes(): |
| 19 |
def should_detect_if_a_type_was_removed_or_not(): |
| 20 |
old_schema = build_schema(""" |
| 21 |
type Type1 |
| 22 |
type Type2 |
| 23 |
""") |
| 24 |
|
| 25 |
new_schema = build_schema(""" |
| 26 |
type Type2 |
| 27 |
""") |
| 28 |
|
| 29 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 30 |
(BreakingChangeType.TYPE_REMOVED, "Type1 was removed.") |
| 31 |
] |
| 32 |
assert find_breaking_changes(old_schema, old_schema) == [] |
| 33 |
|
| 34 |
def should_detect_if_a_standard_scalar_was_removed(): |
| 35 |
old_schema = build_schema(""" |
| 36 |
type Query { |
| 37 |
foo: Float |
| 38 |
} |
| 39 |
""") |
| 40 |
|
| 41 |
new_schema = build_schema(""" |
| 42 |
type Query { |
| 43 |
foo: String |
| 44 |
} |
| 45 |
""") |
| 46 |
|
| 47 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 48 |
( |
| 49 |
BreakingChangeType.TYPE_REMOVED, |
| 50 |
"Standard scalar Float was removed" |
| 51 |
" because it is not referenced anymore.", |
| 52 |
), |
| 53 |
( |
| 54 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 55 |
"Query.foo changed type from Float to String.", |
| 56 |
), |
| 57 |
] |
| 58 |
|
| 59 |
def should_detect_if_a_type_changed_its_type(): |
| 60 |
old_schema = build_schema(""" |
| 61 |
scalar TypeWasScalarBecomesEnum |
| 62 |
interface TypeWasInterfaceBecomesUnion |
| 63 |
type TypeWasObjectBecomesInputObject |
| 64 |
""") |
| 65 |
|
| 66 |
new_schema = build_schema(""" |
| 67 |
enum TypeWasScalarBecomesEnum |
| 68 |
union TypeWasInterfaceBecomesUnion |
| 69 |
input TypeWasObjectBecomesInputObject |
| 70 |
""") |
| 71 |
|
| 72 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 73 |
( |
| 74 |
BreakingChangeType.TYPE_CHANGED_KIND, |
| 75 |
"TypeWasScalarBecomesEnum changed from a Scalar type to an Enum type.", |
| 76 |
), |
| 77 |
( |
| 78 |
BreakingChangeType.TYPE_CHANGED_KIND, |
| 79 |
"TypeWasInterfaceBecomesUnion changed" |
| 80 |
" from an Interface type to a Union type.", |
| 81 |
), |
| 82 |
( |
| 83 |
BreakingChangeType.TYPE_CHANGED_KIND, |
| 84 |
"TypeWasObjectBecomesInputObject changed" |
| 85 |
" from an Object type to an Input type.", |
| 86 |
), |
| 87 |
] |
| 88 |
|
| 89 |
def should_detect_if_a_field_on_type_was_deleted_or_changed_type(): |
| 90 |
old_schema = build_schema(""" |
| 91 |
type TypeA |
| 92 |
type TypeB |
| 93 |
|
| 94 |
interface Type1 { |
| 95 |
field1: TypeA |
| 96 |
field2: String |
| 97 |
field3: String |
| 98 |
field4: TypeA |
| 99 |
field6: String |
| 100 |
field7: [String] |
| 101 |
field8: Int |
| 102 |
field9: Int! |
| 103 |
field10: [Int]! |
| 104 |
field11: Int |
| 105 |
field12: [Int] |
| 106 |
field13: [Int!] |
| 107 |
field14: [Int] |
| 108 |
field15: [[Int]] |
| 109 |
field16: Int! |
| 110 |
field17: [Int] |
| 111 |
field18: [[Int!]!] |
| 112 |
} |
| 113 |
""") |
| 114 |
|
| 115 |
new_schema = build_schema(""" |
| 116 |
type TypeA |
| 117 |
type TypeB |
| 118 |
|
| 119 |
interface Type1 { |
| 120 |
field1: TypeA |
| 121 |
field3: Boolean |
| 122 |
field4: TypeB |
| 123 |
field5: String |
| 124 |
field6: [String] |
| 125 |
field7: String |
| 126 |
field8: Int! |
| 127 |
field9: Int |
| 128 |
field10: [Int] |
| 129 |
field11: [Int]! |
| 130 |
field12: [Int!] |
| 131 |
field13: [Int] |
| 132 |
field14: [[Int]] |
| 133 |
field15: [Int] |
| 134 |
field16: [Int]! |
| 135 |
field17: [Int]! |
| 136 |
field18: [[Int!]] |
| 137 |
} |
| 138 |
""") |
| 139 |
|
| 140 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 141 |
(BreakingChangeType.FIELD_REMOVED, "Type1.field2 was removed."), |
| 142 |
( |
| 143 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 144 |
"Type1.field3 changed type from String to Boolean.", |
| 145 |
), |
| 146 |
( |
| 147 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 148 |
"Type1.field4 changed type from TypeA to TypeB.", |
| 149 |
), |
| 150 |
( |
| 151 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 152 |
"Type1.field6 changed type from String to [String].", |
| 153 |
), |
| 154 |
( |
| 155 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 156 |
"Type1.field7 changed type from [String] to String.", |
| 157 |
), |
| 158 |
( |
| 159 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 160 |
"Type1.field9 changed type from Int! to Int.", |
| 161 |
), |
| 162 |
( |
| 163 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 164 |
"Type1.field10 changed type from [Int]! to [Int].", |
| 165 |
), |
| 166 |
( |
| 167 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 168 |
"Type1.field11 changed type from Int to [Int]!.", |
| 169 |
), |
| 170 |
( |
| 171 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 172 |
"Type1.field13 changed type from [Int!] to [Int].", |
| 173 |
), |
| 174 |
( |
| 175 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 176 |
"Type1.field14 changed type from [Int] to [[Int]].", |
| 177 |
), |
| 178 |
( |
| 179 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 180 |
"Type1.field15 changed type from [[Int]] to [Int].", |
| 181 |
), |
| 182 |
( |
| 183 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 184 |
"Type1.field16 changed type from Int! to [Int]!.", |
| 185 |
), |
| 186 |
( |
| 187 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 188 |
"Type1.field18 changed type from [[Int!]!] to [[Int!]].", |
| 189 |
), |
| 190 |
] |
| 191 |
|
| 192 |
def should_detect_if_fields_on_input_types_changed_kind_or_were_removed(): |
| 193 |
old_schema = build_schema(""" |
| 194 |
input InputType1 { |
| 195 |
field1: String |
| 196 |
field2: Boolean |
| 197 |
field3: [String] |
| 198 |
field4: String! |
| 199 |
field5: String |
| 200 |
field6: [Int] |
| 201 |
field7: [Int]! |
| 202 |
field8: Int |
| 203 |
field9: [Int] |
| 204 |
field10: [Int!] |
| 205 |
field11: [Int] |
| 206 |
field12: [[Int]] |
| 207 |
field13: Int! |
| 208 |
field14: [[Int]!] |
| 209 |
field15: [[Int]!] |
| 210 |
} |
| 211 |
""") |
| 212 |
|
| 213 |
new_schema = build_schema(""" |
| 214 |
input InputType1 { |
| 215 |
field1: Int |
| 216 |
field3: String |
| 217 |
field4: String |
| 218 |
field5: String! |
| 219 |
field6: [Int]! |
| 220 |
field7: [Int] |
| 221 |
field8: [Int]! |
| 222 |
field9: [Int!] |
| 223 |
field10: [Int] |
| 224 |
field11: [[Int]] |
| 225 |
field12: [Int] |
| 226 |
field13: [Int]! |
| 227 |
field14: [[Int]] |
| 228 |
field15: [[Int!]!] |
| 229 |
} |
| 230 |
""") |
| 231 |
|
| 232 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 233 |
(BreakingChangeType.FIELD_REMOVED, "InputType1.field2 was removed."), |
| 234 |
( |
| 235 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 236 |
"InputType1.field1 changed type from String to Int.", |
| 237 |
), |
| 238 |
( |
| 239 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 240 |
"InputType1.field3 changed type from [String] to String.", |
| 241 |
), |
| 242 |
( |
| 243 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 244 |
"InputType1.field5 changed type from String to String!.", |
| 245 |
), |
| 246 |
( |
| 247 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 248 |
"InputType1.field6 changed type from [Int] to [Int]!.", |
| 249 |
), |
| 250 |
( |
| 251 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 252 |
"InputType1.field8 changed type from Int to [Int]!.", |
| 253 |
), |
| 254 |
( |
| 255 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 256 |
"InputType1.field9 changed type from [Int] to [Int!].", |
| 257 |
), |
| 258 |
( |
| 259 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 260 |
"InputType1.field11 changed type from [Int] to [[Int]].", |
| 261 |
), |
| 262 |
( |
| 263 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 264 |
"InputType1.field12 changed type from [[Int]] to [Int].", |
| 265 |
), |
| 266 |
( |
| 267 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 268 |
"InputType1.field13 changed type from Int! to [Int]!.", |
| 269 |
), |
| 270 |
( |
| 271 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 272 |
"InputType1.field15 changed type from [[Int]!] to [[Int!]!].", |
| 273 |
), |
| 274 |
] |
| 275 |
|
| 276 |
def should_detect_if_a_required_field_is_added_to_an_input_type(): |
| 277 |
old_schema = build_schema(""" |
| 278 |
input InputType1 { |
| 279 |
field1: String |
| 280 |
} |
| 281 |
""") |
| 282 |
|
| 283 |
new_schema = build_schema(""" |
| 284 |
input InputType1 { |
| 285 |
field1: String |
| 286 |
requiredField: Int! |
| 287 |
optionalField1: Boolean |
| 288 |
optionalField2: Boolean! = false |
| 289 |
} |
| 290 |
""") |
| 291 |
|
| 292 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 293 |
( |
| 294 |
BreakingChangeType.REQUIRED_INPUT_FIELD_ADDED, |
| 295 |
"A required field requiredField on input type InputType1 was added.", |
| 296 |
) |
| 297 |
] |
| 298 |
|
| 299 |
def should_detect_if_a_type_was_removed_from_a_union_type(): |
| 300 |
old_schema = build_schema(""" |
| 301 |
type Type1 |
| 302 |
type Type2 |
| 303 |
type Type3 |
| 304 |
|
| 305 |
union UnionType1 = Type1 | Type2 |
| 306 |
""") |
| 307 |
|
| 308 |
new_schema = build_schema(""" |
| 309 |
type Type1 |
| 310 |
type Type2 |
| 311 |
type Type3 |
| 312 |
|
| 313 |
union UnionType1 = Type1 | Type3 |
| 314 |
""") |
| 315 |
|
| 316 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 317 |
( |
| 318 |
BreakingChangeType.TYPE_REMOVED_FROM_UNION, |
| 319 |
"Type2 was removed from union type UnionType1.", |
| 320 |
) |
| 321 |
] |
| 322 |
|
| 323 |
def should_detect_if_a_value_was_removed_from_an_enum_type(): |
| 324 |
old_schema = build_schema(""" |
| 325 |
enum EnumType1 { |
| 326 |
VALUE0 |
| 327 |
VALUE1 |
| 328 |
VALUE2 |
| 329 |
} |
| 330 |
""") |
| 331 |
|
| 332 |
new_schema = build_schema(""" |
| 333 |
enum EnumType1 { |
| 334 |
VALUE0 |
| 335 |
VALUE2 |
| 336 |
VALUE3 |
| 337 |
} |
| 338 |
""") |
| 339 |
|
| 340 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 341 |
( |
| 342 |
BreakingChangeType.VALUE_REMOVED_FROM_ENUM, |
| 343 |
"VALUE1 was removed from enum type EnumType1.", |
| 344 |
) |
| 345 |
] |
| 346 |
|
| 347 |
def should_detect_if_a_field_argument_was_removed(): |
| 348 |
old_schema = build_schema(""" |
| 349 |
interface Interface1 { |
| 350 |
field1(arg1: Boolean, objectArg: String): String |
| 351 |
} |
| 352 |
|
| 353 |
type Type1 { |
| 354 |
field1(name: String): String |
| 355 |
} |
| 356 |
""") |
| 357 |
|
| 358 |
new_schema = build_schema(""" |
| 359 |
interface Interface1 { |
| 360 |
field1: String |
| 361 |
} |
| 362 |
|
| 363 |
type Type1 { |
| 364 |
field1: String |
| 365 |
} |
| 366 |
""") |
| 367 |
|
| 368 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 369 |
(BreakingChangeType.ARG_REMOVED, "Interface1.field1 arg arg1 was removed."), |
| 370 |
( |
| 371 |
BreakingChangeType.ARG_REMOVED, |
| 372 |
"Interface1.field1 arg objectArg was removed.", |
| 373 |
), |
| 374 |
(BreakingChangeType.ARG_REMOVED, "Type1.field1 arg name was removed."), |
| 375 |
] |
| 376 |
|
| 377 |
def should_detect_if_a_field_argument_has_changed_type(): |
| 378 |
old_schema = build_schema(""" |
| 379 |
type Type1 { |
| 380 |
field1( |
| 381 |
arg1: String |
| 382 |
arg2: String |
| 383 |
arg3: [String] |
| 384 |
arg4: String |
| 385 |
arg5: String! |
| 386 |
arg6: String! |
| 387 |
arg7: [Int]! |
| 388 |
arg8: Int |
| 389 |
arg9: [Int] |
| 390 |
arg10: [Int!] |
| 391 |
arg11: [Int] |
| 392 |
arg12: [[Int]] |
| 393 |
arg13: Int! |
| 394 |
arg14: [[Int]!] |
| 395 |
arg15: [[Int]!] |
| 396 |
): String |
| 397 |
} |
| 398 |
""") |
| 399 |
|
| 400 |
new_schema = build_schema(""" |
| 401 |
type Type1 { |
| 402 |
field1( |
| 403 |
arg1: Int |
| 404 |
arg2: [String] |
| 405 |
arg3: String |
| 406 |
arg4: String! |
| 407 |
arg5: Int |
| 408 |
arg6: Int! |
| 409 |
arg7: [Int] |
| 410 |
arg8: [Int]! |
| 411 |
arg9: [Int!] |
| 412 |
arg10: [Int] |
| 413 |
arg11: [[Int]] |
| 414 |
arg12: [Int] |
| 415 |
arg13: [Int]! |
| 416 |
arg14: [[Int]] |
| 417 |
arg15: [[Int!]!] |
| 418 |
): String |
| 419 |
} |
| 420 |
""") |
| 421 |
|
| 422 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 423 |
( |
| 424 |
BreakingChangeType.ARG_CHANGED_KIND, |
| 425 |
"Type1.field1 arg arg1 has changed type from String to Int.", |
| 426 |
), |
| 427 |
( |
| 428 |
BreakingChangeType.ARG_CHANGED_KIND, |
| 429 |
"Type1.field1 arg arg2 has changed type from String to [String].", |
| 430 |
), |
| 431 |
( |
| 432 |
BreakingChangeType.ARG_CHANGED_KIND, |
| 433 |
"Type1.field1 arg arg3 has changed type from [String] to String.", |
| 434 |
), |
| 435 |
( |
| 436 |
BreakingChangeType.ARG_CHANGED_KIND, |
| 437 |
"Type1.field1 arg arg4 has changed type from String to String!.", |
| 438 |
), |
| 439 |
( |
| 440 |
BreakingChangeType.ARG_CHANGED_KIND, |
| 441 |
"Type1.field1 arg arg5 has changed type from String! to Int.", |
| 442 |
), |
| 443 |
( |
| 444 |
BreakingChangeType.ARG_CHANGED_KIND, |
| 445 |
"Type1.field1 arg arg6 has changed type from String! to Int!.", |
| 446 |
), |
| 447 |
( |
| 448 |
BreakingChangeType.ARG_CHANGED_KIND, |
| 449 |
"Type1.field1 arg arg8 has changed type from Int to [Int]!.", |
| 450 |
), |
| 451 |
( |
| 452 |
BreakingChangeType.ARG_CHANGED_KIND, |
| 453 |
"Type1.field1 arg arg9 has changed type from [Int] to [Int!].", |
| 454 |
), |
| 455 |
( |
| 456 |
BreakingChangeType.ARG_CHANGED_KIND, |
| 457 |
"Type1.field1 arg arg11 has changed type from [Int] to [[Int]].", |
| 458 |
), |
| 459 |
( |
| 460 |
BreakingChangeType.ARG_CHANGED_KIND, |
| 461 |
"Type1.field1 arg arg12 has changed type from [[Int]] to [Int].", |
| 462 |
), |
| 463 |
( |
| 464 |
BreakingChangeType.ARG_CHANGED_KIND, |
| 465 |
"Type1.field1 arg arg13 has changed type from Int! to [Int]!.", |
| 466 |
), |
| 467 |
( |
| 468 |
BreakingChangeType.ARG_CHANGED_KIND, |
| 469 |
"Type1.field1 arg arg15 has changed type from [[Int]!] to [[Int!]!].", |
| 470 |
), |
| 471 |
] |
| 472 |
|
| 473 |
def should_detect_if_a_required_field_argument_was_added(): |
| 474 |
old_schema = build_schema(""" |
| 475 |
type Type1 { |
| 476 |
field1(arg1: String): String |
| 477 |
} |
| 478 |
""") |
| 479 |
|
| 480 |
new_schema = build_schema(""" |
| 481 |
type Type1 { |
| 482 |
field1( |
| 483 |
arg1: String, |
| 484 |
newRequiredArg: String! |
| 485 |
newOptionalArg1: Int |
| 486 |
newOptionalArg2: Int! = 0 |
| 487 |
): String |
| 488 |
} |
| 489 |
""") |
| 490 |
|
| 491 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 492 |
( |
| 493 |
BreakingChangeType.REQUIRED_ARG_ADDED, |
| 494 |
"A required arg newRequiredArg on Type1.field1 was added.", |
| 495 |
) |
| 496 |
] |
| 497 |
|
| 498 |
def should_not_flag_args_with_the_same_type_signature_as_breaking(): |
| 499 |
old_schema = build_schema(""" |
| 500 |
input InputType1 { |
| 501 |
field1: String |
| 502 |
} |
| 503 |
|
| 504 |
type Type1 { |
| 505 |
field1(arg1: Int!, arg2: InputType1): Int |
| 506 |
} |
| 507 |
""") |
| 508 |
|
| 509 |
new_schema = build_schema(""" |
| 510 |
input InputType1 { |
| 511 |
field1: String |
| 512 |
} |
| 513 |
|
| 514 |
type Type1 { |
| 515 |
field1(arg1: Int!, arg2: InputType1): Int |
| 516 |
} |
| 517 |
""") |
| 518 |
|
| 519 |
assert find_breaking_changes(old_schema, new_schema) == [] |
| 520 |
|
| 521 |
def should_consider_args_that_move_away_from_non_null_as_non_breaking(): |
| 522 |
old_schema = build_schema(""" |
| 523 |
type Type1 { |
| 524 |
field1(name: String!): String |
| 525 |
} |
| 526 |
""") |
| 527 |
|
| 528 |
new_schema = build_schema(""" |
| 529 |
type Type1 { |
| 530 |
field1(name: String): String |
| 531 |
} |
| 532 |
""") |
| 533 |
|
| 534 |
assert find_breaking_changes(old_schema, new_schema) == [] |
| 535 |
|
| 536 |
def should_detect_interfaces_removed_from_types(): |
| 537 |
old_schema = build_schema(""" |
| 538 |
interface Interface1 |
| 539 |
|
| 540 |
type Type1 implements Interface1 |
| 541 |
""") |
| 542 |
|
| 543 |
new_schema = build_schema(""" |
| 544 |
interface Interface1 |
| 545 |
|
| 546 |
type Type1 |
| 547 |
""") |
| 548 |
|
| 549 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 550 |
( |
| 551 |
BreakingChangeType.IMPLEMENTED_INTERFACE_REMOVED, |
| 552 |
"Type1 no longer implements interface Interface1.", |
| 553 |
) |
| 554 |
] |
| 555 |
|
| 556 |
def should_detect_intrefaces_removed_from_interfaces(): |
| 557 |
old_schema = build_schema(""" |
| 558 |
interface Interface1 |
| 559 |
|
| 560 |
interface Interface2 implements Interface1 |
| 561 |
""") |
| 562 |
|
| 563 |
new_schema = build_schema(""" |
| 564 |
interface Interface1 |
| 565 |
|
| 566 |
interface Interface2 |
| 567 |
""") |
| 568 |
|
| 569 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 570 |
( |
| 571 |
BreakingChangeType.IMPLEMENTED_INTERFACE_REMOVED, |
| 572 |
"Interface2 no longer implements interface Interface1.", |
| 573 |
) |
| 574 |
] |
| 575 |
|
| 576 |
def should_ignore_changes_in_order_of_interfaces(): |
| 577 |
old_schema = build_schema(""" |
| 578 |
interface FirstInterface |
| 579 |
interface SecondInterface |
| 580 |
|
| 581 |
type Type1 implements FirstInterface & SecondInterface |
| 582 |
""") |
| 583 |
|
| 584 |
new_schema = build_schema(""" |
| 585 |
interface FirstInterface |
| 586 |
interface SecondInterface |
| 587 |
|
| 588 |
type Type1 implements SecondInterface & FirstInterface |
| 589 |
""") |
| 590 |
|
| 591 |
assert find_breaking_changes(old_schema, new_schema) == [] |
| 592 |
|
| 593 |
def should_detect_all_breaking_changes(): |
| 594 |
old_schema = build_schema(""" |
| 595 |
directive @DirectiveThatIsRemoved on FIELD_DEFINITION |
| 596 |
|
| 597 |
directive @DirectiveThatRemovesArg(arg1: String) on FIELD_DEFINITION |
| 598 |
|
| 599 |
directive @NonNullDirectiveAdded on FIELD_DEFINITION |
| 600 |
|
| 601 |
directive @DirectiveThatWasRepeatable repeatable on FIELD_DEFINITION |
| 602 |
|
| 603 |
directive @DirectiveName on FIELD_DEFINITION | QUERY |
| 604 |
|
| 605 |
type ArgThatChanges { |
| 606 |
field1(id: Float): String |
| 607 |
} |
| 608 |
|
| 609 |
enum EnumTypeThatLosesAValue { |
| 610 |
VALUE0 |
| 611 |
VALUE1 |
| 612 |
VALUE2 |
| 613 |
} |
| 614 |
|
| 615 |
interface Interface1 |
| 616 |
type TypeThatLoosesInterface1 implements Interface1 |
| 617 |
|
| 618 |
type TypeInUnion1 |
| 619 |
type TypeInUnion2 |
| 620 |
union UnionTypeThatLosesAType = TypeInUnion1 | TypeInUnion2 |
| 621 |
|
| 622 |
type TypeThatChangesType |
| 623 |
|
| 624 |
type TypeThatGetsRemoved |
| 625 |
|
| 626 |
interface TypeThatHasBreakingFieldChanges { |
| 627 |
field1: String |
| 628 |
field2: String |
| 629 |
} |
| 630 |
""") |
| 631 |
|
| 632 |
new_schema = build_schema(""" |
| 633 |
directive @DirectiveThatRemovesArg on FIELD_DEFINITION |
| 634 |
|
| 635 |
directive @NonNullDirectiveAdded(arg1: Boolean!) on FIELD_DEFINITION |
| 636 |
|
| 637 |
directive @DirectiveThatWasRepeatable on FIELD_DEFINITION |
| 638 |
|
| 639 |
directive @DirectiveName on FIELD_DEFINITION |
| 640 |
|
| 641 |
type ArgThatChanges { |
| 642 |
field1(id: String): String |
| 643 |
} |
| 644 |
|
| 645 |
enum EnumTypeThatLosesAValue { |
| 646 |
VALUE1 |
| 647 |
VALUE2 |
| 648 |
} |
| 649 |
|
| 650 |
interface Interface1 |
| 651 |
type TypeThatLoosesInterface1 |
| 652 |
|
| 653 |
type TypeInUnion1 |
| 654 |
type TypeInUnion2 |
| 655 |
|
| 656 |
union UnionTypeThatLosesAType = TypeInUnion1 |
| 657 |
|
| 658 |
interface TypeThatChangesType |
| 659 |
|
| 660 |
interface TypeThatHasBreakingFieldChanges { |
| 661 |
field2: Boolean |
| 662 |
} |
| 663 |
""") |
| 664 |
|
| 665 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 666 |
( |
| 667 |
BreakingChangeType.TYPE_REMOVED, |
| 668 |
"Standard scalar Float was removed" |
| 669 |
" because it is not referenced anymore.", |
| 670 |
), |
| 671 |
(BreakingChangeType.TYPE_REMOVED, "TypeThatGetsRemoved was removed."), |
| 672 |
( |
| 673 |
BreakingChangeType.ARG_CHANGED_KIND, |
| 674 |
"ArgThatChanges.field1 arg id has changed type from Float to String.", |
| 675 |
), |
| 676 |
( |
| 677 |
BreakingChangeType.VALUE_REMOVED_FROM_ENUM, |
| 678 |
"VALUE0 was removed from enum type EnumTypeThatLosesAValue.", |
| 679 |
), |
| 680 |
( |
| 681 |
BreakingChangeType.IMPLEMENTED_INTERFACE_REMOVED, |
| 682 |
"TypeThatLoosesInterface1 no longer implements interface Interface1.", |
| 683 |
), |
| 684 |
( |
| 685 |
BreakingChangeType.TYPE_REMOVED_FROM_UNION, |
| 686 |
"TypeInUnion2 was removed from union type UnionTypeThatLosesAType.", |
| 687 |
), |
| 688 |
( |
| 689 |
BreakingChangeType.TYPE_CHANGED_KIND, |
| 690 |
"TypeThatChangesType changed from an Object type to an" |
| 691 |
" Interface type.", |
| 692 |
), |
| 693 |
( |
| 694 |
BreakingChangeType.FIELD_REMOVED, |
| 695 |
"TypeThatHasBreakingFieldChanges.field1 was removed.", |
| 696 |
), |
| 697 |
( |
| 698 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 699 |
"TypeThatHasBreakingFieldChanges.field2 changed type" |
| 700 |
" from String to Boolean.", |
| 701 |
), |
| 702 |
( |
| 703 |
BreakingChangeType.DIRECTIVE_REMOVED, |
| 704 |
"DirectiveThatIsRemoved was removed.", |
| 705 |
), |
| 706 |
( |
| 707 |
BreakingChangeType.DIRECTIVE_ARG_REMOVED, |
| 708 |
"arg1 was removed from DirectiveThatRemovesArg.", |
| 709 |
), |
| 710 |
( |
| 711 |
BreakingChangeType.REQUIRED_DIRECTIVE_ARG_ADDED, |
| 712 |
"A required arg arg1 on directive NonNullDirectiveAdded was added.", |
| 713 |
), |
| 714 |
( |
| 715 |
BreakingChangeType.DIRECTIVE_REPEATABLE_REMOVED, |
| 716 |
"Repeatable flag was removed from DirectiveThatWasRepeatable.", |
| 717 |
), |
| 718 |
( |
| 719 |
BreakingChangeType.DIRECTIVE_LOCATION_REMOVED, |
| 720 |
"QUERY was removed from DirectiveName.", |
| 721 |
), |
| 722 |
] |
| 723 |
|
| 724 |
def should_detect_if_a_directive_was_explicitly_removed(): |
| 725 |
old_schema = build_schema(""" |
| 726 |
directive @DirectiveThatIsRemoved on FIELD_DEFINITION |
| 727 |
directive @DirectiveThatStays on FIELD_DEFINITION |
| 728 |
""") |
| 729 |
|
| 730 |
new_schema = build_schema(""" |
| 731 |
directive @DirectiveThatStays on FIELD_DEFINITION |
| 732 |
""") |
| 733 |
|
| 734 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 735 |
( |
| 736 |
BreakingChangeType.DIRECTIVE_REMOVED, |
| 737 |
"DirectiveThatIsRemoved was removed.", |
| 738 |
) |
| 739 |
] |
| 740 |
|
| 741 |
def should_detect_if_a_directive_was_implicitly_removed(): |
| 742 |
old_schema = GraphQLSchema() |
| 743 |
|
| 744 |
new_schema = GraphQLSchema( |
| 745 |
directives=[ |
| 746 |
GraphQLSkipDirective, |
| 747 |
GraphQLIncludeDirective, |
| 748 |
GraphQLSpecifiedByDirective, |
| 749 |
GraphQLOneOfDirective, |
| 750 |
] |
| 751 |
) |
| 752 |
|
| 753 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 754 |
( |
| 755 |
BreakingChangeType.DIRECTIVE_REMOVED, |
| 756 |
f"{GraphQLDeprecatedDirective.name} was removed.", |
| 757 |
) |
| 758 |
] |
| 759 |
|
| 760 |
def should_detect_if_a_directive_argument_was_removed(): |
| 761 |
old_schema = build_schema(""" |
| 762 |
directive @DirectiveWithArg(arg1: String) on FIELD_DEFINITION |
| 763 |
""") |
| 764 |
|
| 765 |
new_schema = build_schema(""" |
| 766 |
directive @DirectiveWithArg on FIELD_DEFINITION |
| 767 |
""") |
| 768 |
|
| 769 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 770 |
( |
| 771 |
BreakingChangeType.DIRECTIVE_ARG_REMOVED, |
| 772 |
"arg1 was removed from DirectiveWithArg.", |
| 773 |
) |
| 774 |
] |
| 775 |
|
| 776 |
def should_detect_if_an_optional_directive_argument_was_added(): |
| 777 |
old_schema = build_schema(""" |
| 778 |
directive @DirectiveName on FIELD_DEFINITION |
| 779 |
""") |
| 780 |
|
| 781 |
new_schema = build_schema(""" |
| 782 |
directive @DirectiveName( |
| 783 |
newRequiredArg: String! |
| 784 |
newOptionalArg1: Int |
| 785 |
newOptionalArg2: Int! = 0 |
| 786 |
) on FIELD_DEFINITION |
| 787 |
""") |
| 788 |
|
| 789 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 790 |
( |
| 791 |
BreakingChangeType.REQUIRED_DIRECTIVE_ARG_ADDED, |
| 792 |
"A required arg newRequiredArg on directive DirectiveName was added.", |
| 793 |
) |
| 794 |
] |
| 795 |
|
| 796 |
def should_detect_removal_of_repeatable_flag(): |
| 797 |
old_schema = build_schema(""" |
| 798 |
directive @DirectiveName repeatable on OBJECT |
| 799 |
""") |
| 800 |
|
| 801 |
new_schema = build_schema(""" |
| 802 |
directive @DirectiveName on OBJECT |
| 803 |
""") |
| 804 |
|
| 805 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 806 |
( |
| 807 |
BreakingChangeType.DIRECTIVE_REPEATABLE_REMOVED, |
| 808 |
"Repeatable flag was removed from DirectiveName.", |
| 809 |
) |
| 810 |
] |
| 811 |
|
| 812 |
def should_detect_locations_removed_from_a_directive(): |
| 813 |
old_schema = build_schema(""" |
| 814 |
directive @DirectiveName on FIELD_DEFINITION | QUERY |
| 815 |
""") |
| 816 |
|
| 817 |
new_schema = build_schema(""" |
| 818 |
directive @DirectiveName on FIELD_DEFINITION |
| 819 |
""") |
| 820 |
|
| 821 |
assert find_breaking_changes(old_schema, new_schema) == [ |
| 822 |
( |
| 823 |
BreakingChangeType.DIRECTIVE_LOCATION_REMOVED, |
| 824 |
"QUERY was removed from DirectiveName.", |
| 825 |
) |
| 826 |
] |
| 827 |
|
| 828 |
|
| 829 |
def describe_find_dangerous_changes(): |
| 830 |
def should_detect_if_a_default_value_changed_on_an_argument(): |
| 831 |
old_sdl = """ |
| 832 |
input Input1 { |
| 833 |
innerInputArray: [Input2] |
| 834 |
} |
| 835 |
|
| 836 |
input Input2 { |
| 837 |
arrayField: [Int] |
| 838 |
} |
| 839 |
|
| 840 |
type Type1 { |
| 841 |
field1( |
| 842 |
withDefaultValue: String = "TO BE DELETED" |
| 843 |
stringArg: String = "test" |
| 844 |
emptyArray: [Int!] = [] |
| 845 |
valueArray: [[String]] = [["a", "b"], ["c"]] |
| 846 |
complexObject: Input1 = { |
| 847 |
innerInputArray: [{ arrayField: [1, 2, 3] }] |
| 848 |
} |
| 849 |
): String |
| 850 |
} |
| 851 |
""" |
| 852 |
|
| 853 |
old_schema = build_schema(old_sdl) |
| 854 |
copy_of_old_schema = build_schema(old_sdl) |
| 855 |
assert find_dangerous_changes(old_schema, copy_of_old_schema) == [] |
| 856 |
|
| 857 |
new_schema = build_schema(""" |
| 858 |
input Input1 { |
| 859 |
innerInputArray: [Input2] |
| 860 |
} |
| 861 |
|
| 862 |
input Input2 { |
| 863 |
arrayField: [Int] |
| 864 |
} |
| 865 |
|
| 866 |
type Type1 { |
| 867 |
field1( |
| 868 |
withDefaultValue: String |
| 869 |
stringArg: String = "Test" |
| 870 |
emptyArray: [Int!] = [7] |
| 871 |
valueArray: [[String]] = [["b", "a"], ["d"]] |
| 872 |
complexObject: Input1 = { |
| 873 |
innerInputArray: [{ arrayField: [3, 2, 1] }] |
| 874 |
} |
| 875 |
): String |
| 876 |
} |
| 877 |
""") |
| 878 |
|
| 879 |
assert find_dangerous_changes(old_schema, new_schema) == [ |
| 880 |
( |
| 881 |
DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, |
| 882 |
"Type1.field1 arg withDefaultValue defaultValue was removed.", |
| 883 |
), |
| 884 |
( |
| 885 |
DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, |
| 886 |
"Type1.field1 arg stringArg has changed defaultValue" |
| 887 |
' from "test" to "Test".', |
| 888 |
), |
| 889 |
( |
| 890 |
DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, |
| 891 |
"Type1.field1 arg emptyArray has changed defaultValue from [] to [7].", |
| 892 |
), |
| 893 |
( |
| 894 |
DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, |
| 895 |
"Type1.field1 arg valueArray has changed defaultValue" |
| 896 |
' from [["a", "b"], ["c"]] to [["b", "a"], ["d"]].', |
| 897 |
), |
| 898 |
( |
| 899 |
DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, |
| 900 |
"Type1.field1 arg complexObject has changed defaultValue" |
| 901 |
" from {innerInputArray: [{arrayField: [1, 2, 3]}]}" |
| 902 |
" to {innerInputArray: [{arrayField: [3, 2, 1]}]}.", |
| 903 |
), |
| 904 |
] |
| 905 |
|
| 906 |
def should_ignore_changes_in_field_order_of_default_value(): |
| 907 |
old_schema = build_schema(""" |
| 908 |
input Input1 { |
| 909 |
a: String |
| 910 |
b: String |
| 911 |
c: String |
| 912 |
} |
| 913 |
|
| 914 |
type Type1 { |
| 915 |
field1( |
| 916 |
arg1: Input1 = { a: "a", b: "b", c: "c" } |
| 917 |
): String |
| 918 |
} |
| 919 |
""") |
| 920 |
|
| 921 |
new_schema = build_schema(""" |
| 922 |
input Input1 { |
| 923 |
a: String |
| 924 |
b: String |
| 925 |
c: String |
| 926 |
} |
| 927 |
|
| 928 |
type Type1 { |
| 929 |
field1( |
| 930 |
arg1: Input1 = { c: "c", b: "b", a: "a" } |
| 931 |
): String |
| 932 |
} |
| 933 |
""") |
| 934 |
|
| 935 |
assert find_dangerous_changes(old_schema, new_schema) == [] |
| 936 |
|
| 937 |
def should_ignore_changes_in_field_definitions_order(): |
| 938 |
old_schema = build_schema(""" |
| 939 |
input Input1 { |
| 940 |
a: String |
| 941 |
b: String |
| 942 |
c: String |
| 943 |
} |
| 944 |
|
| 945 |
type Type1 { |
| 946 |
field1( |
| 947 |
arg1: Input1 = { a: "a", b: "b", c: "c" } |
| 948 |
): String |
| 949 |
} |
| 950 |
""") |
| 951 |
|
| 952 |
new_schema = build_schema(""" |
| 953 |
input Input1 { |
| 954 |
c: String |
| 955 |
b: String |
| 956 |
a: String |
| 957 |
} |
| 958 |
|
| 959 |
type Type1 { |
| 960 |
field1( |
| 961 |
arg1: Input1 = { a: "a", b: "b", c: "c" } |
| 962 |
): String |
| 963 |
} |
| 964 |
""") |
| 965 |
|
| 966 |
assert find_dangerous_changes(old_schema, new_schema) == [] |
| 967 |
|
| 968 |
def should_detect_if_a_value_was_added_to_an_enum_type(): |
| 969 |
old_schema = build_schema(""" |
| 970 |
enum EnumType1 { |
| 971 |
VALUE0 |
| 972 |
VALUE1 |
| 973 |
} |
| 974 |
""") |
| 975 |
|
| 976 |
new_schema = build_schema(""" |
| 977 |
enum EnumType1 { |
| 978 |
VALUE0 |
| 979 |
VALUE1 |
| 980 |
VALUE2 |
| 981 |
} |
| 982 |
""") |
| 983 |
|
| 984 |
assert find_dangerous_changes(old_schema, new_schema) == [ |
| 985 |
( |
| 986 |
DangerousChangeType.VALUE_ADDED_TO_ENUM, |
| 987 |
"VALUE2 was added to enum type EnumType1.", |
| 988 |
) |
| 989 |
] |
| 990 |
|
| 991 |
def should_detect_interfaces_added_to_types(): |
| 992 |
old_schema = build_schema(""" |
| 993 |
interface OldInterface |
| 994 |
interface NewInterface |
| 995 |
|
| 996 |
type Type1 implements OldInterface |
| 997 |
""") |
| 998 |
|
| 999 |
new_schema = build_schema(""" |
| 1000 |
interface OldInterface |
| 1001 |
interface NewInterface |
| 1002 |
|
| 1003 |
type Type1 implements OldInterface & NewInterface |
| 1004 |
""") |
| 1005 |
|
| 1006 |
assert find_dangerous_changes(old_schema, new_schema) == [ |
| 1007 |
( |
| 1008 |
DangerousChangeType.IMPLEMENTED_INTERFACE_ADDED, |
| 1009 |
"NewInterface added to interfaces implemented by Type1.", |
| 1010 |
) |
| 1011 |
] |
| 1012 |
|
| 1013 |
def should_detect_interfaces_added_to_interfaces(): |
| 1014 |
old_schema = build_schema(""" |
| 1015 |
interface OldInterface |
| 1016 |
interface NewInterface |
| 1017 |
|
| 1018 |
interface Interface1 implements OldInterface |
| 1019 |
""") |
| 1020 |
|
| 1021 |
new_schema = build_schema(""" |
| 1022 |
interface OldInterface |
| 1023 |
interface NewInterface |
| 1024 |
|
| 1025 |
interface Interface1 implements OldInterface & NewInterface |
| 1026 |
""") |
| 1027 |
|
| 1028 |
assert find_dangerous_changes(old_schema, new_schema) == [ |
| 1029 |
( |
| 1030 |
DangerousChangeType.IMPLEMENTED_INTERFACE_ADDED, |
| 1031 |
"NewInterface added to interfaces implemented by Interface1.", |
| 1032 |
) |
| 1033 |
] |
| 1034 |
|
| 1035 |
def should_detect_if_a_type_was_added_to_a_union_type(): |
| 1036 |
old_schema = build_schema(""" |
| 1037 |
type Type1 |
| 1038 |
type Type2 |
| 1039 |
|
| 1040 |
union UnionType1 = Type1 |
| 1041 |
""") |
| 1042 |
|
| 1043 |
new_schema = build_schema(""" |
| 1044 |
type Type1 |
| 1045 |
type Type2 |
| 1046 |
|
| 1047 |
union UnionType1 = Type1 | Type2 |
| 1048 |
""") |
| 1049 |
|
| 1050 |
assert find_dangerous_changes(old_schema, new_schema) == [ |
| 1051 |
( |
| 1052 |
DangerousChangeType.TYPE_ADDED_TO_UNION, |
| 1053 |
"Type2 was added to union type UnionType1.", |
| 1054 |
) |
| 1055 |
] |
| 1056 |
|
| 1057 |
def should_detect_if_an_optional_field_was_added_to_an_input(): |
| 1058 |
old_schema = build_schema(""" |
| 1059 |
input InputType1 { |
| 1060 |
field1: String |
| 1061 |
} |
| 1062 |
""") |
| 1063 |
|
| 1064 |
new_schema = build_schema(""" |
| 1065 |
input InputType1 { |
| 1066 |
field1: String |
| 1067 |
field2: Int |
| 1068 |
} |
| 1069 |
""") |
| 1070 |
|
| 1071 |
assert find_dangerous_changes(old_schema, new_schema) == [ |
| 1072 |
( |
| 1073 |
DangerousChangeType.OPTIONAL_INPUT_FIELD_ADDED, |
| 1074 |
"An optional field field2 on input type InputType1 was added.", |
| 1075 |
) |
| 1076 |
] |
| 1077 |
|
| 1078 |
def should_find_all_dangerous_changes(): |
| 1079 |
old_schema = build_schema(""" |
| 1080 |
enum EnumType1 { |
| 1081 |
VALUE0 |
| 1082 |
VALUE1 |
| 1083 |
} |
| 1084 |
|
| 1085 |
type Type1 { |
| 1086 |
field1(argThatChangesDefaultValue: String = "test"): String |
| 1087 |
} |
| 1088 |
|
| 1089 |
interface Interface1 |
| 1090 |
type TypeThatGainsInterface1 |
| 1091 |
|
| 1092 |
type TypeInUnion1 |
| 1093 |
union UnionTypeThatGainsAType = TypeInUnion1 |
| 1094 |
""") |
| 1095 |
|
| 1096 |
new_schema = build_schema(""" |
| 1097 |
enum EnumType1 { |
| 1098 |
VALUE0 |
| 1099 |
VALUE1 |
| 1100 |
VALUE2 |
| 1101 |
} |
| 1102 |
|
| 1103 |
type Type1 { |
| 1104 |
field1(argThatChangesDefaultValue: String = "Test"): String |
| 1105 |
} |
| 1106 |
|
| 1107 |
interface Interface1 |
| 1108 |
type TypeThatGainsInterface1 implements Interface1 |
| 1109 |
|
| 1110 |
type TypeInUnion1 |
| 1111 |
type TypeInUnion2 |
| 1112 |
union UnionTypeThatGainsAType = TypeInUnion1 | TypeInUnion2 |
| 1113 |
""") |
| 1114 |
|
| 1115 |
assert find_dangerous_changes(old_schema, new_schema) == [ |
| 1116 |
( |
| 1117 |
DangerousChangeType.VALUE_ADDED_TO_ENUM, |
| 1118 |
"VALUE2 was added to enum type EnumType1.", |
| 1119 |
), |
| 1120 |
( |
| 1121 |
DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, |
| 1122 |
"Type1.field1 arg argThatChangesDefaultValue has changed defaultValue" |
| 1123 |
' from "test" to "Test".', |
| 1124 |
), |
| 1125 |
( |
| 1126 |
DangerousChangeType.IMPLEMENTED_INTERFACE_ADDED, |
| 1127 |
"Interface1 added to interfaces implemented" |
| 1128 |
" by TypeThatGainsInterface1.", |
| 1129 |
), |
| 1130 |
( |
| 1131 |
DangerousChangeType.TYPE_ADDED_TO_UNION, |
| 1132 |
"TypeInUnion2 was added to union type UnionTypeThatGainsAType.", |
| 1133 |
), |
| 1134 |
] |
| 1135 |
|
| 1136 |
def should_detect_if_an_optional_field_argument_was_added(): |
| 1137 |
old_schema = build_schema(""" |
| 1138 |
type Type1 { |
| 1139 |
field1(arg1: String): String |
| 1140 |
} |
| 1141 |
""") |
| 1142 |
|
| 1143 |
new_schema = build_schema(""" |
| 1144 |
type Type1 { |
| 1145 |
field1(arg1: String, arg2: String): String |
| 1146 |
} |
| 1147 |
""") |
| 1148 |
|
| 1149 |
assert find_dangerous_changes(old_schema, new_schema) == [ |
| 1150 |
( |
| 1151 |
DangerousChangeType.OPTIONAL_ARG_ADDED, |
| 1152 |
"An optional arg arg2 on Type1.field1 was added.", |
| 1153 |
) |
| 1154 |
] |