Ejemplo n.º 1
0
    def correctly_extend_object_type():
        object_sdl = dedent("""
            type SomeObject implements Foo {
              first: String
            }

            extend type SomeObject implements Bar {
              second: Int
            }

            extend type SomeObject implements Baz {
              third: Float
            }
            """)
        schema = build_schema(object_sdl + dedent("""
                interface Foo
                interface Bar
                interface Baz
                """))

        some_object = assert_object_type(schema.get_type("SomeObject"))
        assert print_type(some_object) + "\n" == dedent("""
            type SomeObject implements Foo & Bar & Baz {
              first: String
              second: Int
              third: Float
            }
            """)

        assert print_all_ast_nodes(some_object) == object_sdl
    def extends_objects_by_adding_new_fields():
        extended_schema = extend_test_schema("""
            extend type Foo {
              newField: String
            }
            """)
        assert print_test_schema_changes(extended_schema) == dedent("""
            type Foo implements SomeInterface {
              name: String
              some: SomeInterface
              tree: [Foo]!
              newField: String
            }
            """)

        foo_type = assert_object_type(extended_schema.get_type("Foo"))
        query_type = assert_object_type(extended_schema.get_type("Query"))
        assert query_type.fields["foo"].type == foo_type
    def correctly_extend_object_type():
        schema = build_schema(
            """
            type SomeObject implements Foo {
              first: String
            }

            extend type SomeObject implements Bar {
              second: Int
            }

            extend type SomeObject implements Baz {
              third: Float
            }

            interface Foo
            interface Bar
            interface Baz
            """
        )

        some_object = assert_object_type(schema.get_type("SomeObject"))
        assert print_type(some_object) == dedent(
            """
            type SomeObject implements Foo & Bar & Baz {
              first: String
              second: Int
              third: Float
            }
            """
        )

        expect_ast_node(
            some_object,
            dedent(
                """
            type SomeObject implements Foo {
              first: String
            }
            """
            ),
        )
        expect_extension_ast_nodes(
            some_object,
            dedent(
                """
            extend type SomeObject implements Bar {
              second: Int
            }

            extend type SomeObject implements Baz {
              third: Float
            }
            """
            ),
        )
 def extends_objects_with_deprecated_fields():
     extended_schema = extend_test_schema("""
         extend type Foo {
           deprecatedField: String @deprecated(reason: "not used anymore")
         }
         """)
     foo_type = assert_object_type(extended_schema.get_type("Foo"))
     deprecated_field_def = foo_type.fields["deprecatedField"]
     assert deprecated_field_def.is_deprecated is True
     assert deprecated_field_def.deprecation_reason == "not used anymore"
Ejemplo n.º 5
0
        def adds_multiple_new_root_types_via_schema_extension():
            schema = build_schema("type Query")
            extend_ast = parse("""
                extend schema {
                  mutation: Mutation
                  subscription: Subscription
                }

                type Mutation
                type Subscription
                """)
            extended_schema = extend_schema(schema, extend_ast)

            mutation_type = assert_object_type(extended_schema.mutation_type)
            assert mutation_type.name == "Mutation"

            subscription_type = assert_object_type(
                extended_schema.subscription_type)
            assert subscription_type.name == "Subscription"
    def can_describe_the_extended_fields():
        extended_schema = extend_test_schema("""
            extend type Query {
              "New field description."
              newField: String
            }
            """)
        query_type = assert_object_type(extended_schema.get_type("Query"))

        assert query_type.fields[
            "newField"].description == "New field description."
    def extends_unions_by_adding_new_types():
        extended_schema = extend_test_schema("""
            extend union SomeUnion = Bar
            """)
        assert print_test_schema_changes(extended_schema) == dedent("""
            union SomeUnion = Foo | Biz | Bar
            """)

        some_union_type = extended_schema.get_type("SomeUnion")
        query_type = assert_object_type(extended_schema.get_type("Query"))
        union_field = query_type.fields["someUnion"]
        assert union_field.type == some_union_type
Ejemplo n.º 8
0
    def allows_to_reference_introspection_types():
        schema = build_schema("""
            type Query {
              introspectionField: __EnumValue
            }
            """)

        query_type = assert_object_type(schema.get_type("Query"))
        __EnumValue = introspection_types["__EnumValue"]
        assert query_type.fields["introspectionField"].type is __EnumValue
        assert schema.get_type(
            "__EnumValue") is introspection_types["__EnumValue"]
