Beispiel #1
0
def diff(old_schema: Union[SDL, GQLSchema],
         new_schema: Union[SDL, GQLSchema]) -> List[Change]:
    """Compare two graphql schemas highlighting dangerous and breaking changes.

    Returns:
        changes (List[Change]): List of differences between both schemas with details about each change
    """
    first = SchemaLoader.from_sdl(
        old_schema) if not is_schema(old_schema) else old_schema
    second = SchemaLoader.from_sdl(
        new_schema) if not is_schema(new_schema) else new_schema
    return Schema(first, second).diff()
def main(args) -> int:
    # Load schemas from file path args
    old_schema = SchemaLoader.from_sdl(args.old_schema.read())
    new_schema = SchemaLoader.from_sdl(args.new_schema.read())
    args.old_schema.close()
    args.new_schema.close()

    diff = Schema(old_schema, new_schema).diff()
    restricted = evaluate_rules(diff, args.validation_rules)
    if args.allow_list:
        allow_list = args.allow_list.read()
        args.allow_list.close()
        allowed_changes = read_allowed_changes(allow_list)
        diff = [change for change in diff if change.checksum() not in allowed_changes]
    if args.as_json:
        print_json(diff)
    else:
        print_diff(diff)

    return exit_code(diff, args.strict, restricted, args.tolerant)
def test_diff_from_schema():
    schema_object = SchemaLoader.from_sdl("""
    schema {
        query: Query
    }
    type Query {
        a: ID!
        b: Int
    }
    """)
    assert is_schema(schema_object)
    changes = diff(schema_object, schema_object)
    assert changes == []
def test_load_from_string():
    schema_string = """
    schema {
        query: Query
    }
    
    type Query {
        a: ID!
        b: MyType
    }
    
    type MyType {
        c: String
        d: Float
    }
    """
    schema = SchemaLoader.from_sdl(schema_string)
    assert is_schema(schema)
    assert len(schema.query_type.fields) == 2
def test_load_empty_schema(schema):
    with pytest.raises(GraphQLSyntaxError):
        SchemaLoader.from_sdl(schema)