1

For example, I'd like to call the inserted_on timestamp created_at in the front end in the following Absinthe schema:

defmodule MyAppWeb.Schema.AccountTypes do
  use Absinthe.Schema.Notation

  object :user do
    field :id, :id
    field :email, :string
    field :inserted_on, :datetime
  end
end

but I'm not sure how to setup the Ecto <-> Absinthe mapping. Should I just add a virtual field to my Ecto schema?

Michael Johnston
  • 5,298
  • 1
  • 29
  • 37

1 Answers1

0

One option would be to use the :source option in Ecto schemas for database fields, so you can use your own internal name:

defmodule MyAppWeb.Schema.AccountTypes do
  use Absinthe.Schema.Notation

  object :user do
    field :id, :id
    field :email, :string
    field :created_at, :datetime, source: :inserted_on
  end
end

But the best option would probably be to set the proper field name in the query macro:

defmodule My.Schema do
  use Absinthe.Schema
  query do
    field :created_at, :string do
      resolve &MyResolver.inserted_on/3
  end
end

.. or use your own datatype instead of string..

raarts
  • 2,711
  • 4
  • 25
  • 45
  • I tried the first solution (of course, applied to my data structure), but got this error: ` ** (KeyError) key :source not found expanding struct:: Absinthe.Type.Field.__struct__/1` Is it possible that this `source` option was removed from Absinthe? – user2414208 Mar 19 '19 at 11:33
  • It's a relatively new feature, so maybe check your ecto version. It's decribed here: https://hexdocs.pm/ecto/Ecto.Schema.html#field/3 – raarts Mar 20 '19 at 12:13