def test_schema_query_fields_type_has_changes(): old_schema = schema(""" schema { query: Query } type Query { field: String! } """) new_schema = schema(""" schema { query: Query } type Query { field: Int! } """) diff = Schema(old_schema, new_schema).diff() assert diff and len(diff) == 1 assert diff[ 0].message == "`Query.field` type changed from `String!` to `Int!`" assert diff[0].criticality == Criticality.breaking( 'Changing a field type will break queries that assume its type')
def test_object_type_added_field(): a = schema(""" type MyType{ a: Int } """) b = schema(""" type MyType{ a: Int b: String! } """) diff = Schema(a, b).diff() assert diff and len(diff) == 1 assert diff[0].message == "Field `b` was added to object type `MyType`" assert diff[0].path == 'MyType.b' assert diff[0].criticality == Criticality.safe() diff = Schema(b, a).diff() assert diff and len(diff) == 1 assert diff[0].message == "Field `b` was removed from object type `MyType`" assert diff[0].path == 'MyType.b' assert diff[0].criticality == Criticality.breaking( 'Removing a field is a breaking change. It is preferred to deprecate the field before removing it.' )
def test_directive_argument_changes(): name_arg = schema(""" directive @somedir(name: String) on FIELD_DEFINITION type A { a: String } """) id_arg = schema(""" directive @somedir(id: ID) on FIELD_DEFINITION type A { a: String } """) diff = Schema(name_arg, id_arg).diff() assert diff and len(diff) == 2 expected_message = ( 'Removed argument `name: String` from `@somedir` directive' "Added argument `id: ID` to `@somedir` directive" ) for change in diff: assert change.message in expected_message assert change.path == '@somedir' assert change.criticality == Criticality.breaking( 'Removing a directive argument will break existing usages of the argument' ) if 'Removed' in change.message else Criticality.safe()
def test_enum_value_removed(): a = schema(""" type Query { a: String } enum Letters { A B } """) b = schema(""" type Query { a: String } enum Letters { A } """) diff = Schema(a, b).diff() assert len(diff) == 1 change = diff[0] assert change.message == "Enum value `B` was removed from `Letters` enum" assert change.path == 'Letters' assert change.criticality == Criticality.breaking( 'Removing an enum value will break existing queries that use this enum value' )
def test_input_field_added_field(): a = schema(""" input Recipe { ingredients: [String] } """) b = schema(""" input Recipe { ingredients: [String] love: Float } """) diff = Schema(a, b).diff() assert diff and len(diff) == 1 assert diff[0].message == ( "Input Field `love: Float` was added to input type `Recipe`") assert diff[0].path == 'Recipe.love' assert diff[0].criticality == Criticality.safe() diff = Schema(b, a).diff() assert diff and len(diff) == 1 assert diff[0].message == ( "Input Field `love` removed from input type `Recipe`") assert diff[0].path == 'Recipe.love' assert diff[0].criticality == Criticality.breaking( 'Removing an input field will break queries that use this input field.' )
def test_added_removed_directive(): no_directive = schema(""" type A { a: String } """) one_directive = schema(""" directive @somedir on FIELD_DEFINITION type A { a: String } """) diff = Schema(no_directive, one_directive).diff() assert diff and len(diff) == 1 assert diff[0].message == "Directive `@somedir` was added to use on `FIELD_DEFINITION`" assert diff[0].path == '@somedir' assert diff[0].criticality == Criticality.safe() diff = Schema(one_directive, no_directive).diff() assert diff and len(diff) == 1 assert diff[0].message == "Directive `@somedir` was removed" assert diff[0].path == '@somedir' assert diff[0].criticality == Criticality.breaking('Removing a directive may break clients that depend on them.') two_locations = schema(""" directive @somedir on FIELD_DEFINITION | QUERY type A { a: String } """) diff = Schema(no_directive, two_locations).diff() assert diff and len(diff) == 1 assert diff[0].message == "Directive `@somedir` was added to use on `FIELD_DEFINITION | QUERY`" assert diff[0].path == '@somedir' assert diff[0].criticality == Criticality.safe()
def test_directive_location_added_and_removed(): one_location = schema(""" directive @somedir on FIELD_DEFINITION type A { a: String } """) two_locations = schema(""" directive @somedir on FIELD_DEFINITION | FIELD type A { a: String } """) diff = Schema(one_location, two_locations).diff() assert diff and len(diff) == 1 expected_message = ( "Directive locations of `@somedir` changed from `FIELD_DEFINITION` to `FIELD_DEFINITION | FIELD`" ) assert diff[0].message == expected_message assert diff[0].path == '@somedir' assert diff[0].criticality == Criticality.safe() diff = Schema(two_locations, one_location).diff() assert diff and len(diff) == 1 expected_message = ( "Directive locations of `@somedir` changed from `FIELD_DEFINITION | FIELD` to `FIELD_DEFINITION`" ) assert diff[0].message == expected_message assert diff[0].path == '@somedir' assert diff[0].criticality == Criticality.breaking( 'Removing a directive location will break any instance of its usage. Be sure no one uses it before removing it' )
def test_schema_removed_type(): old_schema = schema(""" schema { query: Query } type Query { field: String! } type ToBeRemovedType { added: Int } """) new_schema = schema(""" schema { query: Query } type Query { field: String! } """) diff = Schema(old_schema, new_schema).diff() assert diff and len(diff) == 1 # Type Int was also removed but it is ignored because it's a primitive. assert diff[0].message == "Type `ToBeRemovedType` was removed" assert diff[0].criticality == Criticality.breaking( 'Removing a type is a breaking change. It is preferred to ' 'deprecate and remove all references to this type first.')
def __init__(self, directive, old_locations, new_locations): self.directive = directive self.old_locations = old_locations self.new_locations = new_locations self.criticality = (Criticality.safe() if self._only_additions( ) else Criticality.breaking( "Removing a directive location will break any instance of its usage. " "Be sure no one uses it before removing it"))
def __init__(self, directive, arg_name, arg_type): self.criticality = Criticality.safe( ) if not is_non_null_type(arg_type.type) else Criticality.breaking( "Adding a non nullable directive argument will break existing usages of the directive" ) self.directive = directive self.arg_name = arg_name self.arg_type = arg_type
def __init__(self, type_, field_name, old_field, new_field): self.criticality = Criticality.safe()\ if is_safe_type_change(old_field.type, new_field.type)\ else Criticality.breaking('Changing a field type will break queries that assume its type') self.type_ = type_ self.field_name = field_name self.old_field = old_field self.new_field = new_field
def __init__(self, parent_type, field, arg_name, old_arg, new_arg): super().__init__(parent_type, field, arg_name, old_arg, new_arg) self.criticality = (Criticality.safe( ) if is_safe_change_for_input_value( old_arg.type, new_arg.type ) else Criticality.breaking( "Changing the type of a field's argument can break existing queries that use this argument." ))
class BreakingChange(Change): criticality = Criticality.breaking('this is breaking') def message(self): return 'test message' def path(self): return 'test path'
class InterfaceFieldDeprecationReasonChanged(AbstractInterfanceChange): criticality = Criticality.breaking('Breaking change') # TODO: Improve this logic to check if it was deprecated before @property def message(self): return ( f"`{self.interface.name}.{self.field_name}` deprecation reason changed " f"from `{self.old_field.deprecation_reason}` to `{self.new_field.deprecation_reason}`" )
def test_add_type_to_union(): two_types = schema(""" type Query { c: Int } type Result { message: String } type Error { message: String details: String } type Unknown { message: String details: String traceback: String } union Outcome = Result | Error """) three_types = schema(""" type Query { c: Int } type Result { message: String } type Error { message: String details: String } type Unknown { message: String details: String traceback: String } union Outcome = Result | Error | Unknown """) diff = Schema(two_types, three_types).diff() assert diff and len(diff) == 1 assert diff[ 0].message == "Union member `Unknown` was added to `Outcome` Union type" assert diff[0].path == 'Outcome' assert diff[0].criticality == Criticality.dangerous( 'Adding a possible type to Unions may break existing clients ' 'that were not programming defensively against a new possible type.') diff = Schema(three_types, two_types).diff() assert diff and len(diff) == 1 assert diff[ 0].message == "Union member `Unknown` was removed from `Outcome` Union type" assert diff[0].path == 'Outcome' assert diff[0].criticality == Criticality.breaking( 'Removing a union member from a union can break queries that use this union member in a fragment spread' )
def __init__(self, parent, field_name, argument_name, arg_type): self.criticality = Criticality.safe('Adding an optional argument is a safe change')\ if not is_non_null_type(arg_type.type)\ else Criticality.breaking("Adding a required argument to an existing field is a breaking " "change because it will break existing uses of this field") self.parent = parent self.field_name = field_name self.argument_name = argument_name self.arg_type = arg_type
def __init__(self, input_object, field_name, field): self.criticality = ( Criticality.safe() if is_non_null_type(field.type) is False else Criticality.breaking(self.BREAKING_MSG) ) self.input_object = input_object self.field_name = field_name self.field = field
class MyChange(Change): criticality = Criticality.breaking('If lorem is ipsum the world might end') @property def message(self): return 'Lorem should not be ipsum' @property def path(self): return 'Lorem.Ipsum'
class RemovedDirective(DirectiveChange): criticality = Criticality.breaking( "Removing a directive may break clients that depend on them.") def __init__(self, directive): self.directive = directive @property def message(self): return f"Directive `{self.directive}` was removed"
class SchemaChange(Change): criticality = Criticality.breaking( 'Changing a root type is a breaking change') def __init__(self, old_type, new_type): self.old_type = old_type self.new_type = new_type @property def path(self): return self.new_type
def __init__(self, directive, arg_name, old_type, new_type): self.criticality = (Criticality.breaking( "Changing the argument type is a breaking change" ) if not is_safe_change_for_input_value( old_type, new_type ) else Criticality.safe( "Changing an input field from non-null to null is considered non-breaking" )) self.directive = directive self.arg_name = arg_name self.old_type = old_type self.new_type = new_type
def __init__(self, parent, field_name, field): self.parent = parent self.field_name = field_name self.field = field self.criticality = ( Criticality.dangerous( "Removing deprecated fields without sufficient time for clients " "to update their queries may break their code" ) if field.deprecation_reason else Criticality.breaking( "Removing a field is a breaking change. It is preferred to deprecate the field before removing it." ) )
def __init__(self, input_, name, new_field, old_field): self.criticality = ( Criticality.safe() if is_safe_change_for_input_value(old_field.type, new_field.type) else Criticality.breaking( "Changing the type of an input field can break existing queries that use this field" ) ) self.input_ = input_ self.name = name self.new_field = new_field self.old_field = old_field
def test_input_field_type_nullability_change_on_lists_of_the_same_underlying_types( ): a = schema(""" input Params { arg: [ID!]! } """) b = schema(""" input Params { arg: [ID!] } """) c = schema(""" input Params { arg: [ID] } """) d = schema(""" input Params { arg: ID } """) diff = Schema(a, b).diff() assert diff and len(diff) == 1 assert diff[ 0].message == "`Params.arg` type changed from `[ID!]!` to `[ID!]`" assert diff[0].path == 'Params.arg' assert diff[0].criticality == Criticality.safe( ) # Because dropping the non-null constraint will not break anything diff = Schema(a, c).diff() assert diff[ 0].message == "`Params.arg` type changed from `[ID!]!` to `[ID]`" assert diff[0].path == 'Params.arg' assert diff[0].criticality == Criticality.breaking(ERROR) diff = Schema(a, d).diff() assert diff[0].message == "`Params.arg` type changed from `[ID!]!` to `ID`" assert diff[0].path == 'Params.arg' assert diff[0].criticality == Criticality.breaking(ERROR)
class DirectiveArgumentRemoved(DirectiveChange): criticality = Criticality.breaking( "Removing a directive argument will break existing usages of the argument" ) def __init__(self, directive, arg_name, arg_type): self.directive = directive self.arg_name = arg_name self.arg_type = arg_type @property def message(self): return f"Removed argument `{self.arg_name}: {self.arg_type.type}` from `{self.directive!s}` directive"
def test_input_field_now_is_not_nullable(): a = schema(""" input Params { arg: ID } """) b = schema(""" input Params { arg: ID! } """) diff = Schema(a, b).diff() assert diff and len(diff) == 1 assert diff[0].message == "`Params.arg` type changed from `ID` to `ID!`" assert diff[0].path == 'Params.arg' assert diff[0].criticality == Criticality.breaking(ERROR)
class InputFieldRemoved(Change): criticality = Criticality.breaking( 'Removing an input field will break queries that use this input field.' ) def __init__(self, input_object, value): self.input_object = input_object self.value = value @property def message(self): return f"Input Field `{self.value}` removed from input type `{self.input_object}`" @property def path(self): return f"{self.input_object}.{self.value}"
class UnionMemberRemoved(Change): criticality = Criticality.breaking( 'Removing a union member from a union can break ' 'queries that use this union member in a fragment spread') def __init__(self, union, value): self.union = union self.value = value @property def message(self): return f"Union member `{self.value}` was removed from `{self.union.name}` Union type" @property def path(self): return f"{self.union.name}"
def test_field_type_changed(): a_schema = schema(""" type Query { a: String! } """) changed_schema = schema(""" type Query { a: Int } """) diff = Schema(a_schema, changed_schema).diff() assert len(diff) == 1 assert diff[0].message == "`Query.a` type changed from `String!` to `Int`" assert diff[0].path == 'Query.a' assert diff[0].criticality == Criticality.breaking( 'Changing a field type will break queries that assume its type')
def test_input_field_inner_type_changed(): a = schema(""" input Params { arg: [Int] } """) b = schema(""" input Params { arg: [String] } """) diff = Schema(a, b).diff() assert diff and len(diff) == 1 assert diff[ 0].message == "`Params.arg` type changed from `[Int]` to `[String]`" assert diff[0].path == 'Params.arg' assert diff[0].criticality == Criticality.breaking(ERROR)