From c5612f93989d96cc5abca03c6dbd4eae8f030db4 Mon Sep 17 00:00:00 2001 From: Joao P Dubas Date: Thu, 24 Jun 2021 00:10:17 +0000 Subject: [PATCH] [day-03] create custom scalar for uuid Graphql doesn't support uuid natively. So, we must create a custom scalar to handle this type. --- lib/wabanex_web/schema/types/custom/uuid4.ex | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 lib/wabanex_web/schema/types/custom/uuid4.ex diff --git a/lib/wabanex_web/schema/types/custom/uuid4.ex b/lib/wabanex_web/schema/types/custom/uuid4.ex new file mode 100644 index 0000000..3c541fc --- /dev/null +++ b/lib/wabanex_web/schema/types/custom/uuid4.ex @@ -0,0 +1,32 @@ +defmodule WabanexWeb.Schema.Types.Custom.UUID4 do + use Absinthe.Schema.Notation + + alias Ecto.UUID + + scalar :uuid4, 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