Exemplo n.º 1
0
    def test_select_with_arguments(self) -> None:
        class FooResolver(Resolver):
            def __init__(self, foo: int) -> None:
                super().__init__("Foo")

                self.__foo = foo

            def bar(self, abc: int) -> int:
                return abc + self.__foo

        class QueryResolver(Resolver):
            def __init__(self):
                super().__init__("__Query")

            def foo(self, foo: int) -> FooResolver:
                return FooResolver(foo)

        self.assertQueryResult(
            '{"foo": {"bar": 7}}',
            s.Schema(
                [
                    s.Object("Foo", {"bar": s.Field(s.Int, {"abc": s.Int})})
                ],
                s.Object("__Query", {
                    "foo": s.Field("Foo", {"foo": s.Int})
                })
            ),
            "{foo(foo: 3){bar(abc: 4)}}",
            QueryResolver()
        )
Exemplo n.º 2
0
 def test_required_arguments(self) -> None:
     schema = s.Schema(
         [s.Object("Foo", {"abc": s.Int})],
         s.Object("Query",
                  {"foo": s.Field("Foo", {"bar": s.NonNull(s.Int)})}))
     self.assertNoErrors("{ foo(bar: 10) {abc}}", schema)
     self.assertValidationError("{ foo {abc}}", schema,
                                "Argument `bar` is required")
Exemplo n.º 3
0
    def test_non_null_param_with_default(self):
        class Query(Resolver):
            def foo(self, a: int) -> int:
                return a + 5

        self.assertQueryResult(
            '{"foo": 8}',
            s.Schema(
                [
                ],
                s.Object("Query", {
                    "foo": s.Field(s.Int, {"a": s.NonNull(s.Int)}),
                })
            ),
            "query ($a: Int! = 3){ foo(a: $a) }",
            Query()
        )
Exemplo n.º 4
0
    def test_default_argument(self):
        class Query(Resolver):
            def foo(self, a: int) -> int:
                return a + 5

        self.assertQueryResult(
            '{"foo": 7}',
            s.Schema(
                [
                ],
                s.Object("Query", {
                    "foo": s.Field(s.Int, {"a": s.InputValue(s.Int, 2)}),
                })
            ),
            "{ foo }",
            Query()
        )
Exemplo n.º 5
0
    def test_variables_with_default(self):
        class Query(Resolver):
            def foo(self, a, b):
                return a + b

        self.assertQueryResult(
            '{"foo": 6}',
            s.Schema(
                [
                ],
                s.Object("Query", {
                    "foo": s.Field(s.Int, {"a": s.Int, "b": s.Int}),
                })
            ),
            "query ($v1: Int, $v2: Int = 5){ foo(a: $v1, b: $v2) }",
            Query(),
            variables={"v1": 1}
        )
Exemplo n.º 6
0
    def test_variables(self):
        class Query(Resolver):
            def foo(self, a, b):
                return a + b

        self.assertQueryResult(
            '{"foo": 8}',
            s.Schema(
                [
                ],
                s.Object("Query", {
                    "foo": s.Field(s.Int, {"a": s.Int, "b": s.Int}),
                })
            ),
            "query ($v: Int){ foo(a: 3, b: $v) }",
            Query(),
            variables={"v": 5}
        )
Exemplo n.º 7
0
    def test_null_by_default(self):
        class Query(Resolver):
            def foo(self, a: t.Optional[int]) -> int:
                if a is None:
                    return 1
                return a + 5

        self.assertQueryResult(
            '{"foo": 1}',
            s.Schema(
                [
                ],
                s.Object("Query", {
                    "foo": s.Field(s.Int, {"a": s.Int}),
                })
            ),
            "{ foo }",
            Query()
        )
    def test(self):
        schema = s.Schema(
            [
                # Use s.Object, s.Interface, s.InputObject
                # and s.Union to define types
                s.Object(
                    "Foo",
                    {
                        # Use s.Int, s.String, etc for scalar types
                        "foo": s.String
                    })
            ],

            # query type is the second parameter
            s.Object(
                "QueryRoot",
                {
                    # You can refer user defined types by names
                    # Use s.NonNull to define non nullable type
                    "foo": s.NonNull("Foo")
                }),

            # optional mutation type
            s.Object(
                "MutationRoot",
                {
                    # Example of full field definition
                    "bar":
                    s.Field(
                        # Field type
                        # Scalar can be non-null either
                        s.NonNull(s.Boolean),
                        {
                            "arg1":
                            s.Int,
                            # full specification works like this
                            "arg2":
                            s.InputValue(
                                # argument type
                                s.NonNull(s.Float),
                                # argument default (can be None)
                                1.2,
                                # Field description (reported in
                                # introspection response)
                                "Some float argument")
                        },
                        # Field description
                        "Mutate bar",
                        # is deprecated?
                        True,
                        # deprecation reason
                        "Some deprecation reason")
                }))

        print(schema.format())

        # Resolver understands which type it resolves by type
        # name. If you want different class name or few
        # classes to implement the same Resolver pass type
        # name as the first parameter.
        class FooResolver(Resolver):
            def __init__(self):
                super().__init__()

                # You can resolve fields just by assigning
                # their values to resolver attributes
                self.foo = "Hello :)"

        class QueryRootResolver(Resolver):
            # field without arguments can be plain attribute
            # or method
            def foo(self):
                # When field is user defined type - return
                # resolver
                return FooResolver()

        class MutationRootResolver(Resolver):
            def bar(self, arg1, arg2):
                return arg1 + arg2 > 10

        executor = Executor(schema, QueryRootResolver(),
                            MutationRootResolver())

        # run simple query
        try:
            # this will print
            # {"foo": {"foo": "Hello :)"}}
            print(json.dumps(executor.query("{foo{foo}}", {})))
        except GqlParsingError as e:
            print("Wrong query syntax: " + str(e))
        except GqlValidationError as e:
            print("Invalid query: " + str(e))
        except GqlExecutionError as e:
            print("Error in resolvers: " + str(e))

        # run query with variables
        # prints
        # {"bar": true}
        print(
            json.dumps(
                executor.query("mutation ($a: Int){bar(arg1: $a, arg2: 5.0)}",
                               {"a": 11})))
Exemplo n.º 9
0
 def test_do_not_require_nullable_args(self) -> None:
     schema = s.Schema([],
                       s.Object("Query",
                                {"foo": s.Field(s.Int, {"bar": s.Int})}))
     self.assertNoErrors("{ foo }", schema)