Absinthe: Custom scalar and validation crash instead of GraphQL error output

Created on 5 Feb 2021  路  2Comments  路  Source: absinthe-graphql/absinthe

Environment

  • Elixir version (elixir -v): Elixir 1.11.3
  • Absinthe version absinthe: absinthe 1.6.1 (Hex package) / absinthe_plug 1.5.4
  • Client Framework and version (Relay, Apollo, etc): GraphiQL

Expected behavior

A serialized GraphQL error when having a type mismatch when sending an argument from a custom scalar

Actual behavior

I've got this custom Scalar (UUID)

defmodule Bigseat.Schema.Scalars.Uuid do
  use Absinthe.Schema.Notation

  alias Ecto.UUID

  scalar :uuid, name: "UUID4" do
    description("""
    The `UUID4` scalar type represents UUID4 compliant string data, represented as UTF-8
    character sequences. The UUID4 type is most often used to represent unique
    human-readable ID strings.
    """)

    serialize(&encode/1)
    parse(&decode/1)
  end

  @spec decode(Absinthe.Blueprint.Input.String.t()) :: {:ok, term()} | :error
  @spec decode(Absinthe.Blueprint.Input.Null.t()) :: {:ok, nil}
  defp decode(%Absinthe.Blueprint.Input.String{value: value}) do
    UUID.cast(value)
  end

  defp decode(%Absinthe.Blueprint.Input.Null{}) do
    {:ok, nil}
  end

  defp decode(_) do
    {:error}
  end

  defp encode(value), do: value
end

I use it on a query space and when I send a valid UUID such as abe0b120-9d23-44f1-8046-ce485940a42d it passes, but when I send something like 1 on purpose it completely crashes instead of validating in a clean and readable way what's happened.

Screen Shot 2021-02-05 at 11 22 20 PM

It goes through {:error} as intended, but why isn't it showing an error with the GraphQL format?

Stacktrace

# CaseClauseError at POST /graphql\n\nException:\n\n    ** (CaseClauseError) no case clause matching: {:error}\n        (absinthe 1.6.1) lib/absinthe/phase/document/arguments/parse.ex:44: Absinthe.Phase.Document.Arguments.Parse.build_value/3\n        (absinthe 1.6.1) lib/absinthe/phase/document/arguments/parse.ex:24: Absinthe.Phase.Document.Arguments.Parse.handle_node/2\n        (absinthe 1.6.1) lib/absinthe/blueprint/transform.ex:16: anonymous fn/3 in 

Most helpful comment

Hi @Loschcode, the correct value to return from a scalar is :error, not {:error}. I would also make sure that UUID.cast(value) returns :error in the event of a failed cast and not something like {:error, reason}.

All 2 comments

Hi @Loschcode, the correct value to return from a scalar is :error, not {:error}. I would also make sure that UUID.cast(value) returns :error in the event of a failed cast and not something like {:error, reason}.

Ah stupid mistake! Hope it helps people making it too, thanks for the quick response 馃憤

Was this page helpful?
0 / 5 - 0 ratings