Skip to content

Commit a003063

Browse files
syrusakbaryCito
authored andcommitted
Added excution_context_class for custom ExecutionContext (#6)
1 parent a16edf1 commit a003063

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

graphql/execution/execute.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from inspect import isawaitable
22
from typing import (
33
Any, Awaitable, Dict, Iterable, List, NamedTuple, Optional, Set, Union,
4-
Tuple, cast)
4+
Tuple, Type, cast)
55

66
from ..error import GraphQLError, INVALID, located_error
77
from ..language import (
@@ -69,6 +69,7 @@ def execute(
6969
variable_values: Dict[str, Any]=None,
7070
operation_name: str=None,
7171
field_resolver: GraphQLFieldResolver=None,
72+
execution_context_class: Type[ExecutionContext]=ExecutionContext,
7273
middleware: Middleware=None
7374
) -> MaybeAwaitable[ExecutionResult]:
7475
"""Execute a GraphQL operation.
@@ -87,7 +88,7 @@ def execute(
8788

8889
# If a valid execution context cannot be created due to incorrect
8990
# arguments, a "Response" with only errors is returned.
90-
exe_context = ExecutionContext.build(
91+
exe_context = execution_context_class.build(
9192
schema, document, root_value, context_value,
9293
variable_values, operation_name, field_resolver, middleware)
9394

graphql/graphql.py

+45-31
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
from asyncio import ensure_future
22
from inspect import isawaitable
3-
from typing import Any, Awaitable, Callable, Dict, Union, cast
3+
from typing import Any, Awaitable, Callable, Dict, Union, Type, cast
44

55
from .error import GraphQLError
66
from .execution import execute, ExecutionResult, Middleware
77
from .language import parse, Source
88
from .pyutils import MaybeAwaitable
99
from .type import GraphQLSchema, validate_schema
10+
from .execution.execute import ExecutionResult, ExecutionContext
1011

11-
__all__ = ['graphql', 'graphql_sync']
12+
__all__ = ["graphql", "graphql_sync"]
1213

1314

1415
async def graphql(
15-
schema: GraphQLSchema,
16-
source: Union[str, Source],
17-
root_value: Any=None,
18-
context_value: Any=None,
19-
variable_values: Dict[str, Any]=None,
20-
operation_name: str=None,
21-
field_resolver: Callable=None,
22-
middleware: Middleware=None
23-
) -> ExecutionResult:
16+
schema: GraphQLSchema,
17+
source: Union[str, Source],
18+
root_value: Any=None,
19+
context_value: Any=None,
20+
variable_values: Dict[str, Any]=None,
21+
operation_name: str=None,
22+
field_resolver: Callable=None,
23+
middleware: Middleware=None,
24+
execution_context_class: Type[ExecutionContext] = ExecutionContext,
25+
) -> ExecutionResult:
2426
"""Execute a GraphQL operation asynchronously.
2527
2628
This is the primary entry point function for fulfilling GraphQL operations
@@ -59,6 +61,8 @@ async def graphql(
5961
a value or method on the source value with the field's name).
6062
:arg middleware:
6163
The middleware to wrap the resolvers with
64+
:arg execution_context_class:
65+
The execution context class to use to build the context
6266
"""
6367
# Always return asynchronously for a consistent API.
6468
result = graphql_impl(
@@ -69,7 +73,9 @@ async def graphql(
6973
variable_values,
7074
operation_name,
7175
field_resolver,
72-
middleware)
76+
middleware,
77+
execution_context_class,
78+
)
7379

7480
if isawaitable(result):
7581
return await cast(Awaitable[ExecutionResult], result)
@@ -78,15 +84,16 @@ async def graphql(
7884

7985

8086
def graphql_sync(
81-
schema: GraphQLSchema,
82-
source: Union[str, Source],
83-
root_value: Any=None,
84-
context_value: Any=None,
85-
variable_values: Dict[str, Any]=None,
86-
operation_name: str=None,
87-
field_resolver: Callable=None,
88-
middleware: Middleware=None
89-
) -> ExecutionResult:
87+
schema: GraphQLSchema,
88+
source: Union[str, Source],
89+
root_value: Any=None,
90+
context_value: Any=None,
91+
variable_values: Dict[str, Any]=None,
92+
operation_name: str=None,
93+
field_resolver: Callable=None,
94+
middleware: Middleware=None,
95+
execution_context_class: Type[ExecutionContext] = ExecutionContext,
96+
) -> ExecutionResult:
9097
"""Execute a GraphQL operation synchronously.
9198
9299
The graphql_sync function also fulfills GraphQL operations by parsing,
@@ -102,7 +109,9 @@ def graphql_sync(
102109
variable_values,
103110
operation_name,
104111
field_resolver,
105-
middleware)
112+
middleware,
113+
execution_context_class,
114+
)
106115

107116
# Assert that the execution was synchronous.
108117
if isawaitable(result):
@@ -114,14 +123,16 @@ def graphql_sync(
114123

115124

116125
def graphql_impl(
117-
schema,
118-
source,
119-
root_value,
120-
context_value,
121-
variable_values,
122-
operation_name,
123-
field_resolver,
124-
middleware) -> MaybeAwaitable[ExecutionResult]:
126+
schema,
127+
source,
128+
root_value,
129+
context_value,
130+
variable_values,
131+
operation_name,
132+
field_resolver,
133+
middleware,
134+
execution_context_class,
135+
) -> MaybeAwaitable[ExecutionResult]:
125136
"""Execute a query, return asynchronously only if necessary."""
126137
# Validate Schema
127138
schema_validation_errors = validate_schema(schema)
@@ -139,6 +150,7 @@ def graphql_impl(
139150

140151
# Validate
141152
from .validation import validate
153+
142154
validation_errors = validate(schema, document)
143155
if validation_errors:
144156
return ExecutionResult(data=None, errors=validation_errors)
@@ -152,4 +164,6 @@ def graphql_impl(
152164
variable_values,
153165
operation_name,
154166
field_resolver,
155-
middleware)
167+
middleware,
168+
execution_context_class,
169+
)

0 commit comments

Comments
 (0)