Skip to content

Commit

Permalink
Allow user to pass in a custom resolve info context type (graphql-pyt…
Browse files Browse the repository at this point in the history
  • Loading branch information
Ted Zadniprovskyi committed Feb 24, 2024
1 parent e256148 commit 1751563
Showing 1 changed file with 45 additions and 20 deletions.
65 changes: 45 additions & 20 deletions src/graphql/type/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations # Python < 3.10

import sys
from enum import Enum
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -554,30 +555,54 @@ def to_kwargs(self) -> GraphQLFieldKwargs:
def __copy__(self) -> GraphQLField: # pragma: no cover
return self.__class__(**self.to_kwargs())

if sys.version_info.minor == 9 or sys.version_info.minor == 10:
class GraphQLResolveInfo(NamedTuple):
"""Collection of information passed to the resolvers.
class GraphQLResolveInfo(NamedTuple):
"""Collection of information passed to the resolvers.
This is always passed as the first argument to the resolvers.
This is always passed as the first argument to the resolvers.
Note that contrary to the JavaScript implementation, the context (commonly used to
represent an authenticated user, or request-specific caches) is included here and
not passed as an additional argument.
"""
Note that contrary to the JavaScript implementation, the context (commonly used to
represent an authenticated user, or request-specific caches) is included here and
not passed as an additional argument.
"""

field_name: str
field_nodes: List[FieldNode]
return_type: GraphQLOutputType
parent_type: GraphQLObjectType
path: Path
schema: GraphQLSchema
fragments: Dict[str, FragmentDefinitionNode]
root_value: Any
operation: OperationDefinitionNode
variable_values: Dict[str, Any]
context: Any
is_awaitable: Callable[[Any], bool]
field_name: str
field_nodes: List[FieldNode]
return_type: GraphQLOutputType
parent_type: GraphQLObjectType
path: Path
schema: GraphQLSchema
fragments: Dict[str, FragmentDefinitionNode]
root_value: Any
operation: OperationDefinitionNode
variable_values: Dict[str, Any]
context: Any
is_awaitable: Callable[[Any], bool]
else:
TContext = TypeVar("TContext")

class GraphQLResolveInfo(NamedTuple, Generic[TContext]):
"""Collection of information passed to the resolvers.
This is always passed as the first argument to the resolvers.
Note that contrary to the JavaScript implementation, the context (commonly used to
represent an authenticated user, or request-specific caches) is included here and
not passed as an additional argument.
"""

field_name: str
field_nodes: List[FieldNode]
return_type: GraphQLOutputType
parent_type: GraphQLObjectType
path: Path
schema: GraphQLSchema
fragments: Dict[str, FragmentDefinitionNode]
root_value: Any
operation: OperationDefinitionNode
variable_values: Dict[str, Any]
context: TContext
is_awaitable: Callable[[Any], bool]

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

0 comments on commit 1751563

Please sign in to comment.