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

Passing the request_context to graphql #7

Closed
valentinradu opened this issue Feb 7, 2018 · 6 comments
Closed

Passing the request_context to graphql #7

valentinradu opened this issue Feb 7, 2018 · 6 comments

Comments

@valentinradu
Copy link

Not sure if this is intended or not but shouldn't the context be taken outside the message processing?
In base.py:

def execute(self, request_context, params):
        return graphql(
            self.schema, **dict(params, allow_subscriptions=True))

Should be:

self.schema, **dict(params, context_value= request_context, allow_subscriptions=True))

Otherwise, how can you pass the request_context? Does it make sense to be per message? Or is it intended for another use?

@colanconnon
Copy link
Contributor

@valentinradu Are you talking about the django channels implementation?

@valentinradu
Copy link
Author

Sorry for the late response. No, I'm using it with aiohttp, but this should apply to any implementations.

@Arfey
Copy link

Arfey commented Jun 3, 2018

So guys, we have a good pull request. what does we wait?

@colanconnon
Copy link
Contributor

@Arfey If you need this now I would just refer that branch directly in your requirements file.

@dfee
Copy link
Member

dfee commented Aug 14, 2018

In case anyone else comes by this and they don't feel like installing a commit and then falling off the upgrade path... here's a nicer crutch:

class ContextualAiohttpSubscriptionServer(AiohttpSubscriptionServer):
    def __init__(self, schema, keep_alive=True, loop=None):
        super().__init__(schema, keep_alive, loop)
        self._check_updated()

    @staticmethod
    def _check_updated():
        import pkg_resources
        import warnings
        from distutils.version import StrictVersion
        graphql_ws_dist = pkg_resources.get_distribution('graphql_ws')
        # https://github.com/graphql-python/graphql-ws/issues/7
        if StrictVersion(graphql_ws_dist.version) > '0.2.0':
            warnings.warn('`graphql_ws` is upgraded, check for `execute` fix')
            import sys; sys.exit() # optional, terminate immediately!

    def execute(self, request_context, params):
        # https://github.com/graphql-python/graphql-ws/issues/7
        params['context_value'] = request_context
        return super().execute(request_context, params)

It'll yell at you when graphql_ws gets upgraded past 0.2.0 so you can (maybe) upgrade then.

@SmileyChris
Copy link
Contributor

Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants