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

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement