-
-
Notifications
You must be signed in to change notification settings - Fork 856
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
Unable to access info.context['background'] #957
Comments
Do you by chance have an example how this would look with Ariadne? Or how you would expect it to function? I'm not particularly familiar with it and I imagine that might be the case for others. |
@bartek I don't see this being any different than with Graphene. If during the resolver of a mutation we want to execute a task in the background and return something, I'd like to be able to: For example if I'm trying to send an authentication email it could look roughly like this: @mutation.field("authenticateEmail")
async def send_authentication_email(user, obj, info):
email = user["email"]
tasks = BackgroundTasks()
tasks.add_task(mail("authenticate"))
return True
async def mail(type: string):
if type === "authenticate":
send_the_email() |
Got the first part thanks to Benwis on spectrum.
However, could you help with:
When calling |
Solved Middleware: class BackgroundTaskMiddleware(BaseHTTPMiddleware):
async def dispatch(
self, request: Request, call_next: RequestResponseEndpoint
) -> Response:
request.state.background = None
response = await call_next(request)
if request.state.background:
response.background = request.state.background
return response Resolver: @query.field("getUser")
@check_authentication
async def resolve_get_user(user, obj, info):
task = BackgroundTasks()
task.add_task(test_func)
task.add_task(testing_func_two, "I work now")
request = info.context["request"]
request.state.background = task
return True
async def test_func():
await asyncio.sleep(10)
print("once!!")
async def testing_func_two(message: str):
print(message) The functions still execute synchronously, but because they're background tasks I'm not too worried. |
I'm hoping to avoid any use of Celery at the moment. In the docs there are two ways to add background tasks:
Via Graphene: https://www.starlette.io/graphql/
Via a JSON response: https://www.starlette.io/background/
Does anyone know of a way to add tasks to Starlette's background with Ariadne? I am unable to return a JSONResponse in my resolver, and I do not have access to a info.context["background"]. The only thing I have attached to my context is my request object.
The text was updated successfully, but these errors were encountered: