|
1 | 1 | import pickle
|
| 2 | +import sys |
2 | 3 | from enum import Enum
|
3 | 4 | from math import isnan, nan
|
4 | 5 | from typing import Dict
|
|
16 | 17 | InterfaceTypeExtensionNode,
|
17 | 18 | ObjectTypeDefinitionNode,
|
18 | 19 | ObjectTypeExtensionNode,
|
| 20 | + OperationDefinitionNode, |
19 | 21 | ScalarTypeDefinitionNode,
|
20 | 22 | ScalarTypeExtensionNode,
|
21 | 23 | StringValueNode,
|
|
24 | 26 | ValueNode,
|
25 | 27 | parse_value,
|
26 | 28 | )
|
27 |
| -from graphql.pyutils import Undefined |
| 29 | +from graphql.pyutils import Path, Undefined, is_awaitable |
28 | 30 | from graphql.type import (
|
29 | 31 | GraphQLArgument,
|
30 | 32 | GraphQLEnumType,
|
|
37 | 39 | GraphQLList,
|
38 | 40 | GraphQLNonNull,
|
39 | 41 | GraphQLObjectType,
|
| 42 | + GraphQLResolveInfo, |
40 | 43 | GraphQLScalarType,
|
| 44 | + GraphQLSchema, |
41 | 45 | GraphQLString,
|
42 | 46 | GraphQLUnionType,
|
43 | 47 | introspection_types,
|
@@ -1301,3 +1305,40 @@ def cannot_redefine_introspection_types():
|
1301 | 1305 | TypeError, match=f"Redefinition of reserved type '{name}'"
|
1302 | 1306 | ):
|
1303 | 1307 | introspection_type.__class__(**introspection_type.to_kwargs())
|
| 1308 | + |
| 1309 | + |
| 1310 | +def describe_resolve_info(): |
| 1311 | + class CustomContext: |
| 1312 | + """A custom context for testing""" |
| 1313 | + |
| 1314 | + info_cls = GraphQLResolveInfo |
| 1315 | + |
| 1316 | + info_args = { |
| 1317 | + "field_name": "foo", |
| 1318 | + "field_nodes": [], |
| 1319 | + "return_type": GraphQLString, |
| 1320 | + "parent_type": GraphQLObjectType("Foo", {}), |
| 1321 | + "path": Path(None, "foo", None), |
| 1322 | + "schema": GraphQLSchema(), |
| 1323 | + "fragments": {}, |
| 1324 | + "root_value": None, |
| 1325 | + "operation": OperationDefinitionNode(), |
| 1326 | + "variable_values": {}, |
| 1327 | + "is_awaitable": is_awaitable, |
| 1328 | + } |
| 1329 | + |
| 1330 | + def can_create_resolve_info_with_unspecified_context_type(): |
| 1331 | + info = info_cls(**info_args, context=CustomContext()) |
| 1332 | + assert isinstance(info.context, CustomContext) |
| 1333 | + info = info_cls(**info_args, context="foo") |
| 1334 | + assert isinstance(info.context, str) |
| 1335 | + |
| 1336 | + @pytest.mark.skipif( |
| 1337 | + sys.version_info < (3, 9), reason="this needs at least Python 3.9" |
| 1338 | + ) |
| 1339 | + def can_create_resolve_info_with_specified_context_type(): |
| 1340 | + info = info_cls[CustomContext](**info_args, context=CustomContext()) |
| 1341 | + assert isinstance(info.context, CustomContext) |
| 1342 | + # this should not pass type checking |
| 1343 | + info = info_cls[CustomContext](**info_args, context="foo") # type: ignore |
| 1344 | + assert isinstance(info.context, str) |
0 commit comments