-
-
Save mvmocanu/094679512dbc9c9cb2a7d07746d70bda to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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