Packages
absinthe_utils
0.2.0-1
0.3.0
0.2.0-1
0.1.0
0.0.8
0.0.7
0.0.6
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1-main-fc1bcd7a9a3344c3b816f69996b44e81c7df841e
0.0.1-main-f3f7019eeb29e81e58b52a1904179d5651d5ce47
0.0.1-main-da68e7b37af94f1d1ff6ba949de0bbc71944a428
0.0.1-main-d5a2fe6243598a29dec4ddb349d351661463aec6
0.0.1-main-d255e08b6c32fe6449757ed94e6abba8bb9a31e7
0.0.1-main-b324c3236f3971cfc05577e796dca31477a46391
0.0.1-main-96f126a7c33a43fc8af80891c168df27cb6fe738
0.0.1-main-8a629d261d43f2b462d4b7e6bbd32fee4871d54b
0.0.1-main-72f3aaa55e5beeeb85467282d2d97db0eb3f4f43
0.0.1-main-6cbd7212b9c6abb25241183c5764ec6b58af0cfd
0.0.1-main-413ca15b56c2636667dc5f1b7898f81da11032b0
0.0.1-main-2bcf8f0401b45d43a9c4592f2311f2e2cfd9309c
0.0.1-main-02ce6a58b060743c268bd76061f9b1ef0d90354a
0.0.1-development
Collection of utils for absinthe
Current section
Files
Jump to
Current section
Files
lib/scalars/strict_naive_datetime.ex
defmodule AbsintheUtils.Scalars.StrictNaiveDateTime do
@description """
The `StrictNaiveDateTime` scalar type represents a naive date and time without
timezone.
The output is an ISO8601 formatted string.
The input must be a naive datetime, without timezone offset.
Valid examples:
- 2020-01-01T00:00:00
- 2020-01-01 00:00:00
Invalid examples:
- 2020-01-01T00:00:00+00:00
- 2020-01-01T00:00:00+01:00
- 2020-01-01T00:00:00Z
- 2020-01-01 00:00:00Z
"""
@moduledoc """
#{@description}
**Usage:**
1. In your schema, import the type:
```
import_types(AbsintheUtils.Scalars.StrictNaiveDateTime)
```
2. Use the type in your schema:
```
field :my_field, :strict_naive_datetime
```
**Acknowledgements:**
Based on the type `naive_datetime` from `Absinthe.Type.Custom`.
"""
use Absinthe.Schema.Notation
scalar :strict_naive_datetime, name: "StrictNaiveDateTime" do
description(@description)
parse(&parse_naive_datetime/1)
serialize(fn value ->
case value do
%NaiveDateTime{} -> NaiveDateTime.to_iso8601(value)
_ -> raise Absinthe.SerializationError, "Invalid naive datetime value"
end
end)
end
@spec parse_naive_datetime(Absinthe.Blueprint.Input.String.t()) ::
{:ok, NaiveDateTime.t()} | :error
@spec parse_naive_datetime(Absinthe.Blueprint.Input.Null.t()) :: {:ok, nil}
defp parse_naive_datetime(%Absinthe.Blueprint.Input.String{value: value}) do
case DateTime.from_iso8601(value) do
{:error, :missing_offset} ->
case NaiveDateTime.from_iso8601(value) do
{:ok, naive_datetime} -> {:ok, naive_datetime}
_error -> {:error, "Invalid ISO8601 datetime"}
end
{:ok, _datetime, offset} when not is_nil(offset) ->
{
:error,
"Invalid ISO8601 datetime without timezone offset. Received an offset of #{offset}, expected no offset. " <>
"Example: 2020-01-01T00:00:00 instead of 2020-01-01T00:00:00Z. " <>
"For more information refer to the GraphQL description of this field type."
}
_ ->
{:error, "Invalid ISO8601 datetime without timezone offset"}
end
end
defp parse_naive_datetime(%Absinthe.Blueprint.Input.Null{}) do
{:ok, nil}
end
defp parse_naive_datetime(_) do
:error
end
end