idi527

idi527

How to get the error message from absinthe

Right now i get an unnecessary (to me) In field "signUp": in front of the actual message:

{
  "errors": [
    {
      "message": "In field \"signUp\": should be at least 3 character(s)",
      ...
      "key": "username"
    },
    {
      "message": "In field \"signUp\": should be at least 6 character(s)",
      ...
      "key": "password"
    }
  ]
  ...
}

But i would like to just get "should be at least 6 character(s)" in the "message" fields. Is this possible? Or is there some other way to access this message?

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I wonder if there is a shorter solution which doesn’t require defining a new type.

Unfortunately no not at the moment. The reason returning list_of(:valication_error) didn’t work is that list_of is really a type modifier as far as GraphQL is concerned, rather than a proper generic type of the form List<T>. Practically speaking I think the reason is that any given key that is requested should always be a list of a type or a regular type, but never sometimes one and sometimes the other.

We are looking to perhaps support some schema tools that would make this pattern less verbose and more first class, but at the moment I believe what you have here is the best for now.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey there! Your solution is definitely one approach.

By way of analogy, you can kind of think of GraphQL errors like exceptions in a regular programming language. They aren’t return values from the field, but rather field execution errors that bubble up to the top.

For errors like you’re trying to expose there’s another pattern that uses a union type to represent that a field may return validation errors. Here’s an example:


union :signup_result do
  types [:validation_error, :user]

  resolve_type fn
    %User{} -> :user
    _ -> :validation_error
  end
end

mutation do
  field :signup, :signup_result do
  end
end

And here’s how you’d use this in a document:

mutation SignUp {
  signUp {
    ... on ValidationError {
      __typename
      field
      message
    }
    ... on Success {
      # stuff
    }
  }
}

This is a lot like the difference between Elixir functions that return {:ok, result} | {:error, reason} and functions that simply return the result or raise.

idi527

idi527

I’ve replaced list_of(:validation_error) with :validation_errors

union :validation_result do
  types [:validation_errors, :session]

  resolve_type fn
    %{token: _, user: _}, _ ->
      :session
    _, _ ->
      :validation_errors
  end
end

added validation_errors type

object :validation_errors do
  field :errors, list_of(:validation_error)
end

and updated session.ex to return %{errors: ...} map

  def sign_in(_parent, %{username: username, password: password}, _info) do
    case verify(username, password) do
      {:ok, token, user} ->
        {:ok, %{token: token, user: user}}
      {:error, _msg} ->
        {:ok, %{errors: [%{message: "incorrect login credentials", field: :password}]}}
    end
  end

  def sign_up(_parent, user_params, _info) do
    case Store.Users.insert(user_params) do
      {:ok, user} ->
        token = Phoenix.Token.sign(Web.Endpoint, "user", user.id)
        {:ok, %{token: token, user: user}}
      {:error, changeset} ->
        {:ok, %{errors: translate(changeset.errors)}}
    end
  end

and now it works. The document I send now is

mutation SignUp($username: String!, $password: String!) {
  signUp(username: $username, password: $password) {
    ... on Session {
      token
      user {
        id
        username
      }
    }
    ... on ValidationErrors {
      errors {
        field
        message
      }
    }
  }
}

I wonder if there is a shorter solution which doesn’t require defining a new type.

idi527

idi527

I’ve decided to add an additional "error" field to put the error message in.

{
  "errors": [
    {
      "message": "In field \"signUp\": should be at least 3 character(s)",
      ...
      "key": "username",
      "error": "should be at least 3 character(s)"
    },
    {
      "message": "In field \"signUp\": should be at least 6 character(s)",
      ...
      "key": "password",
      "error": "should be at least 6 character(s)"
    }
  ]
  ...
}

Last Post!

rafbgarcia

rafbgarcia

That worked \o/ Thanks (for the fast answer too)!

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New

Other popular topics Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement