File: test_resolve.py

package info (click to toggle)
graphql-core 3.2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,384 kB
  • sloc: python: 45,812; makefile: 26; sh: 13
file content (327 lines) | stat: -rw-r--r-- 10,894 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
from collections import ChainMap
from typing import Any

from graphql.error import GraphQLError
from graphql.execution import ExecutionResult, execute_sync
from graphql.language import SourceLocation, parse
from graphql.type import (
    GraphQLArgument,
    GraphQLField,
    GraphQLID,
    GraphQLInputField,
    GraphQLInputObjectType,
    GraphQLInt,
    GraphQLList,
    GraphQLObjectType,
    GraphQLSchema,
    GraphQLString,
)


def describe_execute_resolve_function():
    def _test_schema(test_field: GraphQLField) -> GraphQLSchema:
        return GraphQLSchema(GraphQLObjectType("Query", {"test": test_field}))

    def default_function_accesses_attributes():
        class RootValue:
            test = "testValue"

        assert execute_sync(
            schema=_test_schema(GraphQLField(GraphQLString)),
            document=parse("{ test }"),
            root_value=RootValue(),
        ) == (
            {"test": "testValue"},
            None,
        )

    def default_function_accesses_keys_of_dict():
        root_value = {"test": "testValue"}

        assert execute_sync(
            schema=_test_schema(GraphQLField(GraphQLString)),
            document=parse("{ test }"),
            root_value=root_value,
        ) == ({"test": "testValue"}, None)

    def default_function_accesses_keys_of_chain_map():
        # use a mapping that is not a subclass of dict
        root_value = ChainMap({"test": "testValue"})

        assert execute_sync(
            schema=_test_schema(GraphQLField(GraphQLString)),
            document=parse("{ test }"),
            root_value=root_value,
        ) == ({"test": "testValue"}, None)

    def default_function_calls_methods():
        class RootValue:
            _secret = "secretValue"

            def test(self, _info):
                return self._secret

        assert execute_sync(
            schema=_test_schema(GraphQLField(GraphQLString)),
            document=parse("{ test }"),
            root_value=RootValue(),
        ) == (
            {"test": "secretValue"},
            None,
        )

    def default_function_passes_args_and_context():
        class Adder:
            _num: int

            def __init__(self, num):
                self._num = num

            def test(self, info, addend1: int):
                return self._num + addend1 + info.context.addend2

        root_value = Adder(700)

        schema = _test_schema(
            GraphQLField(GraphQLInt, args={"addend1": GraphQLArgument(GraphQLInt)})
        )

        class ContextValue:
            addend2 = 9

        context_value = ContextValue()
        document = parse("{ test(addend1: 80) }")

        assert execute_sync(
            schema=schema,
            document=document,
            root_value=root_value,
            context_value=context_value,
        ) == (
            {"test": 789},
            None,
        )

    def uses_provided_resolve_function():
        schema = _test_schema(
            GraphQLField(
                GraphQLString,
                args={
                    "aStr": GraphQLArgument(GraphQLString),
                    "aInt": GraphQLArgument(GraphQLInt),
                },
                resolve=lambda source, info, **args: repr([source, args]),
            )
        )

        def execute_query(query: str, root_value: Any = None) -> ExecutionResult:
            document = parse(query)
            return execute_sync(
                schema=schema,
                document=document,
                root_value=root_value,
            )

        assert execute_query("{ test }") == ({"test": "[None, {}]"}, None)

        assert execute_query("{ test }", "Source!") == (
            {"test": "['Source!', {}]"},
            None,
        )

        assert execute_query('{ test(aStr: "String!") }', "Source!") == (
            {"test": "['Source!', {'aStr': 'String!'}]"},
            None,
        )

        assert execute_query('{ test(aInt: -123, aStr: "String!") }', "Source!") == (
            {"test": "['Source!', {'aStr': 'String!', 'aInt': -123}]"},
            None,
        )

    def transforms_arguments_using_out_names():
        # This is an extension of GraphQL.js.
        schema = _test_schema(
            GraphQLField(
                GraphQLString,
                args={
                    "aStr": GraphQLArgument(GraphQLString, out_name="a_str"),
                    "aInt": GraphQLArgument(GraphQLInt, out_name="a_int"),
                },
                resolve=lambda source, info, **args: repr([source, args]),
            )
        )

        def execute_query(query: str, root_value: Any = None) -> ExecutionResult:
            document = parse(query)
            return execute_sync(schema=schema, document=document, root_value=root_value)

        assert execute_query("{ test }") == ({"test": "[None, {}]"}, None)

        assert execute_query("{ test }", "Source!") == (
            {"test": "['Source!', {}]"},
            None,
        )

        assert execute_query('{ test(aStr: "String!") }', "Source!") == (
            {"test": "['Source!', {'a_str': 'String!'}]"},
            None,
        )

        assert execute_query('{ test(aInt: -123, aStr: "String!") }', "Source!") == (
            {"test": "['Source!', {'a_str': 'String!', 'a_int': -123}]"},
            None,
        )

    def transforms_arguments_with_inputs_using_out_names():
        # This is an extension of GraphQL.js.
        TestInputObject = GraphQLInputObjectType(
            "TestInputObjectType",
            lambda: {
                "inputOne": GraphQLInputField(GraphQLString, out_name="input_one"),
                "inputRecursive": GraphQLInputField(
                    TestInputObject, out_name="input_recursive"
                ),
            },
        )

        schema = _test_schema(
            GraphQLField(
                GraphQLString,
                args={"aInput": GraphQLArgument(TestInputObject, out_name="a_input")},
                resolve=lambda source, info, **args: repr([source, args]),
            )
        )

        def execute_query(query: str, root_value: Any = None) -> ExecutionResult:
            document = parse(query)
            return execute_sync(schema=schema, document=document, root_value=root_value)

        assert execute_query("{ test }") == ({"test": "[None, {}]"}, None)

        assert execute_query('{ test(aInput: {inputOne: "String!"}) }', "Source!") == (
            {"test": "['Source!', {'a_input': {'input_one': 'String!'}}]"},
            None,
        )

        assert execute_query(
            '{ test(aInput: {inputRecursive: {inputOne: "SourceRecursive!"}}) }',
            "Source!",
        ) == (
            {
                "test": "['Source!',"
                " {'a_input': {'input_recursive': {'input_one': 'SourceRecursive!'}}}]"
            },
            None,
        )

    def transforms_default_values_using_out_names():
        # This is an extension of GraphQL.js.
        resolver_kwargs: Any

        def search_resolver(_obj: None, _info, **kwargs):
            nonlocal resolver_kwargs
            resolver_kwargs = kwargs
            return [{"id": "42"}]

        filters_type = GraphQLInputObjectType(
            "SearchFilters",
            {"pageSize": GraphQLInputField(GraphQLInt, out_name="page_size")},
        )
        result_type = GraphQLObjectType("SearchResult", {"id": GraphQLField(GraphQLID)})
        query = GraphQLObjectType(
            "Query",
            {
                "search": GraphQLField(
                    GraphQLList(result_type),
                    {
                        "searchFilters": GraphQLArgument(
                            filters_type, {"pageSize": 10}, out_name="search_filters"
                        )
                    },
                    resolve=search_resolver,
                )
            },
        )
        schema = GraphQLSchema(query)

        resolver_kwargs = None
        result = execute_sync(schema, parse("{ search { id } }"))
        assert result == ({"search": [{"id": "42"}]}, None)
        assert resolver_kwargs == {"search_filters": {"page_size": 10}}

        resolver_kwargs = None
        result = execute_sync(
            schema, parse("{ search(searchFilters:{pageSize: 25}) { id } }")
        )
        assert result == ({"search": [{"id": "42"}]}, None)
        assert resolver_kwargs == {"search_filters": {"page_size": 25}}

        resolver_kwargs = None
        result = execute_sync(
            schema,
            parse(
                """
                    query ($searchFilters: SearchFilters) {
                      search(searchFilters: $searchFilters) { id }
                }
                """
            ),
        )
        assert result == ({"search": [{"id": "42"}]}, None)
        assert resolver_kwargs == {"search_filters": {"page_size": 10}}

        resolver_kwargs = None
        result = execute_sync(
            schema,
            parse(
                """
                    query ($searchFilters: SearchFilters) {
                      search(searchFilters: $searchFilters) { id }
                }
                """
            ),
            variable_values={"searchFilters": {"pageSize": 25}},
        )
        assert result == ({"search": [{"id": "42"}]}, None)
        assert resolver_kwargs == {"search_filters": {"page_size": 25}}

        resolver_kwargs = None
        result = execute_sync(
            schema,
            parse(
                """
                    query ($searchFilters: SearchFilters = {pageSize: 25}) {
                      search(searchFilters: $searchFilters) { id }
                }
                """
            ),
        )
        assert result == ({"search": [{"id": "42"}]}, None)
        assert resolver_kwargs == {"search_filters": {"page_size": 25}}

    def pass_error_from_resolver_wrapped_as_located_graphql_error():
        def resolve(_obj, _info):
            raise ValueError("Some error")

        schema = _test_schema(GraphQLField(GraphQLString, resolve=resolve))
        result = execute_sync(schema, parse("{ test }"))

        assert result == (
            {"test": None},
            [{"message": "Some error", "locations": [(1, 3)], "path": ["test"]}],
        )

        assert result.errors is not None
        error = result.errors[0]
        assert isinstance(error, GraphQLError)
        assert str(error) == "Some error\n\nGraphQL request:1:3\n1 | { test }\n  |   ^"
        assert error.positions == [2]
        locations = error.locations
        assert locations == [(1, 3)]
        location = locations[0]
        assert isinstance(location, SourceLocation)
        assert location == SourceLocation(1, 3)
        original_error = error.original_error
        assert isinstance(original_error, ValueError)
        assert str(original_error) == "Some error"