Skip to content

Commit 3111d30

Browse files
committed
Add test for GraphQLResolveInfo with custom context
1 parent 602f7d7 commit 3111d30

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

tests/type/test_definition.py

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pickle
2+
import sys
23
from enum import Enum
34
from math import isnan, nan
45
from typing import Dict
@@ -16,6 +17,7 @@
1617
InterfaceTypeExtensionNode,
1718
ObjectTypeDefinitionNode,
1819
ObjectTypeExtensionNode,
20+
OperationDefinitionNode,
1921
ScalarTypeDefinitionNode,
2022
ScalarTypeExtensionNode,
2123
StringValueNode,
@@ -24,7 +26,7 @@
2426
ValueNode,
2527
parse_value,
2628
)
27-
from graphql.pyutils import Undefined
29+
from graphql.pyutils import Path, Undefined, is_awaitable
2830
from graphql.type import (
2931
GraphQLArgument,
3032
GraphQLEnumType,
@@ -37,7 +39,9 @@
3739
GraphQLList,
3840
GraphQLNonNull,
3941
GraphQLObjectType,
42+
GraphQLResolveInfo,
4043
GraphQLScalarType,
44+
GraphQLSchema,
4145
GraphQLString,
4246
GraphQLUnionType,
4347
introspection_types,
@@ -1301,3 +1305,40 @@ def cannot_redefine_introspection_types():
13011305
TypeError, match=f"Redefinition of reserved type '{name}'"
13021306
):
13031307
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

Comments
 (0)