def test_union_type_extension_duplicate_type(): with pytest.raises(SDLError) as exc_info: build_schema(""" type Query { foo: Foo } type Bar { bar: Int } type Baz { baz: Int } union Foo = Bar extend union Foo = Bar """) assert exc_info.value.to_dict() == { "locations": [{ "column": 32, "line": 16 }], "message": ('Found duplicate member type "Bar" when extending UnionType "Foo"'), }
def test_unknown_type_in_interface_list(): with pytest.raises(SDLError) as exc_info: build_schema("type Query implements Bar { field: String }") assert exc_info.value.to_dict() == { "locations": [{"column": 23, "line": 1}], "message": "Type Bar not found in document", }
def test_union_type_extension_bad_extension(): with pytest.raises(SDLError) as exc_info: build_schema(""" type Query { foo: Foo } type Bar { bar: Int } type Baz { baz: Int } union Foo = Bar extend type Foo { one: Int } """) assert exc_info.value.to_dict() == { "locations": [{ "column": 13, "line": 16 }], "message": ("Expected UnionTypeExtension when extending UnionType " "but got ObjectTypeExtension"), }
def test_enum_extension_duplicate_value(): with pytest.raises(SDLError) as exc_info: build_schema(""" type Query { foo: Foo } enum Foo { BLUE GREEN RED } extend enum Foo { RED } """) assert exc_info.value.to_dict() == { "locations": [{ "column": 17, "line": 13 }], "message": 'Found duplicate enum value "RED" when extending EnumType "Foo"', }
def test_enum_extension_bad_extension(): with pytest.raises(SDLError) as exc_info: build_schema(""" type Query { foo: Foo } enum Foo { BLUE GREEN RED } extend type Foo { one: Int } """) assert exc_info.value.to_dict() == { "locations": [{ "column": 13, "line": 12 }], "message": "Expected EnumTypeExtension when extending EnumType but got " "ObjectTypeExtension", }
def test_no_incompatible_changes(old_schema, new_schema): assert [] == list( diff_schema( build_schema(old_schema), build_schema(new_schema), min_severity=SchemaChangeSeverity.DANGEROUS, ))
def test_schema_extension_duplicate_directive(): class OnSchema(SchemaDirective): definition = "onSchema" def on_schema(self, schema): pass with pytest.raises(SDLError) as exc_info: build_schema( """ directive @onSchema on SCHEMA type Foo { foo: String } type Bar { bar: String } schema @onSchema { query: Foo } extend schema @onSchema """, schema_directives=(OnSchema, ), ) assert exc_info.value.to_dict() == { "locations": [{ "column": 27, "line": 11 }], "message": 'Directive "@onSchema" already applied', }
def test_directive_on_wrong_location(): class UppercaseDirective(SchemaDirective): definition = "upper" def on_field(self, field_definition): return wrap_resolver(field_definition, lambda x: x.upper()) with pytest.raises(SDLError) as exc_info: build_schema( """ directive @upper on FIELD_DEFINITION type Query @upper { foo: String } """, schema_directives=(UppercaseDirective, ), ) assert exc_info.value.to_dict() == { "locations": [{ "column": 24, "line": 4 }], "message": 'Directive "@upper" not applicable to "OBJECT"', }
def test_unknown_type_in_union_list(): with pytest.raises(SDLError) as exc_info: build_schema( """ union TestUnion = Bar type Query { testUnion: TestUnion } """ ) assert exc_info.value.to_dict() == { "locations": [{"column": 31, "line": 2}], "message": "Type Bar not found in document", }
def test_schema_extension_directive(): build_schema(""" directive @onSchema on SCHEMA type Foo { foo: String } type Bar { bar: String } schema { query: Foo } extend schema @onSchema """)
def test_object_type_extension_duplicate_field(): with pytest.raises(SDLError) as exc_info: build_schema(""" type Query { foo: String } type Object { one: String } extend type Object { one: Int } """) assert exc_info.value.to_dict() == { "locations": [{ "column": 34, "line": 4 }], "message": 'Found duplicate field "one" when extending type "Object"', }
def test_input_object_type_extension_duplicate_field(): with pytest.raises(SDLError) as exc_info: build_schema(""" type Query { foo(in: Foo): String } input Foo { one: Int } extend input Foo { one: Int } """) assert exc_info.value.to_dict() == { "locations": [{ "column": 32, "line": 4 }], "message": 'Found duplicate field "one" when extending input object "Foo"', }
def test_interface_type_extension_duplicate_field(): with pytest.raises(SDLError) as exc_info: build_schema(""" type Query { foo: IFace } interface IFace { one: String } extend interface IFace { one: Int } """) assert exc_info.value.to_dict() == { "locations": [{ "column": 38, "line": 4 }], "message": 'Found duplicate field "one" when extending interface "IFace"', }
def test_object_type_extension_already_implemented_interface(): with pytest.raises(SDLError) as exc_info: build_schema(""" type Query { foo: String } interface IFace1 { one: String } type Object implements IFace1 { one: String } extend type Object implements IFace1 """) assert exc_info.value.to_dict() == { "locations": [{ "column": 43, "line": 5 }], "message": 'Interface "IFace1" already implemented for type "Object"', }
def test_object_type_extension_bad_extension(): with pytest.raises(SDLError) as exc_info: build_schema(""" type Query { foo: String } type Object { one: String } extend input Object { two: Int } """) assert exc_info.value.to_dict() == { "locations": [{ "column": 13, "line": 4 }], "message": ("Expected ObjectTypeExtension when extending ObjectType " "but got InputObjectTypeExtension"), }
def test_interface_type_extension_bad_extension(): with pytest.raises(SDLError) as exc_info: build_schema(""" type Query { foo: IFace } interface IFace { one: String } extend type IFace { one: Int } """) assert exc_info.value.to_dict() == { "locations": [{ "column": 13, "line": 4 }], "message": ("Expected InterfaceTypeExtension when extending InterfaceType " "but got ObjectTypeExtension"), }
def test_simple_field_modifier(): class UppercaseDirective(SchemaDirective): definition = "upper" def on_field(self, field_definition): return wrap_resolver(field_definition, lambda x: x.upper()) assert (graphql_blocking( build_schema( """ directive @upper on FIELD_DEFINITION type Query { foo: String @upper } """, schema_directives=(UppercaseDirective, ), ), "{ foo }", root={ "foo": "lowerCase" }, ).response() == { "data": { "foo": "LOWERCASE" } })
def test_field_modifier_using_arguments(): class PowerDirective(SchemaDirective): definition = "power" def __init__(self, args): self.exponent = args["exponent"] def on_field(self, field_definition): return wrap_resolver(field_definition, lambda x: x**self.exponent) assert (graphql_blocking( build_schema( """ directive @power(exponent: Int = 2) on FIELD_DEFINITION type Query { foo: Int @power bar: Int @power(exponent: 3) } """, schema_directives=(PowerDirective, ), ), "{ foo, bar }", root={ "foo": 2, "bar": 2 }, ).response() == { "data": { "foo": 4, "bar": 8 } })
def test_minimum_severity(): changes = list( diff_schema( build_schema(""" type Type1 { field1: String } type Query { field1: String } """), build_schema(""" type Type2 { field1: String } type Query { field1: String } """), min_severity=SchemaChangeSeverity.BREAKING, )) assert len(changes) == 1 assert_change_found(changes, (TypeRemoved, "Type Type1 was removed."))
def test_enum_extension(): assert (build_schema(""" type Query { foo: Foo } enum Foo { BLUE GREEN RED } extend enum Foo { YELLOW } """).to_string() == dedent(""" enum Foo { BLUE GREEN RED YELLOW } type Query { foo: Foo } """))
def test_build_schema_ignores_extensions_if_specified(): assert ( build_schema( """ type Query { one: Int } extend type Object { one: Int } extend type Query { two: String } """, ignore_extensions=True, ).to_string() == dedent( """ type Query { one: Int } """ ) )
def test_forbids_duplicate_directive_definition(): with pytest.raises(SDLError) as exc_info: build_schema( """ type Query { foo: String } directive @foo on FIELD directive @foo on MUTATION """ ) assert exc_info.value.to_dict() == { "locations": [{"column": 13, "line": 7}], "message": "Duplicate directive @foo", }
def test_union_type_extension(): assert (build_schema(""" type Query { foo: Foo } type Bar { bar: Int } type Baz { baz: Int } union Foo = Bar extend union Foo = Baz """).to_string() == dedent(""" type Bar { bar: Int } type Baz { baz: Int } union Foo = Bar | Baz type Query { foo: Foo } """))
def test_injected_object_type_extension(): Foo = ObjectType("Foo", [Field("one", String)]) schema = build_schema( """ type Query { foo: Foo } extend type Foo { two: Int } """, additional_types=[Foo], ) assert schema.to_string() == dedent(""" type Foo { one: String two: Int } type Query { foo: Foo } """) assert schema.types["Foo"] is not Foo
def test_executing_interface_default_resolve_type(): schema = build_schema( """ type Query { characters: [Character] } interface Character { name: String! } type Human implements Character { name: String! totalCredits: Int } type Droid implements Character { name: String! primaryFunction: String } """ ) data, _ = graphql_blocking( schema, """ { characters { name ... on Human { totalCredits } ... on Droid { primaryFunction } } } """, root={ "characters": [ { "name": "Han Solo", "totalCredits": 10, "__typename__": "Human", }, { "name": "R2-D2", "primaryFunction": "Astromech", "__typename__": "Droid", }, ] }, ) assert data == { "characters": [ {"name": "Han Solo", "totalCredits": 10}, {"name": "R2-D2", "primaryFunction": "Astromech"}, ] }
def test_unknown_type_referenced(): with pytest.raises(SDLError) as exc_info: build_schema( """ schema { query: Hello } type Hello { bar: Bar } """ ) assert exc_info.value.to_dict() == { "locations": [{"column": 22, "line": 7}], "message": "Type Bar not found in document", }
def test_ignores_specified_directives(): class UppercaseDirective(SchemaDirective): definition = "upper" def on_field(self, field_definition): return wrap_resolver(field_definition, lambda x: x.upper()) build_schema( """ directive @upper on FIELD_DEFINITION type Query @deprecated { foo: String } """, schema_directives=(UppercaseDirective, ), )
def test_does_not_consider_operation_names_or_fragment_name(): with pytest.raises(SDLError) as exc_info: build_schema( """ schema { query: Foo } query Foo { field } fragment Foo on Type { field } """ ) assert exc_info.value.to_dict() == { "locations": [{"column": 24, "line": 3}], "message": "Type Foo not found in document", }
def test_unknown_query_type(): with pytest.raises(SDLError) as exc_info: build_schema( """ schema { query: Wat } type Hello { str: String } """ ) assert exc_info.value.to_dict() == { "locations": [{"column": 24, "line": 3}], "message": "Type Wat not found in document", }
def test_inject_custom_types(): schema = build_schema( """ type Query { foo: UUID } """, additional_types=[UUID], ) assert schema.types["UUID"] is UUID