Skip to content
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

Context passed to subscriptions is None #46

Closed
jonypawks opened this issue Sep 4, 2019 · 3 comments · Fixed by #47
Closed

Context passed to subscriptions is None #46

jonypawks opened this issue Sep 4, 2019 · 3 comments · Fixed by #47
Assignees
Labels
bug Something isn't working

Comments

@jonypawks
Copy link

When using subscriptions, both the resolver and the subscription handler receive None as the context, regardless of what is passed in to register_graphql_handlers.

I was expecting that context to be the same context (though a unique instance as for all requests) that is received by query and mutation resolvers. I wondered how the examples worked but then noticed that it just uses a global instead of passing the redis connection through the context.

I noticed that for subscriptions the context is being pulled from the websocket message payload, which seems odd to me but maybe I don't fully understand:

Here's an small example app that demos the behavior I'm seeing:

import asyncio
import sys

from aiohttp import web

from tartiflette import Resolver, Subscription
from tartiflette_aiohttp import register_graphql_handlers


SDL = """
type Query {
    countdownLength: Int!
}

type Subscription {
    countdown: Int!
}
"""


@Resolver("Query.countdownLength")
async def resolver_length(_parent, _args, ctx, _info):
    return ctx["length"]


@Subscription("Subscription.countdown")
async def on_countdown(parent, _args, ctx, _info):
    print(f"subscription: ctx={ctx}")
    countdown = 5  # would expect to use ctx["length"] here
    while countdown > 0:
        yield countdown
        countdown -= 1
        await asyncio.sleep(1)
    yield 0


@Resolver("Subscription.countdown")
async def resolver_countdown(payload, _args, ctx, _info):
    print(f"resolver: payload={payload} ctx={ctx}")
    return payload


def main():
    app = web.Application()
    register_graphql_handlers(
        app,
        engine_sdl=SDL,
        executor_context={"length": 5},
        subscription_ws_endpoint="/ws",
        graphiql_enabled=True,
    )
    web.run_app(app, port=8089)
    return 0


if __name__ == "__main__":
    sys.exit(main())

Which produces output:

======== Running on http://0.0.0.0:8089 ========
(Press CTRL+C to quit)
subscription: ctx=None
resolver: payload=5 ctx=None
resolver: payload=4 ctx=None
resolver: payload=3 ctx=None
resolver: payload=2 ctx=None
resolver: payload=1 ctx=None
resolver: payload=0 ctx=None
@Maximilien-R
Copy link
Member

Maximilien-R commented Sep 4, 2019

Hi @jonypawks,

Thank you for raising this issue, indeed, the context isn't properly injected in the case of subscriptions.

Getting the context from the payload is indeed an error on our side.

A fix has been made on this PR.

You can test that this corrects your issue by installing the version linked to the PR with:

pip install --extra-index-url https://test.pypi.org/simple/ tartiflette-aiohttp==0.8.5a1567583033

@Maximilien-R Maximilien-R self-assigned this Sep 4, 2019
@Maximilien-R Maximilien-R added the bug Something isn't working label Sep 4, 2019
@jonypawks
Copy link
Author

Thanks @Maximilien-R, installing the PR version does indeed fix the issue for me.

@Maximilien-R
Copy link
Member

@jonypawks The fix is available in the tartiflette-aiohttp==0.8.5 release.

Again thank you for raising this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants