PedroBern/django-graphql-auth

How to customize mutations response

Instrumedley opened this issue · 4 comments

Prerequisites

  • Is it a bug?
  • Is it a new feature?
  • Is it a a question?
  • Can you reproduce the problem?
  • Are you running the latest version?
  • Did you check for similar issues?
  • Did you perform a cursory search?

Description

Hi, I would like to customize the response of the Register mutation. I'd like to return the id of the recently created user, but I'm not finding any good example or tutorial anywhere on how can one achieve that. Is it possible?

Think I'm on the right track here. I created a custom mutation that inherits from Register, which allows me to use the register as I'd like to. The user is registered correctly and I can get the id now. Only thing missing is really how to overwrite the response with that extra parameter. That return did not do the trick. Pretty sure it's something simple, but I can't figure out, any help?

class RegisterCustom(mutations.Register):
    @classmethod
    def mutate(cls, *args, **kwargs):
        try:
            res = super().mutate(*args, **kwargs)
            if res.success:
                user = get_user_model().objects.order_by('-date_joined')[:1]

            return RegisterCustom(success=res.success,errors=res.errors,user_id=user[0]['id'])
        except Exception:
            raise Exception(res.errors)

it's a pity that development and support in this library has been completely abandoned, because the library is good and useful, just need a bit of customization and better docs here and there. With the current status of things I don't think people should adopt it in their project. Sure it might be a good helping hand in the beginning of your project but once it grows in scope and you need to change things you'll fall into a situation where you'll be forced to use something else

Hey, @Instrumedley Hope this helps, maybe you can try using this:

import graphene
from graphql_auth.schema import UserNode

class RegisterCustom(mutations.Register):
    user = graphene.Field(UserNode)

    @classmethod
    def mutate(cls, *args, **kwargs):
        try:
            res = super().mutate(*args, **kwargs)
            if res.success:
                user = get_user_model().objects.order_by('-date_joined')[:1]

            return RegisterCustom(success=res.success, user=user)
        except Exception:
            raise Exception(res.errors)

@Genza999 that's the same thing I tried