Ejemplo n.º 9
0
    def extends_objects_with_deprecated_fields():
        schema = build_schema("type SomeObject")
        extend_ast = parse("""
            extend type SomeObject {
              deprecatedField: String @deprecated(reason: "not used anymore")
            }
            """)
        extended_schema = extend_schema(schema, extend_ast)

        some_type = assert_object_type(extended_schema.get_type("SomeObject"))
        deprecated_field = some_type.fields["deprecatedField"]
        assert deprecated_field.deprecation_reason == "not used anymore"
Ejemplo n.º 10
0
 def one_line_prints_a_short_description():
     description = "This field is awesome"
     output = print_single_field_schema(
         GraphQLField(GraphQLString, description=description))
     assert output == dedent('''
         type Query {
           """This field is awesome"""
           singleField: String
         }
         ''')
     schema = build_schema(output)
     recreated_root = assert_object_type(schema.type_map["Query"])
     recreated_field = recreated_root.fields["singleField"]
     assert recreated_field.description == description
Ejemplo n.º 11
0
        def adds_new_root_types_via_schema_extension():
            schema = build_schema("""
                type Query
                type MutationRoot
                """)
            extension_sdl = dedent("""
                extend schema {
                  mutation: MutationRoot
                }
                """)
            extended_schema = extend_schema(schema, parse(extension_sdl))

            mutation_type = assert_object_type(extended_schema.mutation_type)
            assert mutation_type.name == "MutationRoot"
            assert print_extension_nodes(extended_schema) == extension_sdl
 def preserves_leading_spaces_when_printing_a_description():
     description = '    This field is "awesome"'
     output = print_single_field_schema(
         GraphQLField(GraphQLString, description=description))
     assert output == dedent('''
         type Query {
           """    This field is "awesome"
           """
           singleField: String
         }
         ''')
     schema = build_schema(output)
     recreated_root = assert_object_type(schema.type_map["Query"])
     recreated_field = recreated_root.fields["singleField"]
     assert recreated_field.description == description
 def does_not_one_line_print_a_description_that_ends_with_a_quote():
     description = 'This field is "awesome"'
     output = print_single_field_schema(
         GraphQLField(GraphQLString, description=description))
     assert output == dedent('''
         type Query {
           """
           This field is "awesome"
           """
           singleField: String
         }
         ''')
     schema = build_schema(output)
     recreated_root = assert_object_type(schema.type_map["Query"])
     recreated_field = recreated_root.fields["singleField"]
     assert recreated_field.description == description
Ejemplo n.º 14
0
        def adds_schema_definition_missing_in_the_original_schema():
            schema = build_schema("""
                directive @foo on SCHEMA
                type Foo
                """)
            assert schema.query_type is None

            extension_sdl = dedent("""
                schema @foo {
                  query: Foo
                }
                """)
            extended_schema = extend_schema(schema, parse(extension_sdl))

            query_type = assert_object_type(extended_schema.query_type)
            assert query_type.name == "Foo"
            assert print_ast_node(extended_schema) + "\n" == extension_sdl
    def extends_enums_by_adding_new_values():
        extended_schema = extend_test_schema("""
            extend enum SomeEnum {
              NEW_ENUM
            }
            """)
        assert print_test_schema_changes(extended_schema) == dedent("""
            enum SomeEnum {
              ONE
              TWO
              NEW_ENUM
            }
            """)

        some_enum_type = extended_schema.get_type("SomeEnum")
        query_type = assert_object_type(extended_schema.get_type("Query"))
        enum_field = query_type.fields["someEnum"]
        assert enum_field.type == some_enum_type
    def extends_inputs_by_adding_new_fields():
        extended_schema = extend_test_schema("""
            extend input SomeInput {
              newField: String
            }
            """)
        assert print_test_schema_changes(extended_schema) == dedent("""
            input SomeInput {
              fooArg: String
              newField: String
            }
            """)

        some_input_type = extended_schema.get_type("SomeInput")
        query_type = assert_object_type(extended_schema.get_type("Query"))
        input_field = query_type.fields["someInput"]
        assert input_field.args["input"].type == some_input_type

        foo_directive = assert_directive(extended_schema.get_directive("foo"))
        assert foo_directive.args["input"].type == some_input_type
Ejemplo n.º 17
0
    def builds_types_with_deprecated_fields_and_values():
        schema = GraphQLSchema()
        extend_ast = parse("""
            type SomeObject {
              deprecatedField: String @deprecated(reason: "not used anymore")
            }

            enum SomeEnum {
              DEPRECATED_VALUE @deprecated(reason: "do not use")
            }
            """)
        extended_schema = extend_schema(schema, extend_ast)

        some_type = assert_object_type(extended_schema.get_type("SomeObject"))
        deprecated_field = some_type.fields["deprecatedField"]
        assert deprecated_field.deprecation_reason == "not used anymore"

        some_enum = assert_enum_type(extended_schema.get_type("SomeEnum"))
        deprecated_enum = some_enum.values["DEPRECATED_VALUE"]
        assert deprecated_enum.deprecation_reason == "do not use"
Ejemplo n.º 18
0
    def supports_deprecated_directive():
        sdl = dedent(
            """
            enum MyEnum {
              VALUE
              OLD_VALUE @deprecated
              OTHER_VALUE @deprecated(reason: "Terrible reasons")
            }

            type Query {
              field1: String @deprecated
              field2: Int @deprecated(reason: "Because I said so")
              enum: MyEnum
            }
            """
        )
        assert cycle_sdl(sdl) == sdl

        schema = build_schema(sdl)

        my_enum = assert_enum_type(schema.get_type("MyEnum"))

        value = my_enum.values["VALUE"]
        assert value.is_deprecated is False

        old_value = my_enum.values["OLD_VALUE"]
        assert old_value.is_deprecated is True
        assert old_value.deprecation_reason == "No longer supported"

        other_value = my_enum.values["OTHER_VALUE"]
        assert other_value.is_deprecated is True
        assert other_value.deprecation_reason == "Terrible reasons"

        root_fields = assert_object_type(schema.get_type("Query")).fields
        field1 = root_fields["field1"]
        assert field1.is_deprecated is True
        assert field1.deprecation_reason == "No longer supported"
        field2 = root_fields["field2"]
        assert field2.is_deprecated is True
        assert field2.deprecation_reason == "Because I said so"
    def builds_types_with_deprecated_fields_and_values():
        extended_schema = extend_test_schema("""
            type TypeWithDeprecatedField {
              newDeprecatedField: String @deprecated(reason: "not used anymore")
            }

            enum EnumWithDeprecatedValue {
              DEPRECATED @deprecated(reason: "do not use")
            }
            """)

        deprecated_field_def = assert_object_type(
            extended_schema.get_type(
                "TypeWithDeprecatedField")).fields["newDeprecatedField"]
        assert deprecated_field_def.is_deprecated is True
        assert deprecated_field_def.deprecation_reason == "not used anymore"

        deprecated_enum_def = assert_enum_type(
            extended_schema.get_type(
                "EnumWithDeprecatedValue")).values["DEPRECATED"]
        assert deprecated_enum_def.is_deprecated is True
        assert deprecated_enum_def.deprecation_reason == "do not use"
