From 3111d30c2a2a021566fbef8b9acfd22f10d2bf08 Mon Sep 17 00:00:00 2001 From: Christoph Zwerschke Date: Thu, 29 Feb 2024 08:59:50 +0100 Subject: [PATCH] Add test for GraphQLResolveInfo with custom context --- tests/type/test_definition.py | 43 ++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/tests/type/test_definition.py b/tests/type/test_definition.py index cb38a678..818e2522 100644 --- a/tests/type/test_definition.py +++ b/tests/type/test_definition.py @@ -1,4 +1,5 @@ import pickle +import sys from enum import Enum from math import isnan, nan from typing import Dict @@ -16,6 +17,7 @@ InterfaceTypeExtensionNode, ObjectTypeDefinitionNode, ObjectTypeExtensionNode, + OperationDefinitionNode, ScalarTypeDefinitionNode, ScalarTypeExtensionNode, StringValueNode, @@ -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, @@ -37,7 +39,9 @@ GraphQLList, GraphQLNonNull, GraphQLObjectType, + GraphQLResolveInfo, GraphQLScalarType, + GraphQLSchema, GraphQLString, GraphQLUnionType, introspection_types, @@ -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)