custom.ex (4011B)
1 defmodule Absinthe.Type.Custom do 2 use Absinthe.Schema.Notation 3 4 @moduledoc """ 5 This module contains the following additional data types: 6 - datetime (UTC) 7 - naive_datetime 8 - date 9 - time 10 - decimal (only if [Decimal](https://hex.pm/packages/decimal) is available) 11 12 Further description of these types can be found in the source code. 13 14 To use: `import_types Absinthe.Type.Custom`. 15 """ 16 17 scalar :datetime, name: "DateTime" do 18 description """ 19 The `DateTime` scalar type represents a date and time in the UTC 20 timezone. The DateTime appears in a JSON response as an ISO8601 formatted 21 string, including UTC timezone ("Z"). The parsed date and time string will 22 be converted to UTC if there is an offset. 23 """ 24 25 serialize &DateTime.to_iso8601/1 26 parse &parse_datetime/1 27 end 28 29 scalar :naive_datetime, name: "NaiveDateTime" do 30 description """ 31 The `Naive DateTime` scalar type represents a naive date and time without 32 timezone. The DateTime appears in a JSON response as an ISO8601 formatted 33 string. 34 """ 35 36 serialize &NaiveDateTime.to_iso8601/1 37 parse &parse_naive_datetime/1 38 end 39 40 scalar :date do 41 description """ 42 The `Date` scalar type represents a date. The Date appears in a JSON 43 response as an ISO8601 formatted string, without a time component. 44 """ 45 46 serialize &Date.to_iso8601/1 47 parse &parse_date/1 48 end 49 50 scalar :time do 51 description """ 52 The `Time` scalar type represents a time. The Time appears in a JSON 53 response as an ISO8601 formatted string, without a date component. 54 """ 55 56 serialize &Time.to_iso8601/1 57 parse &parse_time/1 58 end 59 60 if Code.ensure_loaded?(Decimal) do 61 scalar :decimal do 62 description """ 63 The `Decimal` scalar type represents signed double-precision fractional 64 values parsed by the `Decimal` library. The Decimal appears in a JSON 65 response as a string to preserve precision. 66 """ 67 68 serialize &Absinthe.Type.Custom.Decimal.serialize/1 69 parse &Absinthe.Type.Custom.Decimal.parse/1 70 end 71 end 72 73 @spec parse_datetime(Absinthe.Blueprint.Input.String.t()) :: {:ok, DateTime.t()} | :error 74 @spec parse_datetime(Absinthe.Blueprint.Input.Null.t()) :: {:ok, nil} 75 defp parse_datetime(%Absinthe.Blueprint.Input.String{value: value}) do 76 case DateTime.from_iso8601(value) do 77 {:ok, datetime, _} -> {:ok, datetime} 78 _error -> :error 79 end 80 end 81 82 defp parse_datetime(%Absinthe.Blueprint.Input.Null{}) do 83 {:ok, nil} 84 end 85 86 defp parse_datetime(_) do 87 :error 88 end 89 90 @spec parse_naive_datetime(Absinthe.Blueprint.Input.String.t()) :: 91 {:ok, NaiveDateTime.t()} | :error 92 @spec parse_naive_datetime(Absinthe.Blueprint.Input.Null.t()) :: {:ok, nil} 93 defp parse_naive_datetime(%Absinthe.Blueprint.Input.String{value: value}) do 94 case NaiveDateTime.from_iso8601(value) do 95 {:ok, naive_datetime} -> {:ok, naive_datetime} 96 _error -> :error 97 end 98 end 99 100 defp parse_naive_datetime(%Absinthe.Blueprint.Input.Null{}) do 101 {:ok, nil} 102 end 103 104 defp parse_naive_datetime(_) do 105 :error 106 end 107 108 @spec parse_date(Absinthe.Blueprint.Input.String.t()) :: {:ok, Date.t()} | :error 109 @spec parse_date(Absinthe.Blueprint.Input.Null.t()) :: {:ok, nil} 110 defp parse_date(%Absinthe.Blueprint.Input.String{value: value}) do 111 case Date.from_iso8601(value) do 112 {:ok, date} -> {:ok, date} 113 _error -> :error 114 end 115 end 116 117 defp parse_date(%Absinthe.Blueprint.Input.Null{}) do 118 {:ok, nil} 119 end 120 121 defp parse_date(_) do 122 :error 123 end 124 125 @spec parse_time(Absinthe.Blueprint.Input.String.t()) :: {:ok, Time.t()} | :error 126 @spec parse_time(Absinthe.Blueprint.Input.Null.t()) :: {:ok, nil} 127 defp parse_time(%Absinthe.Blueprint.Input.String{value: value}) do 128 case Time.from_iso8601(value) do 129 {:ok, time} -> {:ok, time} 130 _error -> :error 131 end 132 end 133 134 defp parse_time(%Absinthe.Blueprint.Input.Null{}) do 135 {:ok, nil} 136 end 137 138 defp parse_time(_) do 139 :error 140 end 141 end