Ejemplo n.º 20
0
    def correctly_assigns_ast_nodes_to_new_and_extended_types():
        schema = build_schema("""
            type Query

            scalar SomeScalar
            enum SomeEnum
            union SomeUnion
            input SomeInput
            type SomeObject
            interface SomeInterface

            directive @foo on SCALAR
            """)
        first_extension_ast = parse("""
            extend type Query {
              newField(testArg: TestInput): TestEnum
            }

            extend scalar SomeScalar @foo

            extend enum SomeEnum {
              NEW_VALUE
            }

            extend union SomeUnion = SomeObject

            extend input SomeInput {
              newField: String
            }

            extend interface SomeInterface {
              newField: String
            }

            enum TestEnum {
              TEST_VALUE
            }

            input TestInput {
              testInputField: TestEnum
            }
            """)
        extended_schema = extend_schema(schema, first_extension_ast)

        second_extension_ast = parse("""
            extend type Query {
              oneMoreNewField: TestUnion
            }

            extend scalar SomeScalar @test

            extend enum SomeEnum {
              ONE_MORE_NEW_VALUE
            }

            extend union SomeUnion = TestType

            extend input SomeInput {
              oneMoreNewField: String
            }

            extend interface SomeInterface {
              oneMoreNewField: String
            }

            union TestUnion = TestType

            interface TestInterface {
              interfaceField: String
            }

            type TestType implements TestInterface {
              interfaceField: String
            }

            directive @test(arg: Int) repeatable on FIELD | SCALAR
            """)
        extended_twice_schema = extend_schema(extended_schema,
                                              second_extension_ast)

        extend_in_one_go_schema = extend_schema(
            schema, concat_ast([first_extension_ast, second_extension_ast]))
        assert print_schema(extend_in_one_go_schema) == print_schema(
            extended_twice_schema)

        query = assert_object_type(extended_twice_schema.get_type("Query"))
        some_enum = assert_enum_type(
            extended_twice_schema.get_type("SomeEnum"))
        some_union = assert_union_type(
            extended_twice_schema.get_type("SomeUnion"))
        some_scalar = assert_scalar_type(
            extended_twice_schema.get_type("SomeScalar"))
        some_input = assert_input_object_type(
            extended_twice_schema.get_type("SomeInput"))
        some_interface = assert_interface_type(
            extended_twice_schema.get_type("SomeInterface"))

        test_input = assert_input_object_type(
            extended_twice_schema.get_type("TestInput"))
        test_enum = assert_enum_type(
            extended_twice_schema.get_type("TestEnum"))
        test_union = assert_union_type(
            extended_twice_schema.get_type("TestUnion"))
        test_type = assert_object_type(
            extended_twice_schema.get_type("TestType"))
        test_interface = assert_interface_type(
            extended_twice_schema.get_type("TestInterface"))
        test_directive = assert_directive(
            extended_twice_schema.get_directive("test"))

        assert test_type.extension_ast_nodes is None
        assert test_enum.extension_ast_nodes is None
        assert test_union.extension_ast_nodes is None
        assert test_input.extension_ast_nodes is None
        assert test_interface.extension_ast_nodes is None

        assert query.extension_ast_nodes
        assert len(query.extension_ast_nodes) == 2
        assert some_scalar.extension_ast_nodes
        assert len(some_scalar.extension_ast_nodes) == 2
        assert some_enum.extension_ast_nodes
        assert len(some_enum.extension_ast_nodes) == 2
        assert some_union.extension_ast_nodes
        assert len(some_union.extension_ast_nodes) == 2
        assert some_input.extension_ast_nodes
        assert len(some_input.extension_ast_nodes) == 2
        assert some_interface.extension_ast_nodes
        assert len(some_interface.extension_ast_nodes) == 2

        assert {
            test_input.ast_node,
            test_enum.ast_node,
            test_union.ast_node,
            test_interface.ast_node,
            test_type.ast_node,
            test_directive.ast_node,
            *query.extension_ast_nodes,
            *some_scalar.extension_ast_nodes,
            *some_enum.extension_ast_nodes,
            *some_union.extension_ast_nodes,
            *some_input.extension_ast_nodes,
            *some_interface.extension_ast_nodes,
        } == {
            *first_extension_ast.definitions, *second_extension_ast.definitions
        }

        new_field = query.fields["newField"]
        assert print_ast_node(
            new_field) == "newField(testArg: TestInput): TestEnum"
        assert print_ast_node(
            new_field.args["testArg"]) == "testArg: TestInput"
        assert (print_ast_node(
            query.fields["oneMoreNewField"]) == "oneMoreNewField: TestUnion")

        new_value = some_enum.values["NEW_VALUE"]
        assert print_ast_node(new_value) == "NEW_VALUE"

        one_more_new_value = some_enum.values["ONE_MORE_NEW_VALUE"]
        assert print_ast_node(one_more_new_value) == "ONE_MORE_NEW_VALUE"
        assert print_ast_node(
            some_input.fields["newField"]) == "newField: String"
        assert (print_ast_node(
            some_input.fields["oneMoreNewField"]) == "oneMoreNewField: String")
        assert print_ast_node(
            some_interface.fields["newField"]) == "newField: String"
        assert (print_ast_node(some_interface.fields["oneMoreNewField"]) ==
                "oneMoreNewField: String")

        assert (print_ast_node(
            test_input.fields["testInputField"]) == "testInputField: TestEnum")

        test_value = test_enum.values["TEST_VALUE"]
        assert print_ast_node(test_value) == "TEST_VALUE"

        assert (print_ast_node(test_interface.fields["interfaceField"]) ==
                "interfaceField: String")
        assert (print_ast_node(
            test_type.fields["interfaceField"]) == "interfaceField: String")
        assert print_ast_node(test_directive.args["arg"]) == "arg: Int"
    def correctly_assigns_ast_nodes_to_new_and_extended_types():
        extended_schema = extend_test_schema("""
            extend type Query {
              newField(testArg: TestInput): TestEnum
            }

            extend scalar SomeScalar @foo

            extend enum SomeEnum {
              NEW_VALUE
            }

            extend union SomeUnion = Bar

            extend input SomeInput {
              newField: String
            }

            extend interface SomeInterface {
              newField: String
            }

            enum TestEnum {
              TEST_VALUE
            }

            input TestInput {
              testInputField: TestEnum
            }
            """)
        ast = parse("""
            extend type Query {
              oneMoreNewField: TestUnion
            }

            extend scalar SomeScalar @test

            extend enum SomeEnum {
              ONE_MORE_NEW_VALUE
            }

            extend union SomeUnion = TestType

            extend input SomeInput {
              oneMoreNewField: String
            }

            extend interface SomeInterface {
              oneMoreNewField: String
            }

            union TestUnion = TestType

            interface TestInterface {
              interfaceField: String
            }

            type TestType implements TestInterface {
              interfaceField: String
            }

            directive @test(arg: Int) repeatable on FIELD | SCALAR
            """)
        extended_twice_schema = extend_schema(extended_schema, ast)

        query = assert_object_type(extended_twice_schema.get_type("Query"))
        some_enum = assert_enum_type(
            extended_twice_schema.get_type("SomeEnum"))
        some_union = assert_union_type(
            extended_twice_schema.get_type("SomeUnion"))
        some_scalar = assert_scalar_type(
            extended_twice_schema.get_type("SomeScalar"))
        some_input = assert_input_object_type(
            extended_twice_schema.get_type("SomeInput"))
        some_interface = assert_interface_type(
            extended_twice_schema.get_type("SomeInterface"))

        test_input = assert_input_object_type(
            extended_twice_schema.get_type("TestInput"))
        test_enum = assert_enum_type(
            extended_twice_schema.get_type("TestEnum"))
        test_union = assert_union_type(
            extended_twice_schema.get_type("TestUnion"))
        test_type = assert_object_type(
            extended_twice_schema.get_type("TestType"))
        test_interface = assert_interface_type(
            extended_twice_schema.get_type("TestInterface"))
        test_directive = assert_directive(
            extended_twice_schema.get_directive("test"))

        assert test_type.extension_ast_nodes is None
        assert test_enum.extension_ast_nodes is None
        assert test_union.extension_ast_nodes is None
        assert test_input.extension_ast_nodes is None
        assert test_interface.extension_ast_nodes is None

        assert query.extension_ast_nodes
        assert len(query.extension_ast_nodes) == 2
        assert some_scalar.extension_ast_nodes
        assert len(some_scalar.extension_ast_nodes) == 2
        assert some_enum.extension_ast_nodes
        assert len(some_enum.extension_ast_nodes) == 2
        assert some_union.extension_ast_nodes
        assert len(some_union.extension_ast_nodes) == 2
        assert some_input.extension_ast_nodes
        assert len(some_input.extension_ast_nodes) == 2
        assert some_interface.extension_ast_nodes
        assert len(some_interface.extension_ast_nodes) == 2

        definitions: List[Optional[TypeSystemDefinitionNode]] = [
            test_input.ast_node,
            test_enum.ast_node,
            test_union.ast_node,
            test_interface.ast_node,
            test_type.ast_node,
            test_directive.ast_node,
        ]
        extensions: List[Optional[FrozenList[TypeSystemDefinitionNode]]] = [
            query.extension_ast_nodes,
            some_scalar.extension_ast_nodes,
            some_enum.extension_ast_nodes,
            some_union.extension_ast_nodes,
            some_input.extension_ast_nodes,
            some_interface.extension_ast_nodes,
        ]
        for extension_ast_nodes in extensions:
            if extension_ast_nodes:
                definitions.extend(extension_ast_nodes)
        restored_extension_ast = DocumentNode(definitions=definitions)

        assert print_schema(extend_schema(
            test_schema,
            restored_extension_ast)) == print_schema(extended_twice_schema)

        new_field = query.fields["newField"]
        assert print_ast_node(
            new_field) == "newField(testArg: TestInput): TestEnum"
        assert print_ast_node(
            new_field.args["testArg"]) == "testArg: TestInput"
        assert (print_ast_node(
            query.fields["oneMoreNewField"]) == "oneMoreNewField: TestUnion")

        new_value = some_enum.values["NEW_VALUE"]
        assert some_enum
        assert print_ast_node(new_value) == "NEW_VALUE"

        one_more_new_value = some_enum.values["ONE_MORE_NEW_VALUE"]
        assert one_more_new_value
        assert print_ast_node(one_more_new_value) == "ONE_MORE_NEW_VALUE"
        assert print_ast_node(
            some_input.fields["newField"]) == "newField: String"
        assert (print_ast_node(
            some_input.fields["oneMoreNewField"]) == "oneMoreNewField: String")
        assert print_ast_node(
            some_interface.fields["newField"]) == "newField: String"
        assert (print_ast_node(some_interface.fields["oneMoreNewField"]) ==
                "oneMoreNewField: String")

        assert (print_ast_node(
            test_input.fields["testInputField"]) == "testInputField: TestEnum")

        test_value = test_enum.values["TEST_VALUE"]
        assert test_value
        assert print_ast_node(test_value) == "TEST_VALUE"

        assert (print_ast_node(test_interface.fields["interfaceField"]) ==
                "interfaceField: String")
        assert (print_ast_node(
            test_type.fields["interfaceField"]) == "interfaceField: String")
        assert print_ast_node(test_directive.args["arg"]) == "arg: Int"
