Skip to content

Commit 1751563

Browse files
author
Ted Zadniprovskyi
committed
Allow user to pass in a custom resolve info context type (#213)
1 parent e256148 commit 1751563

File tree

1 file changed

+45
-20
lines changed

1 file changed

+45
-20
lines changed

src/graphql/type/definition.py

+45-20
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.minor == 9 or sys.version_info.minor == 10:
559+
class GraphQLResolveInfo(NamedTuple):
560+
"""Collection of information passed to the resolvers.
557561
558-
class GraphQLResolveInfo(NamedTuple):
559-
"""Collection of information passed to the resolvers.
562+
This is always passed as the first argument to the resolvers.
560563
561-
This is always passed as the first argument to the resolvers.
562-
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-
"""
564+
Note that contrary to the JavaScript implementation, the context (commonly used to
565+
represent an authenticated user, or request-specific caches) is included here and
566+
not passed as an additional argument.
567+
"""
567568

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]
569+
field_name: str
570+
field_nodes: List[FieldNode]
571+
return_type: GraphQLOutputType
572+
parent_type: GraphQLObjectType
573+
path: Path
574+
schema: GraphQLSchema
575+
fragments: Dict[str, FragmentDefinitionNode]
576+
root_value: Any
577+
operation: OperationDefinitionNode
578+
variable_values: Dict[str, Any]
579+
context: Any
580+
is_awaitable: Callable[[Any], bool]
581+
else:
582+
TContext = TypeVar("TContext")
583+
584+
class GraphQLResolveInfo(NamedTuple, Generic[TContext]):
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 to
590+
represent an authenticated user, or request-specific caches) is included here and
591+
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: TContext
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)