GraphQL-python-archive / graphql-django-view

[DEPRECATED | Use graphene-django package] A django view that will execute a GraphQLSchema using a given Executor.
http://graphene-python.org/
MIT License
23 stars 7 forks source link

Cannot access request within mutation #2

Closed adamcharnock closed 8 years ago

adamcharnock commented 8 years ago

I am creating a new model within a mutation. I would like to set the model's created_by field to point to the current user (using request.user). However, AFAIK the request object is not available within a mutation's mutate_and_get_payload() method.

Note: It is entirely possible that I'm going about this the wrong way, I'm still new to GraphQL.

jhgg commented 8 years ago

One way to do this is to subclass GraphQLView and override get_root_value to return request.user. Which would put it into info.root_value in your resolver.

I plan on making the entire request object available in info though. Should land later today.

jhgg commented 8 years ago
class CurrentUserRootValueGraphQLView(GraphQLView):
    def get_root_value(self, request):
        return request.user

graphql_view = csrf_exempt(CurrentUserRootValueGraphQLView.as_view(
    schema=schema
))
adamcharnock commented 8 years ago

That is brilliant, thanks very much!