Skip to content

Instantly share code, notes, and snippets.

@mvmocanu

mvmocanu/foo.py Secret

Created Aug 12, 2019
Embed
What would you like to do?
# context.py
from django.utils.functional import cached_property
from gql.loaders import CommentsByArticleIdLoader
class GQLContext:
def __init__(self, request):
self.request = request
self.loaders = {
'comments_by_article_id_loader': CommentsByArticleIdLoader(),
}
def __call__(self):
for method_, instance in self.loaders.items():
setattr(self.request, method_, instance)
return self.request
# views.py
from graphene_django.views import GraphQLView
from gql.context import GQLContext
class CustomGraphQLView(GraphQLView):
def get_context(self, request):
return GQLContext(request)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment