-
Notifications
You must be signed in to change notification settings - Fork 529
Scalar Recipes
HarshBalyan edited this page Feb 24, 2018
·
20 revisions
- UUID
@desc """
The UUID scalar type represents a version 4 (random) UUID. Any binary not conforming to this format will be flagged.
"""
defmodule MyApp.Schema.Types.Custom.UUID do
use Absinthe.Schema.Notation
alias Ecto.UUID
scalar :uuid, name: "UUID" do
serialize &UUID.cast!/1
parse &cast_uuid/1
end
defp cast_uuid(%Absinthe.Blueprint.Input.String{value: value}) do
UUID.cast(value)
end
defp cast_uuid(%Absinthe.Blueprint.Input.Null{}) do
{:ok, nil}
end
defp cast_uuid(_) do
:error
end
end
- JSON (using Jason)
defmodule MyApp.Schema.Types.Custom.JSON do
use Absinthe.Schema.Notation
scalar :json, name: "JSON" do
serialize &Jason.encode!/1
parse &decode/1
end
defp decode(%Absinthe.Blueprint.Input.String{value: value}) do
case Jason.decode(value) do
{:ok, decoded_json} -> {:ok, decoded_json}
_ -> :error
end
end
defp decode(%Absinthe.Blueprint.Input.Null{}) do
{:ok, nil}
end
defp decode(_) do
:error
end
end