11import json
22
33from functools import partial
4- from inspect import iscoroutine
5- from typing import Any , Dict , List , Optional , Union
4+ from inspect import iscoroutine , iscoroutinefunction
5+ from typing import Any , Callable , Dict , List , Optional , Union
66
77from tartiflette import Engine
8+ from tartiflette_aiohttp ._context_factory import default_context_factory
89from tartiflette_aiohttp ._graphiql import graphiql_handler
910from tartiflette_aiohttp ._handler import Handlers
1011from tartiflette_aiohttp ._subscription_ws_handler import (
@@ -35,15 +36,15 @@ def validate_and_compute_graphiql_option(
3536def _set_subscription_ws_handler (
3637 app : "Application" ,
3738 subscription_ws_endpoint : Optional [str ],
38- context : Dict [ str , Any ] ,
39+ context_factory : Callable ,
3940) -> None :
4041 if not subscription_ws_endpoint :
4142 return
4243
4344 app .router .add_route (
4445 "GET" ,
4546 subscription_ws_endpoint ,
46- AIOHTTPSubscriptionHandler (app , context ),
47+ AIOHTTPSubscriptionHandler (app , context_factory ),
4748 )
4849
4950
@@ -116,6 +117,7 @@ def register_graphql_handlers(
116117 engine_modules : Optional [
117118 List [Union [str , Dict [str , Union [str , Dict [str , str ]]]]]
118119 ] = None ,
120+ context_factory : Optional [Callable ] = None ,
119121) -> "Application" :
120122 """Register a Tartiflette Engine to an app
121123
@@ -133,10 +135,12 @@ def register_graphql_handlers(
133135 graphiql_enabled {bool} -- Determines whether or not we should handle a GraphiQL endpoint (default: {False})
134136 graphiql_options {dict} -- Customization options for the GraphiQL instance (default: {None})
135137 engine_modules: {Optional[List[Union[str, Dict[str, Union[str, Dict[str, str]]]]]]} -- Module to import (default:{None})
138+ context_factory: {Optional[Callable]} -- coroutine function in charge of generating the context for each request (default: {None})
136139
137140 Raises:
138141 Exception -- On bad sdl/engine parameter combinaison.
139142 Exception -- On unsupported HTTP Method.
143+ Exception -- if `context_factory` is filled in without a coroutine function.
140144
141145 Return:
142146 The app object.
@@ -150,6 +154,16 @@ def register_graphql_handlers(
150154 if not executor_http_methods :
151155 executor_http_methods = ["GET" , "POST" ]
152156
157+ if context_factory is None :
158+ context_factory = default_context_factory
159+
160+ if not iscoroutinefunction (context_factory ):
161+ raise Exception (
162+ "`context_factory` parameter should be a coroutine function."
163+ )
164+
165+ context_factory = partial (context_factory , executor_context )
166+
153167 if not engine :
154168 engine = Engine ()
155169
@@ -174,14 +188,14 @@ def register_graphql_handlers(
174188 executor_http_endpoint ,
175189 partial (
176190 getattr (Handlers , "handle_%s" % method .lower ()),
177- executor_context ,
191+ context_factory = context_factory ,
178192 ),
179193 )
180194 except AttributeError :
181195 raise Exception ("Unsupported < %s > http method" % method )
182196
183197 _set_subscription_ws_handler (
184- app , subscription_ws_endpoint , executor_context
198+ app , subscription_ws_endpoint , context_factory
185199 )
186200
187201 _set_graphiql_handler (
0 commit comments