Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to customize mutations response #127

Open
4 of 7 tasks
Instrumedley opened this issue Oct 13, 2021 · 4 comments
Open
4 of 7 tasks

How to customize mutations response #127

Instrumedley opened this issue Oct 13, 2021 · 4 comments

Comments

@Instrumedley
Copy link

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?

@Instrumedley
Copy link
Author

Instrumedley commented Oct 13, 2021

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 Instrumedley changed the title How to customize mutations How to customize mutations response Oct 13, 2021
@Instrumedley
Copy link
Author

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
Copy link

Genza999 commented Oct 22, 2021

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
Copy link
Author

@Genza999 that's the same thing I tried

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

No branches or pull requests

2 participants