Skip to content

Instantly share code, notes, and snippets.

@mvmocanu

mvmocanu/foo.py Secret

Created August 12, 2019 07:18
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvmocanu/094679512dbc9c9cb2a7d07746d70bda to your computer and use it in GitHub Desktop.
Save mvmocanu/094679512dbc9c9cb2a7d07746d70bda to your computer and use it in GitHub Desktop.
# 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