Ejemplo n.º 22
0
 def returns_true_for_object_type():
     assert is_object_type(ObjectType) is True
     assert_object_type(ObjectType)
    def supports_deprecated_directive():
        sdl = dedent(
            """
            enum MyEnum {
              VALUE
              OLD_VALUE @deprecated
              OTHER_VALUE @deprecated(reason: "Terrible reasons")
            }

            input MyInput {
              oldInput: String @deprecated
              otherInput: String @deprecated(reason: "Use newInput")
              newInput: String
            }

            type Query {
              field1: String @deprecated
              field2: Int @deprecated(reason: "Because I said so")
              enum: MyEnum
              field3(oldArg: String @deprecated, arg: String): String
              field4(oldArg: String @deprecated(reason: "Why not?"), arg: String): String
              field5(arg: MyInput): String
            }
            """  # noqa: E501
        )
        assert cycle_sdl(sdl) == sdl

        schema = build_schema(sdl)

        my_enum = assert_enum_type(schema.get_type("MyEnum"))

        value = my_enum.values["VALUE"]
        assert value.deprecation_reason is None

        old_value = my_enum.values["OLD_VALUE"]
        assert old_value.deprecation_reason == "No longer supported"

        other_value = my_enum.values["OTHER_VALUE"]
        assert other_value.deprecation_reason == "Terrible reasons"

        root_fields = assert_object_type(schema.get_type("Query")).fields
        field1 = root_fields["field1"]
        assert field1.deprecation_reason == "No longer supported"
        field2 = root_fields["field2"]
        assert field2.deprecation_reason == "Because I said so"

        input_fields = assert_input_object_type(schema.get_type("MyInput")).fields

        new_input = input_fields["newInput"]
        assert new_input.deprecation_reason is None

        old_input = input_fields["oldInput"]
        assert old_input.deprecation_reason == "No longer supported"

        other_input = input_fields["otherInput"]
        assert other_input.deprecation_reason == "Use newInput"

        field3_old_arg = root_fields["field3"].args["oldArg"]
        assert field3_old_arg.deprecation_reason == "No longer supported"

        field4_old_arg = root_fields["field4"].args["oldArg"]
        assert field4_old_arg.deprecation_reason == "Why not?"
