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

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement