def fails_on_very_deep_lists_more_than_7_levels():
        schema = GraphQLSchema(
            GraphQLObjectType(
                'Query', {
                    'foo':
                    GraphQLField(
                        GraphQLList(
                            GraphQLList(
                                GraphQLList(
                                    GraphQLList(
                                        GraphQLList(
                                            GraphQLList(
                                                GraphQLList(
                                                    GraphQLList(
                                                        GraphQLString)))))))))
                }))

        introspection = introspection_from_schema(schema)

        with raises(TypeError) as exc_info:
            build_client_schema(introspection)

        assert str(exc_info.value) == (
            'Query fields cannot be resolved:'
            ' Decorated type deeper than introspection query.')
예제 #2
0
        def recursive_interfaces():
            sdl = """
                type Query {
                  foo: Foo
                }

                type Foo {
                  foo: String
                }
                """
            schema = build_schema(sdl, assume_valid=True)
            introspection = introspection_from_schema(schema)

            foo_type_introspection = introspection["__schema"]["types"][1]
            assert foo_type_introspection["name"] == "Foo"
            assert foo_type_introspection["interfaces"] == []
            # we need to patch here since invalid interfaces cannot be built with Python
            foo_type_introspection["interfaces"] = [
                {"kind": "OBJECT", "name": "Foo", "ofType": None}
            ]

            with raises(TypeError) as exc_info:
                build_client_schema(introspection)
            assert str(exc_info.value) == (
                "Foo interfaces cannot be resolved: "
                "Expected Foo to be a GraphQL Interface type."
            )
예제 #3
0
    def throws_when_missing_interfaces():
        null_interface_introspection = {
            "__schema": {
                "queryType": {
                    "name": "QueryType"
                },
                "types": [{
                    "kind":
                    "OBJECT",
                    "name":
                    "QueryType",
                    "fields": [{
                        "name": "aString",
                        "args": [],
                        "type": {
                            "kind": "SCALAR",
                            "name": "String",
                            "ofType": None,
                        },
                        "isDeprecated": False,
                    }],
                }],
            }
        }

        with raises(TypeError) as exc_info:
            build_client_schema(null_interface_introspection)

        assert str(exc_info.value) == (
            "Introspection result missing interfaces:"
            " {'kind': 'OBJECT', 'name': 'QueryType',"
            " 'fields': [{'name': 'aString', 'args': [],"
            " 'type': {'kind': 'SCALAR', 'name': 'String', 'ofType': None},"
            " 'isDeprecated': False}]}")
    def throws_when_missing_interfaces():
        null_interface_introspection = {
            '__schema': {
                'queryType': {
                    'name': 'QueryType'
                },
                'types': [{
                    'kind':
                    'OBJECT',
                    'name':
                    'QueryType',
                    'fields': [{
                        'name': 'aString',
                        'args': [],
                        'type': {
                            'kind': 'SCALAR',
                            'name': 'String',
                            'ofType': None
                        },
                        'isDeprecated': False
                    }]
                }]
            }
        }

        with raises(TypeError) as exc_info:
            build_client_schema(null_interface_introspection)

        assert str(exc_info.value) == (
            'Introspection result missing interfaces:'
            " {'kind': 'OBJECT', 'name': 'QueryType',"
            " 'fields': [{'name': 'aString', 'args': [],"
            " 'type': {'kind': 'SCALAR', 'name': 'String', 'ofType': None},"
            " 'isDeprecated': False}]}")
예제 #5
0
    def fails_on_a_very_deep_non_null_more_than_7_levels():
        schema = GraphQLSchema(
            GraphQLObjectType(
                "Query",
                {
                    "foo":
                    GraphQLField(
                        GraphQLList(
                            GraphQLNonNull(
                                GraphQLList(
                                    GraphQLNonNull(
                                        GraphQLList(
                                            GraphQLNonNull(
                                                GraphQLList(
                                                    GraphQLNonNull(
                                                        GraphQLString)))))))))
                },
            ))

        introspection = introspection_from_schema(schema)

        with raises(TypeError) as exc_info:
            build_client_schema(introspection)

        assert str(exc_info.value) == (
            "Query fields cannot be resolved:"
            " Decorated type deeper than introspection query.")
