Skip to content

Commit

Permalink
Add test for GraphQLResolveInfo with custom context
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Feb 29, 2024
1 parent 602f7d7 commit 3111d30
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion tests/type/test_definition.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pickle
import sys
from enum import Enum
from math import isnan, nan
from typing import Dict
Expand All @@ -16,6 +17,7 @@
InterfaceTypeExtensionNode,
ObjectTypeDefinitionNode,
ObjectTypeExtensionNode,
OperationDefinitionNode,
ScalarTypeDefinitionNode,
ScalarTypeExtensionNode,
StringValueNode,
Expand All @@ -24,7 +26,7 @@
ValueNode,
parse_value,
)
from graphql.pyutils import Undefined
from graphql.pyutils import Path, Undefined, is_awaitable
from graphql.type import (
GraphQLArgument,
GraphQLEnumType,
Expand All @@ -37,7 +39,9 @@
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLResolveInfo,
GraphQLScalarType,
GraphQLSchema,
GraphQLString,
GraphQLUnionType,
introspection_types,
Expand Down Expand Up @@ -1301,3 +1305,40 @@ def cannot_redefine_introspection_types():
TypeError, match=f"Redefinition of reserved type '{name}'"
):
introspection_type.__class__(**introspection_type.to_kwargs())


def describe_resolve_info():
class CustomContext:
"""A custom context for testing"""

info_cls = GraphQLResolveInfo

info_args = {
"field_name": "foo",
"field_nodes": [],
"return_type": GraphQLString,
"parent_type": GraphQLObjectType("Foo", {}),
"path": Path(None, "foo", None),
"schema": GraphQLSchema(),
"fragments": {},
"root_value": None,
"operation": OperationDefinitionNode(),
"variable_values": {},
"is_awaitable": is_awaitable,
}

def can_create_resolve_info_with_unspecified_context_type():
info = info_cls(**info_args, context=CustomContext())
assert isinstance(info.context, CustomContext)
info = info_cls(**info_args, context="foo")
assert isinstance(info.context, str)

@pytest.mark.skipif(
sys.version_info < (3, 9), reason="this needs at least Python 3.9"
)
def can_create_resolve_info_with_specified_context_type():
info = info_cls[CustomContext](**info_args, context=CustomContext())
assert isinstance(info.context, CustomContext)
# this should not pass type checking
info = info_cls[CustomContext](**info_args, context="foo") # type: ignore
assert isinstance(info.context, str)

0 comments on commit 3111d30

Please sign in to comment.