PedroBern / django-graphql-auth

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

Customize error output #34

Closed boolangery closed 4 years ago

boolangery commented 4 years ago

Hi, This is just a question, is there an easy way to customize error output of mutations ? I would like it to be the same as the rest of my API Because I tried quickly with inheritance and I got MRO errors. Thanks !

PedroBern commented 4 years ago

Hi @boolangery, you mean for the form errors, or for graphQL located errors? Neither is customizable right now. Give me an example, maybe we can work a PR.

boolangery commented 4 years ago

Yes I mean for GraphQL form errors. Right now its the same format as Django Form.errors. I need something more like this (to integrate it perfectly with the rest of my API):

type InputError {
  key: String!
  message: String!
}

type InputErrors {
  inputs: [InputError]
  nonFieldErrors: [String]
}

type RegisterPayload {
  errors: InputErrors
  clientMutationId: String
}

And here is how I managed to get the output I want:

# wrap graphql_auth.Register to get standard error output
class Register(relay.ClientIDMutation):
    success = graphene.Boolean()
    errors = graphene.Field(InputErrors)

    class Input:
        email = graphene.String(required=True)
        password1 = graphene.String(required=True)
        password2 = graphene.String(required=True)
        firstName = graphene.String(required=True)
        lastName = graphene.String(required=True)
        birthday = graphene.Date(required=True)
        isCoach = graphene.Boolean(required=True)

    @classmethod
    def mutate_and_get_payload(cls, root, info, **args):
        res = graphql_auth.Register.resolve_mutation(root, info, **args)
        return cls(success=res.success, errors=django_graphql_auth_errors_to_validation_errors(res.errors))

Tell me if you see a better way to do it :)

PedroBern commented 4 years ago

Unfortunately for you, there is no easy way for this, but I think you manage to make it the best way possible.

Because I tried quickly with inheritance and I got MRO errors.

Maybe if you inherit from the mixins instead of relay/mutations, would not get MRO, but I don't know and would give you a lot of work.

PR proposal

Maybe you can make PR, if you are willing to try to make it work on an easy way for you, make a PR just providing a setting that accepts a string path for the ExpectedErrorType class.

I don't know the implications on the rest of the package right now, so I don't if it will work, but maybe it works simply like this.

boolangery commented 4 years ago

Ok thank you, I'll keep this in my mind but I don't have the time for it right now :)

124bit commented 4 years ago

I had the same request in my project. I need to rename the 'errors' field and to reformat errors. I haven't found the easy way to patch/monkeypatch, so I just skipped it.

PedroBern commented 4 years ago

Closed by #65