예제 #6
0
        def recursive_union():
            sdl = """
                type Query {
                  foo: Foo
                }

                union Foo
                """
            schema = build_schema(sdl, assume_valid=True)
            introspection = introspection_from_schema(schema)

            foo_type_introspection = introspection["__schema"]["types"][1]
            assert foo_type_introspection["name"] == "Foo"
            assert foo_type_introspection["kind"] == "UNION"
            assert foo_type_introspection["possibleTypes"] == []
            # we need to patch here since invalid unions cannot be built with Python
            foo_type_introspection["possibleTypes"] = [
                {"kind": "UNION", "name": "Foo", "ofType": None}
            ]

            with raises(TypeError) as exc_info:
                build_client_schema(introspection)
            assert str(exc_info.value) == (
                "Foo types cannot be resolved:"
                " Expected Foo to be a GraphQL Object type."
            )
예제 #7
0
        def throws_when_type_reference_is_missing_name():
            introspection = introspection_from_schema(dummy_schema)

            assert introspection["__schema"]["queryType"]["name"] == "Query"
            del introspection["__schema"]["queryType"]["name"]

            with raises(TypeError) as exc_info:
                build_client_schema(introspection)

            assert str(exc_info.value) == "Unknown type reference: {}"
예제 #8
0
        def throws_when_missing_directive_args():
            introspection = introspection_from_schema(dummy_schema)

            some_directive_introspection = introspection["__schema"]["directives"][0]
            assert some_directive_introspection["name"] == "SomeDirective"
            assert some_directive_introspection["args"] == []
            del some_directive_introspection["args"]

            with raises(
                TypeError,
                match="^Introspection result missing directive args:"
                r" {'name': 'SomeDirective', .*}\.$",
            ):
                build_client_schema(introspection)
예제 #9
0
        def throws_when_referenced_unknown_type():
            introspection = introspection_from_schema(dummy_schema)

            introspection["__schema"]["types"] = [
                type_ for type_ in introspection["__schema"]["types"]
                if type_["name"] != "Query"
            ]

            with raises(TypeError) as exc_info:
                build_client_schema(introspection)

            assert str(exc_info.value) == (
                "Invalid or incomplete schema, unknown type: Query."
                " Ensure that a full introspection query is used"
                " in order to build a client schema.")
        def throws_when_missing_directive_locations():
            introspection = introspection_from_schema(dummy_schema)
            some_directive_introspection = introspection["__schema"][
                "directives"][0]

            assert some_directive_introspection["name"] == "SomeDirective"
            assert some_directive_introspection["locations"] == ["QUERY"]
            del some_directive_introspection["locations"]  # type: ignore

            with raises(
                    TypeError,
                    match="^Introspection result missing directive locations:"
                    r" {'name': 'SomeDirective', .*}\.$",
            ):
                build_client_schema(introspection)
        def throws_when_missing_kind():
            introspection = introspection_from_schema(dummy_schema)
            query_type_introspection = next(
                type_ for type_ in introspection["__schema"]["types"]
                if type_["name"] == "Query")
            assert query_type_introspection["kind"] == "OBJECT"
            del query_type_introspection["kind"]

            with raises(
                    TypeError,
                    match=r"^Invalid or incomplete introspection result\."
                    " Ensure that a full introspection query is used"
                    r" in order to build a client schema: {'name': 'Query', .*}\.$",
            ):
                build_client_schema(introspection)
예제 #12
0
        def throws_when_missing_directive_args():
            introspection = introspection_from_schema(dummy_schema)

            some_directive_introspection = introspection["__schema"]["directives"][0]
            assert some_directive_introspection["name"] == "SomeDirective"
            assert some_directive_introspection["args"] == []
            del some_directive_introspection["args"]

            with raises(TypeError) as exc_info:
                build_client_schema(introspection)

            assert str(exc_info.value).startswith(
                "Introspection result missing directive args:"
                " {'name': 'SomeDirective',"
            )
예제 #13
0
        def throws_when_missing_possible_types():
            introspection = introspection_from_schema(dummy_schema)

            some_union_introspection = next(
                type_ for type_ in introspection["__schema"]["types"]
                if type_["name"] == "SomeUnion")
            assert some_union_introspection["possibleTypes"]
            del some_union_introspection["possibleTypes"]

            with raises(
                    TypeError,
                    match="^Introspection result missing possibleTypes:"
                    r" {'kind': 'UNION', 'name': 'SomeUnion', .*}\.$",
            ):
                build_client_schema(introspection)
예제 #14
0
    def fails_on_a_very_deep_non_null_more_than_7_levels():
        schema = build_schema("""
            type Query {
              foo: [[[[String!]!]!]!]
            }
            """)

        introspection = introspection_from_schema(schema)

        with raises(TypeError) as exc_info:
            build_client_schema(introspection)

        assert str(exc_info.value) == (
            "Query fields cannot be resolved:"
            " Decorated type deeper than introspection query.")
예제 #15
0
        def throws_when_missing_enum_values():
            introspection = introspection_from_schema(dummy_schema)

            some_enum_introspection = next(
                type_ for type_ in introspection["__schema"]["types"]
                if type_["name"] == "SomeEnum")
            assert some_enum_introspection["enumValues"]
            del some_enum_introspection["enumValues"]

            with raises(
                    TypeError,
                    match="^Introspection result missing enumValues:"
                    r" {'kind': 'ENUM', 'name': 'SomeEnum', .*}\.$",
            ):
                build_client_schema(introspection)
예제 #16
0
        def throws_when_missing_field_args():
            introspection = introspection_from_schema(dummy_schema)

            query_type_introspection = next(
                type_ for type_ in introspection["__schema"]["types"]
                if type_["name"] == "Query")
            assert query_type_introspection["fields"][0]["args"]
            del query_type_introspection["fields"][0]["args"]

            with raises(
                    TypeError,
                    match="^Query fields cannot be resolved."
                    r" Introspection result missing field args: {'name': 'foo', .*}\.$",
            ):
                build_client_schema(introspection)
예제 #17
0
        def throws_when_missing_input_fields():
            introspection = introspection_from_schema(dummy_schema)

            some_input_object_introspection = next(
                type_ for type_ in introspection["__schema"]["types"]
                if type_["name"] == "SomeInputObject")
            assert some_input_object_introspection["inputFields"]
            del some_input_object_introspection["inputFields"]

            with raises(
                    TypeError,
                    match="^Introspection result missing inputFields:"
                    r" {'kind': 'INPUT_OBJECT', 'name': 'SomeInputObject', .*}\.$",
            ):
                build_client_schema(introspection)
예제 #18
0
    def can_use_client_schema_for_limited_execution():
        schema = build_schema(
            """
            scalar CustomScalar

            type Query {
              foo(custom1: CustomScalar, custom2: CustomScalar): String
            }
            """
        )

        introspection = introspection_from_schema(schema)
        client_schema = build_client_schema(introspection)

        class Data:
            foo = "bar"
            unused = "value"

        result = graphql_sync(
            client_schema,
            "query Limited($v: CustomScalar) { foo(custom1: 123, custom2: $v) }",
            root_value=Data(),
            variable_values={"v": "baz"},
        )

        assert result.data == {"foo": "bar"}
예제 #19
0
        def throws_when_missing_interfaces():
            introspection = introspection_from_schema(dummy_schema)

            query_type_introspection = [
                type_ for type_ in introspection["__schema"]["types"]
                if type_["name"] == "Query"
            ][0]
            assert query_type_introspection["interfaces"] == []
            del query_type_introspection["interfaces"]

            with raises(TypeError) as exc_info:
                build_client_schema(introspection)

            assert str(exc_info.value).startswith(
                "Introspection result missing interfaces:"
                " {'kind': 'OBJECT', 'name': 'Query',")
