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

Move context value creation to a user-overridable get_context method #104

Open
gabubarks opened this issue Jun 23, 2023 · 1 comment
Open

Comments

@gabubarks
Copy link

Would it be possible to refactor this:

# Prepare a context object.
context = DictAsObject({})
context.channels_scope = self.scope
context.channel_name = self.channel_name
context.graphql_operation_name = op_name
context.graphql_operation_id = op_id

into its own get_context method, which a user could override in a custom subclass of the GraphqlWsConsumer if they want to inject something into the context?

Currently I'm injecting my own fields by overriding _on_gql_start__subscribe and injecting the fields before passing it on to the super function, which seemed to be the least impactful place to do it. But I just found out that queries and mutations don't pass through that function, meaning I would have to copy-paste the entire _on_gql_start method from the source into my override class just to add my own fields to the context...

A get_context method would make this trivial and very user friendly. It would also mimic how graphene-django provides this functionality to its users:

https://github.com/graphql-python/graphene-django/blob/e950164c8ee2a9babf54a0c2d3da27194d7cfddc/graphene_django/views.py#L139-L140

@cpd67
Copy link

cpd67 commented Aug 14, 2023

+1 on this. Without a way to inject something into the context, I was forced to override _on_gql_start() in order to inject what I needed. I feel like having a get_context() method would be a great solution.

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

2 participants