PedroBern / django-graphql-auth

Django registration and authentication with GraphQL.
https://django-graphql-auth.readthedocs.io/en/latest/
MIT License
333 stars 105 forks source link

How to customize mutations response #127

Open Instrumedley opened 3 years ago

Instrumedley commented 3 years ago

Prerequisites

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?

Instrumedley commented 3 years ago

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)
Instrumedley commented 3 years ago

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

Genza999 commented 3 years ago

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)
Instrumedley commented 3 years ago

@Genza999 that's the same thing I tried