| 1 |
from enum import Enum |
| 2 |
from typing import Any, Collection, Dict, List, NamedTuple, Union, cast |
| 3 |
|
| 4 |
from ..language import print_ast |
| 5 |
from ..pyutils import inspect, Undefined |
| 6 |
from ..type import ( |
| 7 |
GraphQLEnumType, |
| 8 |
GraphQLField, |
| 9 |
GraphQLList, |
| 10 |
GraphQLNamedType, |
| 11 |
GraphQLNonNull, |
| 12 |
GraphQLInputType, |
| 13 |
GraphQLInterfaceType, |
| 14 |
GraphQLObjectType, |
| 15 |
GraphQLSchema, |
| 16 |
GraphQLType, |
| 17 |
GraphQLUnionType, |
| 18 |
is_enum_type, |
| 19 |
is_input_object_type, |
| 20 |
is_interface_type, |
| 21 |
is_list_type, |
| 22 |
is_named_type, |
| 23 |
is_non_null_type, |
| 24 |
is_object_type, |
| 25 |
is_required_argument, |
| 26 |
is_required_input_field, |
| 27 |
is_scalar_type, |
| 28 |
is_specified_scalar_type, |
| 29 |
is_union_type, |
| 30 |
) |
| 31 |
from ..utilities.sort_value_node import sort_value_node |
| 32 |
from .ast_from_value import ast_from_value |
| 33 |
|
| 34 |
__all__ = [ |
| 35 |
"BreakingChange", |
| 36 |
"BreakingChangeType", |
| 37 |
"DangerousChange", |
| 38 |
"DangerousChangeType", |
| 39 |
"find_breaking_changes", |
| 40 |
"find_dangerous_changes", |
| 41 |
] |
| 42 |
|
| 43 |
|
| 44 |
class BreakingChangeType(Enum): |
| 45 |
TYPE_REMOVED = 10 |
| 46 |
TYPE_CHANGED_KIND = 11 |
| 47 |
TYPE_REMOVED_FROM_UNION = 20 |
| 48 |
VALUE_REMOVED_FROM_ENUM = 21 |
| 49 |
REQUIRED_INPUT_FIELD_ADDED = 22 |
| 50 |
IMPLEMENTED_INTERFACE_REMOVED = 23 |
| 51 |
FIELD_REMOVED = 30 |
| 52 |
FIELD_CHANGED_KIND = 31 |
| 53 |
REQUIRED_ARG_ADDED = 40 |
| 54 |
ARG_REMOVED = 41 |
| 55 |
ARG_CHANGED_KIND = 42 |
| 56 |
DIRECTIVE_REMOVED = 50 |
| 57 |
DIRECTIVE_ARG_REMOVED = 51 |
| 58 |
REQUIRED_DIRECTIVE_ARG_ADDED = 52 |
| 59 |
DIRECTIVE_REPEATABLE_REMOVED = 53 |
| 60 |
DIRECTIVE_LOCATION_REMOVED = 54 |
| 61 |
|
| 62 |
|
| 63 |
class DangerousChangeType(Enum): |
| 64 |
VALUE_ADDED_TO_ENUM = 60 |
| 65 |
TYPE_ADDED_TO_UNION = 61 |
| 66 |
OPTIONAL_INPUT_FIELD_ADDED = 62 |
| 67 |
OPTIONAL_ARG_ADDED = 63 |
| 68 |
IMPLEMENTED_INTERFACE_ADDED = 64 |
| 69 |
ARG_DEFAULT_VALUE_CHANGE = 65 |
| 70 |
|
| 71 |
|
| 72 |
class BreakingChange(NamedTuple): |
| 73 |
type: BreakingChangeType |
| 74 |
description: str |
| 75 |
|
| 76 |
|
| 77 |
class DangerousChange(NamedTuple): |
| 78 |
type: DangerousChangeType |
| 79 |
description: str |
| 80 |
|
| 81 |
|
| 82 |
Change = Union[BreakingChange, DangerousChange] |
| 83 |
|
| 84 |
|
| 85 |
def find_breaking_changes( |
| 86 |
old_schema: GraphQLSchema, new_schema: GraphQLSchema |
| 87 |
) -> List[BreakingChange]: |
| 88 |
"""Find breaking changes. |
| 89 |
|
| 90 |
Given two schemas, returns a list containing descriptions of all the types of |
| 91 |
breaking changes covered by the other functions down below. |
| 92 |
""" |
| 93 |
return [ |
| 94 |
change |
| 95 |
for change in find_schema_changes(old_schema, new_schema) |
| 96 |
if isinstance(change.type, BreakingChangeType) |
| 97 |
] |
| 98 |
|
| 99 |
|
| 100 |
def find_dangerous_changes( |
| 101 |
old_schema: GraphQLSchema, new_schema: GraphQLSchema |
| 102 |
) -> List[DangerousChange]: |
| 103 |
"""Find dangerous changes. |
| 104 |
|
| 105 |
Given two schemas, returns a list containing descriptions of all the types of |
| 106 |
potentially dangerous changes covered by the other functions down below. |
| 107 |
""" |
| 108 |
return [ |
| 109 |
change |
| 110 |
for change in find_schema_changes(old_schema, new_schema) |
| 111 |
if isinstance(change.type, DangerousChangeType) |
| 112 |
] |
| 113 |
|
| 114 |
|
| 115 |
def find_schema_changes( |
| 116 |
old_schema: GraphQLSchema, new_schema: GraphQLSchema |
| 117 |
) -> List[Change]: |
| 118 |
return find_type_changes(old_schema, new_schema) + find_directive_changes( |
| 119 |
old_schema, new_schema |
| 120 |
) |
| 121 |
|
| 122 |
|
| 123 |
def find_directive_changes( |
| 124 |
old_schema: GraphQLSchema, new_schema: GraphQLSchema |
| 125 |
) -> List[Change]: |
| 126 |
schema_changes: List[Change] = [] |
| 127 |
|
| 128 |
directives_diff = list_diff(old_schema.directives, new_schema.directives) |
| 129 |
|
| 130 |
for directive in directives_diff.removed: |
| 131 |
schema_changes.append( |
| 132 |
BreakingChange( |
| 133 |
BreakingChangeType.DIRECTIVE_REMOVED, f"{directive.name} was removed." |
| 134 |
) |
| 135 |
) |
| 136 |
|
| 137 |
for old_directive, new_directive in directives_diff.persisted: |
| 138 |
args_diff = dict_diff(old_directive.args, new_directive.args) |
| 139 |
|
| 140 |
for arg_name, new_arg in args_diff.added.items(): |
| 141 |
if is_required_argument(new_arg): |
| 142 |
schema_changes.append( |
| 143 |
BreakingChange( |
| 144 |
BreakingChangeType.REQUIRED_DIRECTIVE_ARG_ADDED, |
| 145 |
f"A required arg {arg_name} on directive" |
| 146 |
f" {old_directive.name} was added.", |
| 147 |
) |
| 148 |
) |
| 149 |
|
| 150 |
for arg_name in args_diff.removed: |
| 151 |
schema_changes.append( |
| 152 |
BreakingChange( |
| 153 |
BreakingChangeType.DIRECTIVE_ARG_REMOVED, |
| 154 |
f"{arg_name} was removed from {new_directive.name}.", |
| 155 |
) |
| 156 |
) |
| 157 |
|
| 158 |
if old_directive.is_repeatable and not new_directive.is_repeatable: |
| 159 |
schema_changes.append( |
| 160 |
BreakingChange( |
| 161 |
BreakingChangeType.DIRECTIVE_REPEATABLE_REMOVED, |
| 162 |
f"Repeatable flag was removed from {old_directive.name}.", |
| 163 |
) |
| 164 |
) |
| 165 |
|
| 166 |
for location in old_directive.locations: |
| 167 |
if location not in new_directive.locations: |
| 168 |
schema_changes.append( |
| 169 |
BreakingChange( |
| 170 |
BreakingChangeType.DIRECTIVE_LOCATION_REMOVED, |
| 171 |
f"{location.name} was removed from {new_directive.name}.", |
| 172 |
) |
| 173 |
) |
| 174 |
|
| 175 |
return schema_changes |
| 176 |
|
| 177 |
|
| 178 |
def find_type_changes( |
| 179 |
old_schema: GraphQLSchema, new_schema: GraphQLSchema |
| 180 |
) -> List[Change]: |
| 181 |
schema_changes: List[Change] = [] |
| 182 |
types_diff = dict_diff(old_schema.type_map, new_schema.type_map) |
| 183 |
|
| 184 |
for type_name, old_type in types_diff.removed.items(): |
| 185 |
schema_changes.append( |
| 186 |
BreakingChange( |
| 187 |
BreakingChangeType.TYPE_REMOVED, |
| 188 |
( |
| 189 |
f"Standard scalar {type_name} was removed" |
| 190 |
" because it is not referenced anymore." |
| 191 |
if is_specified_scalar_type(old_type) |
| 192 |
else f"{type_name} was removed." |
| 193 |
), |
| 194 |
) |
| 195 |
) |
| 196 |
|
| 197 |
for type_name, (old_type, new_type) in types_diff.persisted.items(): |
| 198 |
if is_enum_type(old_type) and is_enum_type(new_type): |
| 199 |
schema_changes.extend(find_enum_type_changes(old_type, new_type)) |
| 200 |
elif is_union_type(old_type) and is_union_type(new_type): |
| 201 |
schema_changes.extend(find_union_type_changes(old_type, new_type)) |
| 202 |
elif is_input_object_type(old_type) and is_input_object_type(new_type): |
| 203 |
schema_changes.extend(find_input_object_type_changes(old_type, new_type)) |
| 204 |
elif is_object_type(old_type) and is_object_type(new_type): |
| 205 |
schema_changes.extend(find_field_changes(old_type, new_type)) |
| 206 |
schema_changes.extend( |
| 207 |
find_implemented_interfaces_changes(old_type, new_type) |
| 208 |
) |
| 209 |
elif is_interface_type(old_type) and is_interface_type(new_type): |
| 210 |
schema_changes.extend(find_field_changes(old_type, new_type)) |
| 211 |
schema_changes.extend( |
| 212 |
find_implemented_interfaces_changes(old_type, new_type) |
| 213 |
) |
| 214 |
elif old_type.__class__ is not new_type.__class__: |
| 215 |
schema_changes.append( |
| 216 |
BreakingChange( |
| 217 |
BreakingChangeType.TYPE_CHANGED_KIND, |
| 218 |
f"{type_name} changed from {type_kind_name(old_type)}" |
| 219 |
f" to {type_kind_name(new_type)}.", |
| 220 |
) |
| 221 |
) |
| 222 |
|
| 223 |
return schema_changes |
| 224 |
|
| 225 |
|
| 226 |
def find_input_object_type_changes( |
| 227 |
old_type: Union[GraphQLObjectType, GraphQLInterfaceType], |
| 228 |
new_type: Union[GraphQLObjectType, GraphQLInterfaceType], |
| 229 |
) -> List[Change]: |
| 230 |
schema_changes: List[Change] = [] |
| 231 |
fields_diff = dict_diff(old_type.fields, new_type.fields) |
| 232 |
|
| 233 |
for field_name, new_field in fields_diff.added.items(): |
| 234 |
if is_required_input_field(new_field): |
| 235 |
schema_changes.append( |
| 236 |
BreakingChange( |
| 237 |
BreakingChangeType.REQUIRED_INPUT_FIELD_ADDED, |
| 238 |
f"A required field {field_name} on" |
| 239 |
f" input type {old_type.name} was added.", |
| 240 |
) |
| 241 |
) |
| 242 |
else: |
| 243 |
schema_changes.append( |
| 244 |
DangerousChange( |
| 245 |
DangerousChangeType.OPTIONAL_INPUT_FIELD_ADDED, |
| 246 |
f"An optional field {field_name} on" |
| 247 |
f" input type {old_type.name} was added.", |
| 248 |
) |
| 249 |
) |
| 250 |
|
| 251 |
for field_name in fields_diff.removed: |
| 252 |
schema_changes.append( |
| 253 |
BreakingChange( |
| 254 |
BreakingChangeType.FIELD_REMOVED, |
| 255 |
f"{old_type.name}.{field_name} was removed.", |
| 256 |
) |
| 257 |
) |
| 258 |
|
| 259 |
for field_name, (old_field, new_field) in fields_diff.persisted.items(): |
| 260 |
is_safe = is_change_safe_for_input_object_field_or_field_arg( |
| 261 |
old_field.type, new_field.type |
| 262 |
) |
| 263 |
if not is_safe: |
| 264 |
schema_changes.append( |
| 265 |
BreakingChange( |
| 266 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 267 |
f"{old_type.name}.{field_name} changed type" |
| 268 |
f" from {old_field.type} to {new_field.type}.", |
| 269 |
) |
| 270 |
) |
| 271 |
|
| 272 |
return schema_changes |
| 273 |
|
| 274 |
|
| 275 |
def find_union_type_changes( |
| 276 |
old_type: GraphQLUnionType, new_type: GraphQLUnionType |
| 277 |
) -> List[Change]: |
| 278 |
schema_changes: List[Change] = [] |
| 279 |
possible_types_diff = list_diff(old_type.types, new_type.types) |
| 280 |
|
| 281 |
for possible_type in possible_types_diff.added: |
| 282 |
schema_changes.append( |
| 283 |
DangerousChange( |
| 284 |
DangerousChangeType.TYPE_ADDED_TO_UNION, |
| 285 |
f"{possible_type.name} was added" f" to union type {old_type.name}.", |
| 286 |
) |
| 287 |
) |
| 288 |
|
| 289 |
for possible_type in possible_types_diff.removed: |
| 290 |
schema_changes.append( |
| 291 |
BreakingChange( |
| 292 |
BreakingChangeType.TYPE_REMOVED_FROM_UNION, |
| 293 |
f"{possible_type.name} was removed from union type {old_type.name}.", |
| 294 |
) |
| 295 |
) |
| 296 |
|
| 297 |
return schema_changes |
| 298 |
|
| 299 |
|
| 300 |
def find_enum_type_changes( |
| 301 |
old_type: GraphQLEnumType, new_type: GraphQLEnumType |
| 302 |
) -> List[Change]: |
| 303 |
schema_changes: List[Change] = [] |
| 304 |
values_diff = dict_diff(old_type.values, new_type.values) |
| 305 |
|
| 306 |
for value_name in values_diff.added: |
| 307 |
schema_changes.append( |
| 308 |
DangerousChange( |
| 309 |
DangerousChangeType.VALUE_ADDED_TO_ENUM, |
| 310 |
f"{value_name} was added to enum type {old_type.name}.", |
| 311 |
) |
| 312 |
) |
| 313 |
|
| 314 |
for value_name in values_diff.removed: |
| 315 |
schema_changes.append( |
| 316 |
BreakingChange( |
| 317 |
BreakingChangeType.VALUE_REMOVED_FROM_ENUM, |
| 318 |
f"{value_name} was removed from enum type {old_type.name}.", |
| 319 |
) |
| 320 |
) |
| 321 |
|
| 322 |
return schema_changes |
| 323 |
|
| 324 |
|
| 325 |
def find_implemented_interfaces_changes( |
| 326 |
old_type: Union[GraphQLObjectType, GraphQLInterfaceType], |
| 327 |
new_type: Union[GraphQLObjectType, GraphQLInterfaceType], |
| 328 |
) -> List[Change]: |
| 329 |
schema_changes: List[Change] = [] |
| 330 |
interfaces_diff = list_diff(old_type.interfaces, new_type.interfaces) |
| 331 |
|
| 332 |
for interface in interfaces_diff.added: |
| 333 |
schema_changes.append( |
| 334 |
DangerousChange( |
| 335 |
DangerousChangeType.IMPLEMENTED_INTERFACE_ADDED, |
| 336 |
f"{interface.name} added to interfaces implemented by {old_type.name}.", |
| 337 |
) |
| 338 |
) |
| 339 |
|
| 340 |
for interface in interfaces_diff.removed: |
| 341 |
schema_changes.append( |
| 342 |
BreakingChange( |
| 343 |
BreakingChangeType.IMPLEMENTED_INTERFACE_REMOVED, |
| 344 |
f"{old_type.name} no longer implements interface {interface.name}.", |
| 345 |
) |
| 346 |
) |
| 347 |
|
| 348 |
return schema_changes |
| 349 |
|
| 350 |
|
| 351 |
def find_field_changes( |
| 352 |
old_type: Union[GraphQLObjectType, GraphQLInterfaceType], |
| 353 |
new_type: Union[GraphQLObjectType, GraphQLInterfaceType], |
| 354 |
) -> List[Change]: |
| 355 |
schema_changes: List[Change] = [] |
| 356 |
fields_diff = dict_diff(old_type.fields, new_type.fields) |
| 357 |
|
| 358 |
for field_name in fields_diff.removed: |
| 359 |
schema_changes.append( |
| 360 |
BreakingChange( |
| 361 |
BreakingChangeType.FIELD_REMOVED, |
| 362 |
f"{old_type.name}.{field_name} was removed.", |
| 363 |
) |
| 364 |
) |
| 365 |
|
| 366 |
for field_name, (old_field, new_field) in fields_diff.persisted.items(): |
| 367 |
schema_changes.extend( |
| 368 |
find_arg_changes(old_type, field_name, old_field, new_field) |
| 369 |
) |
| 370 |
is_safe = is_change_safe_for_object_or_interface_field( |
| 371 |
old_field.type, new_field.type |
| 372 |
) |
| 373 |
if not is_safe: |
| 374 |
schema_changes.append( |
| 375 |
BreakingChange( |
| 376 |
BreakingChangeType.FIELD_CHANGED_KIND, |
| 377 |
f"{old_type.name}.{field_name} changed type" |
| 378 |
f" from {old_field.type} to {new_field.type}.", |
| 379 |
) |
| 380 |
) |
| 381 |
|
| 382 |
return schema_changes |
| 383 |
|
| 384 |
|
| 385 |
def find_arg_changes( |
| 386 |
old_type: Union[GraphQLObjectType, GraphQLInterfaceType], |
| 387 |
field_name: str, |
| 388 |
old_field: GraphQLField, |
| 389 |
new_field: GraphQLField, |
| 390 |
) -> List[Change]: |
| 391 |
schema_changes: List[Change] = [] |
| 392 |
args_diff = dict_diff(old_field.args, new_field.args) |
| 393 |
|
| 394 |
for arg_name in args_diff.removed: |
| 395 |
schema_changes.append( |
| 396 |
BreakingChange( |
| 397 |
BreakingChangeType.ARG_REMOVED, |
| 398 |
f"{old_type.name}.{field_name} arg" f" {arg_name} was removed.", |
| 399 |
) |
| 400 |
) |
| 401 |
|
| 402 |
for arg_name, (old_arg, new_arg) in args_diff.persisted.items(): |
| 403 |
is_safe = is_change_safe_for_input_object_field_or_field_arg( |
| 404 |
old_arg.type, new_arg.type |
| 405 |
) |
| 406 |
if not is_safe: |
| 407 |
schema_changes.append( |
| 408 |
BreakingChange( |
| 409 |
BreakingChangeType.ARG_CHANGED_KIND, |
| 410 |
f"{old_type.name}.{field_name} arg" |
| 411 |
f" {arg_name} has changed type from" |
| 412 |
f" {old_arg.type} to {new_arg.type}.", |
| 413 |
) |
| 414 |
) |
| 415 |
elif old_arg.default_value is not Undefined: |
| 416 |
if new_arg.default_value is Undefined: |
| 417 |
schema_changes.append( |
| 418 |
DangerousChange( |
| 419 |
DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, |
| 420 |
f"{old_type.name}.{field_name} arg" |
| 421 |
f" {arg_name} defaultValue was removed.", |
| 422 |
) |
| 423 |
) |
| 424 |
else: |
| 425 |
# Since we are looking only for client's observable changes we should |
| 426 |
# compare default values in the same representation as they are |
| 427 |
# represented inside introspection. |
| 428 |
old_value_str = stringify_value(old_arg.default_value, old_arg.type) |
| 429 |
new_value_str = stringify_value(new_arg.default_value, new_arg.type) |
| 430 |
|
| 431 |
if old_value_str != new_value_str: |
| 432 |
schema_changes.append( |
| 433 |
DangerousChange( |
| 434 |
DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, |
| 435 |
f"{old_type.name}.{field_name} arg" |
| 436 |
f" {arg_name} has changed defaultValue" |
| 437 |
f" from {old_value_str} to {new_value_str}.", |
| 438 |
) |
| 439 |
) |
| 440 |
|
| 441 |
for arg_name, new_arg in args_diff.added.items(): |
| 442 |
if is_required_argument(new_arg): |
| 443 |
schema_changes.append( |
| 444 |
BreakingChange( |
| 445 |
BreakingChangeType.REQUIRED_ARG_ADDED, |
| 446 |
f"A required arg {arg_name} on" |
| 447 |
f" {old_type.name}.{field_name} was added.", |
| 448 |
) |
| 449 |
) |
| 450 |
else: |
| 451 |
schema_changes.append( |
| 452 |
DangerousChange( |
| 453 |
DangerousChangeType.OPTIONAL_ARG_ADDED, |
| 454 |
f"An optional arg {arg_name} on" |
| 455 |
f" {old_type.name}.{field_name} was added.", |
| 456 |
) |
| 457 |
) |
| 458 |
|
| 459 |
return schema_changes |
| 460 |
|
| 461 |
|
| 462 |
def is_change_safe_for_object_or_interface_field( |
| 463 |
old_type: GraphQLType, new_type: GraphQLType |
| 464 |
) -> bool: |
| 465 |
if is_list_type(old_type): |
| 466 |
return ( |
| 467 |
# if they're both lists, make sure underlying types are compatible |
| 468 |
is_list_type(new_type) |
| 469 |
and is_change_safe_for_object_or_interface_field( |
| 470 |
cast(GraphQLList, old_type).of_type, cast(GraphQLList, new_type).of_type |
| 471 |
) |
| 472 |
) or ( |
| 473 |
# moving from nullable to non-null of same underlying type is safe |
| 474 |
is_non_null_type(new_type) |
| 475 |
and is_change_safe_for_object_or_interface_field( |
| 476 |
old_type, cast(GraphQLNonNull, new_type).of_type |
| 477 |
) |
| 478 |
) |
| 479 |
|
| 480 |
if is_non_null_type(old_type): |
| 481 |
# if they're both non-null, make sure underlying types are compatible |
| 482 |
return is_non_null_type( |
| 483 |
new_type |
| 484 |
) and is_change_safe_for_object_or_interface_field( |
| 485 |
cast(GraphQLNonNull, old_type).of_type, |
| 486 |
cast(GraphQLNonNull, new_type).of_type, |
| 487 |
) |
| 488 |
|
| 489 |
return ( |
| 490 |
# if they're both named types, see if their names are equivalent |
| 491 |
is_named_type(new_type) |
| 492 |
and cast(GraphQLNamedType, old_type).name |
| 493 |
== cast(GraphQLNamedType, new_type).name |
| 494 |
) or ( |
| 495 |
# moving from nullable to non-null of same underlying type is safe |
| 496 |
is_non_null_type(new_type) |
| 497 |
and is_change_safe_for_object_or_interface_field( |
| 498 |
old_type, cast(GraphQLNonNull, new_type).of_type |
| 499 |
) |
| 500 |
) |
| 501 |
|
| 502 |
|
| 503 |
def is_change_safe_for_input_object_field_or_field_arg( |
| 504 |
old_type: GraphQLType, new_type: GraphQLType |
| 505 |
) -> bool: |
| 506 |
if is_list_type(old_type): |
| 507 |
|
| 508 |
return is_list_type( |
| 509 |
# if they're both lists, make sure underlying types are compatible |
| 510 |
new_type |
| 511 |
) and is_change_safe_for_input_object_field_or_field_arg( |
| 512 |
cast(GraphQLList, old_type).of_type, cast(GraphQLList, new_type).of_type |
| 513 |
) |
| 514 |
|
| 515 |
if is_non_null_type(old_type): |
| 516 |
return ( |
| 517 |
# if they're both non-null, make sure the underlying types are compatible |
| 518 |
is_non_null_type(new_type) |
| 519 |
and is_change_safe_for_input_object_field_or_field_arg( |
| 520 |
cast(GraphQLNonNull, old_type).of_type, |
| 521 |
cast(GraphQLNonNull, new_type).of_type, |
| 522 |
) |
| 523 |
) or ( |
| 524 |
# moving from non-null to nullable of same underlying type is safe |
| 525 |
not is_non_null_type(new_type) |
| 526 |
and is_change_safe_for_input_object_field_or_field_arg( |
| 527 |
cast(GraphQLNonNull, old_type).of_type, new_type |
| 528 |
) |
| 529 |
) |
| 530 |
|
| 531 |
return ( |
| 532 |
# if they're both named types, see if their names are equivalent |
| 533 |
is_named_type(new_type) |
| 534 |
and cast(GraphQLNamedType, old_type).name |
| 535 |
== cast(GraphQLNamedType, new_type).name |
| 536 |
) |
| 537 |
|
| 538 |
|
| 539 |
def type_kind_name(type_: GraphQLNamedType) -> str: |
| 540 |
if is_scalar_type(type_): |
| 541 |
return "a Scalar type" |
| 542 |
if is_object_type(type_): |
| 543 |
return "an Object type" |
| 544 |
if is_interface_type(type_): |
| 545 |
return "an Interface type" |
| 546 |
if is_union_type(type_): |
| 547 |
return "a Union type" |
| 548 |
if is_enum_type(type_): |
| 549 |
return "an Enum type" |
| 550 |
if is_input_object_type(type_): |
| 551 |
return "an Input type" |
| 552 |
|
| 553 |
# Not reachable. All possible output types have been considered. |
| 554 |
raise TypeError(f"Unexpected type {inspect(type)}") |
| 555 |
|
| 556 |
|
| 557 |
def stringify_value(value: Any, type_: GraphQLInputType) -> str: |
| 558 |
ast = ast_from_value(value, type_) |
| 559 |
if ast is None: # pragma: no cover |
| 560 |
raise TypeError(f"Invalid value: {inspect(value)}") |
| 561 |
return print_ast(sort_value_node(ast)) |
| 562 |
|
| 563 |
|
| 564 |
class ListDiff(NamedTuple): |
| 565 |
"""Tuple with added, removed and persisted list items.""" |
| 566 |
|
| 567 |
added: List |
| 568 |
removed: List |
| 569 |
persisted: List |
| 570 |
|
| 571 |
|
| 572 |
def list_diff(old_list: Collection, new_list: Collection) -> ListDiff: |
| 573 |
"""Get differences between two lists of named items.""" |
| 574 |
added = [] |
| 575 |
persisted = [] |
| 576 |
removed = [] |
| 577 |
|
| 578 |
old_set = {item.name for item in old_list} |
| 579 |
new_map = {item.name: item for item in new_list} |
| 580 |
|
| 581 |
for old_item in old_list: |
| 582 |
new_item = new_map.get(old_item.name) |
| 583 |
if new_item: |
| 584 |
persisted.append([old_item, new_item]) |
| 585 |
else: |
| 586 |
removed.append(old_item) |
| 587 |
|
| 588 |
for new_item in new_list: |
| 589 |
if new_item.name not in old_set: |
| 590 |
added.append(new_item) |
| 591 |
|
| 592 |
return ListDiff(added, removed, persisted) |
| 593 |
|
| 594 |
|
| 595 |
class DictDiff(NamedTuple): |
| 596 |
"""Tuple with added, removed and persisted dict entries.""" |
| 597 |
|
| 598 |
added: Dict |
| 599 |
removed: Dict |
| 600 |
persisted: Dict |
| 601 |
|
| 602 |
|
| 603 |
def dict_diff(old_dict: Dict, new_dict: Dict) -> DictDiff: |
| 604 |
"""Get differences between two dicts.""" |
| 605 |
added = {} |
| 606 |
removed = {} |
| 607 |
persisted = {} |
| 608 |
|
| 609 |
for old_name, old_item in old_dict.items(): |
| 610 |
new_item = new_dict.get(old_name) |
| 611 |
if new_item: |
| 612 |
persisted[old_name] = [old_item, new_item] |
| 613 |
else: |
| 614 |
removed[old_name] = old_item |
| 615 |
|
| 616 |
for new_name, new_item in new_dict.items(): |
| 617 |
if new_name not in old_dict: |
| 618 |
added[new_name] = new_item |
| 619 |
|
| 620 |
return DictDiff(added, removed, persisted) |