1
1
from asyncio import ensure_future
2
2
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
4
4
5
5
from .error import GraphQLError
6
6
from .execution import execute , ExecutionResult , Middleware
7
7
from .language import parse , Source
8
8
from .pyutils import MaybeAwaitable
9
9
from .type import GraphQLSchema , validate_schema
10
+ from .execution .execute import ExecutionResult , ExecutionContext
10
11
11
- __all__ = [' graphql' , ' graphql_sync' ]
12
+ __all__ = [" graphql" , " graphql_sync" ]
12
13
13
14
14
15
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 :
24
26
"""Execute a GraphQL operation asynchronously.
25
27
26
28
This is the primary entry point function for fulfilling GraphQL operations
@@ -59,6 +61,8 @@ async def graphql(
59
61
a value or method on the source value with the field's name).
60
62
:arg middleware:
61
63
The middleware to wrap the resolvers with
64
+ :arg execution_context_class:
65
+ The execution context class to use to build the context
62
66
"""
63
67
# Always return asynchronously for a consistent API.
64
68
result = graphql_impl (
@@ -69,7 +73,9 @@ async def graphql(
69
73
variable_values ,
70
74
operation_name ,
71
75
field_resolver ,
72
- middleware )
76
+ middleware ,
77
+ execution_context_class ,
78
+ )
73
79
74
80
if isawaitable (result ):
75
81
return await cast (Awaitable [ExecutionResult ], result )
@@ -78,15 +84,16 @@ async def graphql(
78
84
79
85
80
86
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 :
90
97
"""Execute a GraphQL operation synchronously.
91
98
92
99
The graphql_sync function also fulfills GraphQL operations by parsing,
@@ -102,7 +109,9 @@ def graphql_sync(
102
109
variable_values ,
103
110
operation_name ,
104
111
field_resolver ,
105
- middleware )
112
+ middleware ,
113
+ execution_context_class ,
114
+ )
106
115
107
116
# Assert that the execution was synchronous.
108
117
if isawaitable (result ):
@@ -114,14 +123,16 @@ def graphql_sync(
114
123
115
124
116
125
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 ]:
125
136
"""Execute a query, return asynchronously only if necessary."""
126
137
# Validate Schema
127
138
schema_validation_errors = validate_schema (schema )
@@ -139,6 +150,7 @@ def graphql_impl(
139
150
140
151
# Validate
141
152
from .validation import validate
153
+
142
154
validation_errors = validate (schema , document )
143
155
if validation_errors :
144
156
return ExecutionResult (data = None , errors = validation_errors )
@@ -152,4 +164,6 @@ def graphql_impl(
152
164
variable_values ,
153
165
operation_name ,
154
166
field_resolver ,
155
- middleware )
167
+ middleware ,
168
+ execution_context_class ,
169
+ )
0 commit comments