Ejemplo n.º 24
0
    def correctly_assign_ast_nodes():
        sdl = dedent("""
            schema {
              query: Query
            }

            type Query {
              testField(testArg: TestInput): TestUnion
            }

            input TestInput {
              testInputField: TestEnum
            }

            enum TestEnum {
              TEST_VALUE
            }

            union TestUnion = TestType

            interface TestInterface {
              interfaceField: String
            }

            type TestType implements TestInterface {
              interfaceField: String
            }

            scalar TestScalar

            directive @test(arg: TestScalar) on FIELD
            """)
        schema = build_schema(sdl)
        query = assert_object_type(schema.get_type("Query"))
        test_input = assert_input_object_type(schema.get_type("TestInput"))
        test_enum = assert_enum_type(schema.get_type("TestEnum"))
        test_union = assert_union_type(schema.get_type("TestUnion"))
        test_interface = assert_interface_type(
            schema.get_type("TestInterface"))
        test_type = assert_object_type(schema.get_type("TestType"))
        test_scalar = assert_scalar_type(schema.get_type("TestScalar"))
        test_directive = assert_directive(schema.get_directive("test"))

        restored_schema_ast = DocumentNode(definitions=[
            schema.ast_node,
            query.ast_node,
            test_input.ast_node,
            test_enum.ast_node,
            test_union.ast_node,
            test_interface.ast_node,
            test_type.ast_node,
            test_scalar.ast_node,
            test_directive.ast_node,
        ])
        assert print_ast(restored_schema_ast) == sdl

        test_field = query.fields["testField"]
        assert print_ast(test_field.ast_node) == (
            "testField(testArg: TestInput): TestUnion")
        assert print_ast(
            test_field.args["testArg"].ast_node) == "testArg: TestInput"
        assert print_ast(test_input.fields["testInputField"].ast_node) == (
            "testInputField: TestEnum")
        test_enum_value = test_enum.values["TEST_VALUE"]
        assert test_enum_value
        assert print_ast(test_enum_value.ast_node) == "TEST_VALUE"
        assert print_ast(test_interface.fields["interfaceField"].ast_node) == (
            "interfaceField: String")
        assert print_ast(
            test_directive.args["arg"].ast_node) == "arg: TestScalar"
