I'm using django-graphql-auth and graphene on my django project. The django-graphql-auth library is nice but it lacks a bit on documentation and examples of customization. I already sent this question there but repo does not seem to get much activity lately (time to replace and use another package in the project perhaps), so I'll try my luck here:
most of the questions related to customization are regarding inputs. In my case I'd like to change the output of the Register mutation, as I need the id of the user created on the response.
So far what I manage to accomplish was to create a new custom mutation that inherits register, so I can add the logic to grab the created user:
class RegisterCustom(mutations.Register):
response = graphene.Field(RegisterResponseType)
@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=True, error="", user_id=user[0]['id'])
except Exception:
raise Exception(res.errors)
However the return is not what I expect
{
"errors": [
{
"message": "None",
"locations": [
{
"line": 472,
"column": 5
}
],
"path": [
"registerCustom"
]
}
],
"data": {
"registerCustom": null
}
}
if I add a print right before return let's say print(user[0]['id']) I can see the user id being printed. My guess is that in this line: res = super().mutate(*args, **kwargs) , by calling the mutate method on Register I'm already doomed to get the response from the parent method, no matter what I do next.
I also tried a different approach. This one, based on an example from graphene docs :
class RegisterCustom(mutations.Register):
Output = RegisterResponseType
@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 RegisterResponseType(sucess=True, error="", user_id=user[0]['id'])
except Exception:
raise Exception(res.errors)
Also tried this way, no success
class RegisterCustom(mutations.Register):
response = graphene.Field(RegisterResponseType)
@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]
response = {'success': True, 'error': '', 'user_id': user[0]['id']}
return RegisterCustom(response=response)
except Exception:
raise Exception(res.errors)
Ideally I want to solve this with django-graphql-auth, as I already have many functionalities done with it, be it a good solution or a hack I don't care since I won't be facing this scenario too often. I don't wanna replace my entire authentication logic at this point. My guess is that it's a fairly simple thing to do, but the lack of documentation is making this hard