Gulliver

Gulliver

How to use DateTime in Absinthe

Hi, what’s up everyone?

I’d be very grateful if someone wiser the me could open up this following error message a bit:

DateTime.to_iso8601/2 expects format to be :extended or :basic, got: :extended

I read it over and over again but my logic just fails me. It wants :extended but gets :extended. Pretty deep and almost poetic but not very helpful…

I’d really fancy some datetimes on my Absinthe type, like this:

import_types Absinthe.Type.Custom

  object :user do
    field :id, :id
    field :updated_at, :datetime
    field :inserted_at, :datetime
  end

I also tried to use my own scalar type instead of the import_types, like this:

  scalar :datetime do
    parse fn input ->
      case DateTime.from_iso8601(input.value) do
        {:ok, datetime} -> {:ok, datetime}
        _ -> :error
      end
    end

    serialize fn datetime ->
      DateTime.to_iso8601(datetime)
    end
  end

…but this resulted the very same error message.

So what am I doing wrong or is there a better way to do this?

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Ah no that isn’t, that’s a NaiveDateTime not a DateTime. It lacks timezone information.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Yeah that’s from the DateTime module itself, could probably be better:

iex(2)> NaiveDateTime.utc_now |> DateTime.to_iso8601
** (ArgumentError) DateTime.to_iso8601/2 expects format to be :extended or :basic, got: :extended
    (elixir) lib/calendar/datetime.ex:345: DateTime.to_iso8601/2

Gulliver

Gulliver

Right, actually I just figured it out as you were writing.

  DateTime.from_naive!(datetime, "Etc/UTC")
  |> DateTime.to_iso8601()

seems to do the trick!

Thank you!

OvermindDL1

OvermindDL1

Can’t you just do NaiveDateTime.to_iso8601(datetime)? (it has an optional second argument if you need it too).

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

We're in Beta

About us Mission Statement