Skip to content
This repository has been archived by the owner on Nov 16, 2018. It is now read-only.

Mutation in graphene 2.0 #16

Open
sgaseretto opened this issue Mar 9, 2018 · 2 comments
Open

Mutation in graphene 2.0 #16

sgaseretto opened this issue Mar 9, 2018 · 2 comments

Comments

@sgaseretto
Copy link

The current mutation seems to not be working with graphene 2.0

    @staticmethod
    def mutate(root, args, context, info):
        if not context.user.is_authenticated():
            return CreateMessageMutation(status=403)
        message = args.get('message', '').strip()
        # Here we would usually use Django forms to validate the input
        if not message:
            return CreateMessageMutation(
                status=400,
                formErrors=json.dumps(
                    {'message': ['Please enter a message.']}))
        obj = models.Message.objects.create(
            user=context.user, message=message
        )
        return CreateMessageMutation(status=200, message=obj)

Since the version 2.0 now only receives as arguments for mutate(root, info, **args) where can we get now the context info so that context.user.is_authenticated() could work?

@tycomo
Copy link

tycomo commented Mar 11, 2018

You can get the context from info.

context = info.context
if not context.user.is_authenticated:

@nerdoc
Copy link

nerdoc commented Oct 20, 2018

A working mutation is:

    def mutate(self, info, **kwargs):
        if not info.context.user.is_authenticated:
            return CreateMessage(status=403)
        message = kwargs.get('message', '').strip()
        # Here we would usually use Django forms to validate the input
        if not message:
            return CreateMessage(
                status=400,
                formErrors=json.dumps(
                    {'message': ['Please enter a message.']}))
        obj = models.Message.objects.create(
            user=info.context.user, message=message
        )
        return CreateMessage(status=200, message=obj)

You have to change the tests then too...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants