Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to pass in a custom resolve info context type (#213) #214

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 44 additions & 19 deletions src/graphql/type/definition.py
Cito marked this conversation as resolved.
Show resolved Hide resolved
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 < (3, 9) or sys.version_info >= (3, 11):
TContext = TypeVar("TContext")

class GraphQLResolveInfo(NamedTuple):
"""Collection of information passed to the resolvers.
class GraphQLResolveInfo(NamedTuple, Generic[TContext]):
"""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: TContext
is_awaitable: Callable[[Any], bool]
else:
class GraphQLResolveInfo(NamedTuple):
"""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: Any
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
Loading