예제 #20
0
    def can_build_invalid_schema():
        schema = build_schema("type Query", assume_valid=True)

        introspection = introspection_from_schema(schema)
        client_schema = build_client_schema(introspection, assume_valid=True)

        assert client_schema.to_kwargs()["assume_valid"] is True
예제 #21
0
    def uses_built_in_scalars_when_possible():
        custom_scalar = GraphQLScalarType("CustomScalar",
                                          serialize=lambda: None)

        schema = GraphQLSchema(
            GraphQLObjectType(
                "Scalars",
                {
                    "int": GraphQLField(GraphQLInt),
                    "float": GraphQLField(GraphQLFloat),
                    "string": GraphQLField(GraphQLString),
                    "boolean": GraphQLField(GraphQLBoolean),
                    "id": GraphQLField(GraphQLID),
                    "custom": GraphQLField(custom_scalar),
                },
            ))

        check_schema(schema)

        introspection = introspection_from_schema(schema)
        client_schema = build_client_schema(introspection)

        # Built-ins are used
        assert client_schema.get_type("Int") is GraphQLInt
        assert client_schema.get_type("Float") is GraphQLFloat
        assert client_schema.get_type("String") is GraphQLString
        assert client_schema.get_type("Boolean") is GraphQLBoolean
        assert client_schema.get_type("ID") is GraphQLID

        # Custom are built
        assert client_schema.get_type("CustomScalar") is not custom_scalar
예제 #22
0
    def can_use_client_schema_for_limited_execution():
        custom_scalar = GraphQLScalarType("CustomScalar",
                                          serialize=lambda: None)

        schema = GraphQLSchema(
            GraphQLObjectType(
                "Query",
                {
                    "foo":
                    GraphQLField(
                        GraphQLString,
                        args={
                            "custom1": GraphQLArgument(custom_scalar),
                            "custom2": GraphQLArgument(custom_scalar),
                        },
                    )
                },
            ))

        introspection = introspection_from_schema(schema)
        client_schema = build_client_schema(introspection)

        class Data:
            foo = "bar"
            unused = "value"

        result = graphql_sync(
            client_schema,
            "query Limited($v: CustomScalar) { foo(custom1: 123, custom2: $v) }",
            Data(),
            variable_values={"v": "baz"},
        )

        assert result.data == {"foo": "bar"}
예제 #23
0
        def throws_when_missing_input_fields():
            introspection = introspection_from_schema(dummy_schema)

            some_input_object_introspection = [
                type_ for type_ in introspection["__schema"]["types"]
                if type_["name"] == "SomeInputObject"
            ][0]
            assert some_input_object_introspection["inputFields"]
            del some_input_object_introspection["inputFields"]

            with raises(TypeError) as exc_info:
                build_client_schema(introspection)

            assert str(exc_info.value).startswith(
                "Introspection result missing inputFields:"
                " {'kind': 'INPUT_OBJECT', 'name': 'SomeInputObject',")
예제 #24
0
        def throws_when_missing_interfaces():
            introspection = introspection_from_schema(dummy_schema)

            query_type_introspection = next(
                type_ for type_ in introspection["__schema"]["types"]
                if type_["name"] == "Query")
            assert query_type_introspection["interfaces"] == []
            del query_type_introspection["interfaces"]

            with raises(
                    TypeError,
                    match="^Query interfaces cannot be resolved."
                    " Introspection result missing interfaces:"
                    r" {'kind': 'OBJECT', 'name': 'Query', .*}\.$",
            ):
                build_client_schema(introspection)
