11from asyncio import ensure_future
22from 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
55from .error import GraphQLError
66from .execution import execute , ExecutionResult , Middleware
77from .language import parse , Source
88from .pyutils import MaybeAwaitable
99from .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
1415async 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
8086def 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
116125def 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