Ejemplo n.º 25
0
 def returns_false_for_wrapped_object_type():
     assert is_object_type(GraphQLList(ObjectType)) is False
     with raises(TypeError):
         assert_object_type(GraphQLList(ObjectType))
    def correctly_assign_ast_nodes():
        sdl = dedent(
            """
            schema {
              query: Query
            }

            type Query {
              testField(testArg: TestInput): TestUnion
            }

            input TestInput {
              testInputField: TestEnum
            }

            enum TestEnum {
              TEST_VALUE
            }

            union TestUnion = TestType

            interface TestInterface {
              interfaceField: String
            }

            type TestType implements TestInterface {
              interfaceField: String
            }

            scalar TestScalar

            directive @test(arg: TestScalar) on FIELD
            """
        )
        ast = parse(sdl, no_location=True)

        schema = build_ast_schema(ast)
        query = assert_object_type(schema.get_type("Query"))
        test_input = assert_input_object_type(schema.get_type("TestInput"))
        test_enum = assert_enum_type(schema.get_type("TestEnum"))
        test_union = assert_union_type(schema.get_type("TestUnion"))
        test_interface = assert_interface_type(schema.get_type("TestInterface"))
        test_type = assert_object_type(schema.get_type("TestType"))
        test_scalar = assert_scalar_type(schema.get_type("TestScalar"))
        test_directive = assert_directive(schema.get_directive("test"))

        assert (
            schema.ast_node,
            query.ast_node,
            test_input.ast_node,
            test_enum.ast_node,
            test_union.ast_node,
            test_interface.ast_node,
            test_type.ast_node,
            test_scalar.ast_node,
            test_directive.ast_node,
        ) == ast.definitions

        test_field = query.fields["testField"]
        expect_ast_node(test_field, "testField(testArg: TestInput): TestUnion")
        expect_ast_node(test_field.args["testArg"], "testArg: TestInput")
        expect_ast_node(test_input.fields["testInputField"], "testInputField: TestEnum")
        test_enum_value = test_enum.values["TEST_VALUE"]
        expect_ast_node(test_enum_value, "TEST_VALUE")
        expect_ast_node(
            test_interface.fields["interfaceField"], "interfaceField: String"
        )
        expect_ast_node(test_directive.args["arg"], "arg: TestScalar")