Skip to content

Commit 206de32

Browse files
authored
Allow user to pass in a custom resolve info context type (#213)
1 parent e256148 commit 206de32

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

src/graphql/type/definition.py

+44-19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations # Python < 3.10
44

5+
import sys
56
from enum import Enum
67
from typing import (
78
TYPE_CHECKING,
@@ -554,30 +555,54 @@ def to_kwargs(self) -> GraphQLFieldKwargs:
554555
def __copy__(self) -> GraphQLField: # pragma: no cover
555556
return self.__class__(**self.to_kwargs())
556557

558+
if sys.version_info < (3, 9) or sys.version_info >= (3, 11):
559+
TContext = TypeVar("TContext")
557560

558-
class GraphQLResolveInfo(NamedTuple):
559-
"""Collection of information passed to the resolvers.
561+
class GraphQLResolveInfo(NamedTuple, Generic[TContext]):
562+
"""Collection of information passed to the resolvers.
560563
561-
This is always passed as the first argument to the resolvers.
564+
This is always passed as the first argument to the resolvers.
562565
563-
Note that contrary to the JavaScript implementation, the context (commonly used to
564-
represent an authenticated user, or request-specific caches) is included here and
565-
not passed as an additional argument.
566-
"""
566+
Note that contrary to the JavaScript implementation, the context (commonly used
567+
to represent an authenticated user, or request-specific caches) is included here
568+
and not passed as an additional argument.
569+
"""
567570

568-
field_name: str
569-
field_nodes: List[FieldNode]
570-
return_type: GraphQLOutputType
571-
parent_type: GraphQLObjectType
572-
path: Path
573-
schema: GraphQLSchema
574-
fragments: Dict[str, FragmentDefinitionNode]
575-
root_value: Any
576-
operation: OperationDefinitionNode
577-
variable_values: Dict[str, Any]
578-
context: Any
579-
is_awaitable: Callable[[Any], bool]
571+
field_name: str
572+
field_nodes: List[FieldNode]
573+
return_type: GraphQLOutputType
574+
parent_type: GraphQLObjectType
575+
path: Path
576+
schema: GraphQLSchema
577+
fragments: Dict[str, FragmentDefinitionNode]
578+
root_value: Any
579+
operation: OperationDefinitionNode
580+
variable_values: Dict[str, Any]
581+
context: TContext
582+
is_awaitable: Callable[[Any], bool]
583+
else:
584+
class GraphQLResolveInfo(NamedTuple):
585+
"""Collection of information passed to the resolvers.
586+
587+
This is always passed as the first argument to the resolvers.
588+
589+
Note that contrary to the JavaScript implementation, the context (commonly used
590+
to represent an authenticated user, or request-specific caches) is included here
591+
and not passed as an additional argument.
592+
"""
580593

594+
field_name: str
595+
field_nodes: List[FieldNode]
596+
return_type: GraphQLOutputType
597+
parent_type: GraphQLObjectType
598+
path: Path
599+
schema: GraphQLSchema
600+
fragments: Dict[str, FragmentDefinitionNode]
601+
root_value: Any
602+
operation: OperationDefinitionNode
603+
variable_values: Dict[str, Any]
604+
context: Any
605+
is_awaitable: Callable[[Any], bool]
581606

582607
# Note: Contrary to the Javascript implementation of GraphQLFieldResolver,
583608
# the context is passed as part of the GraphQLResolveInfo and any arguments

0 commit comments

Comments
 (0)