예제 #25
0
        def throws_when_missing_field_args():
            introspection = introspection_from_schema(dummy_schema)

            query_type_introspection = [
                type_ for type_ in introspection["__schema"]["types"]
                if type_["name"] == "Query"
            ][0]
            assert query_type_introspection["fields"][0]["args"]
            del query_type_introspection["fields"][0]["args"]

            with raises(TypeError) as exc_info:
                build_client_schema(introspection)

            assert str(exc_info.value).startswith(
                "Query fields cannot be resolved:"
                " Introspection result missing field args: {'name': 'foo',")
    def uses_built_in_scalars_when_possible():
        custom_scalar = GraphQLScalarType('CustomScalar',
                                          serialize=lambda: None)

        schema = GraphQLSchema(
            GraphQLObjectType(
                'Scalars', {
                    'int': GraphQLField(GraphQLInt),
                    'float': GraphQLField(GraphQLFloat),
                    'string': GraphQLField(GraphQLString),
                    'boolean': GraphQLField(GraphQLBoolean),
                    'id': GraphQLField(GraphQLID),
                    'custom': GraphQLField(custom_scalar)
                }))

        check_schema(schema)

        introspection = introspection_from_schema(schema)
        client_schema = build_client_schema(introspection)

        # Built-ins are used
        assert client_schema.get_type('Int') is GraphQLInt
        assert client_schema.get_type('Float') is GraphQLFloat
        assert client_schema.get_type('String') is GraphQLString
        assert client_schema.get_type('Boolean') is GraphQLBoolean
        assert client_schema.get_type('ID') is GraphQLID

        # Custom are built
        assert client_schema.get_type('CustomScalar') is not custom_scalar
예제 #27
0
        def throws_when_missing_possible_types():
            introspection = introspection_from_schema(dummy_schema)

            some_union_introspection = [
                type_ for type_ in introspection["__schema"]["types"]
                if type_["name"] == "SomeUnion"
            ][0]
            assert some_union_introspection["possibleTypes"]
            del some_union_introspection["possibleTypes"]

            with raises(TypeError) as exc_info:
                build_client_schema(introspection)

            assert str(exc_info.value).startswith(
                "Introspection result missing possibleTypes:"
                " {'kind': 'UNION', 'name': 'SomeUnion',")
예제 #28
0
        def throws_when_missing_enum_values():
            introspection = introspection_from_schema(dummy_schema)

            some_enum_introspection = [
                type_ for type_ in introspection["__schema"]["types"]
                if type_["name"] == "SomeEnum"
            ][0]
            assert some_enum_introspection["enumValues"]
            del some_enum_introspection["enumValues"]

            with raises(TypeError) as exc_info:
                build_client_schema(introspection)

            assert str(exc_info.value).startswith(
                "Introspection result missing enumValues:"
                " {'kind': 'ENUM', 'name': 'SomeEnum',")
예제 #29
0
    def uses_built_in_scalars_when_possible():
        sdl = dedent("""
            scalar CustomScalar

            type Query {
              int: Int
              float: Float
              string: String
              boolean: Boolean
              id: ID
              custom: CustomScalar
            }
            """)

        assert cycle_introspection(sdl) == sdl

        schema = build_schema(sdl)
        introspection = introspection_from_schema(schema)
        client_schema = build_client_schema(introspection)

        # Built-ins are used
        assert client_schema.get_type("Int") is GraphQLInt
        assert client_schema.get_type("Float") is GraphQLFloat
        assert client_schema.get_type("String") is GraphQLString
        assert client_schema.get_type("Boolean") is GraphQLBoolean
        assert client_schema.get_type("ID") is GraphQLID

        # Custom are built
        custom_scalar = schema.get_type("CustomScalar")
        assert client_schema.get_type("CustomScalar") is not custom_scalar
    def can_use_client_schema_for_limited_execution():
        custom_scalar = GraphQLScalarType('CustomScalar',
                                          serialize=lambda: None)

        schema = GraphQLSchema(
            GraphQLObjectType(
                'Query', {
                    'foo':
                    GraphQLField(GraphQLString,
                                 args={
                                     'custom1': GraphQLArgument(custom_scalar),
                                     'custom2': GraphQLArgument(custom_scalar)
                                 })
                }))

        introspection = introspection_from_schema(schema)
        client_schema = build_client_schema(introspection)

        class Data:
            foo = 'bar'
            unused = 'value'

        result = graphql_sync(client_schema,
                              'query Limited($v: CustomScalar) {'
                              ' foo(custom1: 123, custom2: $v) }',
                              Data(),
                              variable_values={'v': 'baz'})

        assert result.data == {'foo': 'bar'}