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

Investigate ability to improve error messages #128

Open
EvgeniyEsaulkov opened this issue Jan 28, 2021 · 2 comments
Open

Investigate ability to improve error messages #128

EvgeniyEsaulkov opened this issue Jan 28, 2021 · 2 comments
Assignees
Labels

Comments

@EvgeniyEsaulkov
Copy link
Contributor

The goal is to make error messages more readable and clear for FE applications. It would be nice if the structure of error message will be displayed in the docs

@EvgeniyEsaulkov EvgeniyEsaulkov added API help wanted Extra attention is needed labels Jan 28, 2021
@EvgeniyEsaulkov EvgeniyEsaulkov self-assigned this Feb 27, 2021
@EvgeniyEsaulkov EvgeniyEsaulkov removed the help wanted Extra attention is needed label Mar 29, 2021
@EvgeniyEsaulkov
Copy link
Contributor Author

Dealing with errors in mutations

They occur due wrong user input and should provide useful feedback to the user. Solution was suggested by Dmitry Tsepelev in his article GraphQL on Rails: On the way to perfection.
We can define ValidationErrorType:

module Types
  class ValidationErrorsType < Types::BaseObject
    field :details, String, null: false
    field :full_messages, [String], null: false

    def details
      object.details.to_json
    end
  end
end

and customize type of returned object in mutation:

module Mutations
  class SignUp < BaseMutation
    argument :input, Types::SignUpInput, required: true

    field :authentication, Types::AuthenticationType, null: true
    field :errors, Types::ValidationErrorsType, null: true

    def resolve(input:)
      signup_user = SignupUser.call(input.to_h)

      if signup_user.success?
        { authentication: signup_user }
      else
        { errors: signup_user.user.errors }
      end
    end
  end
end

But in this case we can miss other types of errors such as authentication errors or 'wrong file type' (when we try to upload avatar image). So maybe it would be better to define common ErrorType:

module Types
  class ErrorType < Types::BaseObject
    field :message, String, null: false
  end
end

and use it like this:

module Mutations
  class SignUp < BaseMutation
    argument :input, Types::SignUpInput, required: true

    field :authentication, Types::AuthenticationType, null: true
    field :errors, [Types::ErrorType], null: true

    def resolve(input:)
      signup_user = SignupUser.call(input.to_h)

      if signup_user.success?
        { authentication: signup_user }
      else
        { errors: signup_user.error_data }
      end
    end
  end
end

@EvgeniyEsaulkov
Copy link
Contributor Author

After discussing with FE developers I think we don't need to implement this feature for now

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

No branches or pull requests

2 participants