Schema/Type of Ecto.embeds_one

How would one go about representing an Ecto.Schema.embeds_one() into an Absinthe schema/type ? All I get is ‘null’. ‘grades’ is the field I’m interested in.

Ecto schema’s:

schema "users_courses" do
    belongs_to(:user, User)
    belongs_to(:course, Course)

    embeds_one(:grades, Grades)


Grades schema:

  embedded_schema do
    field(:_1, :integer, default: 0)
    field(:_2, :integer, default: 0)
    field(:_3, :integer, default: 0)
    field(:_4, :integer, default: 0)
    field(:_5, :integer, default: 0)
    field(:_6, :integer, default: 0)
    field(:_7, :integer, default: 0)
    field(:_8, :integer, default: 0)
    field(:_9, :integer, default: 0)
    field(:_10, :integer, default: 0)
    field(:_11, :integer, default: 0)
    field(:_12, :integer, default: 0)
    field(:_13, :integer, default: 0)
    field(:_14, :integer, default: 0)


  object :user_course do
    field(:user, :user, resolve: assoc(:user))
    field(:course, :course, resolve: assoc(:course))
    field(:grades, :grades)
  object :grades do
    field(:_1, :integer)
    field(:_2, :integer)
    field(:_3, :integer)
    field(:_4, :integer)
    field(:_5, :integer)
    field(:_6, :integer)
    field(:_7, :integer)
    field(:_8, :integer)
    field(:_9, :integer)
    field(:_10, :integer)
    field(:_11, :integer)
    field(:_12, :integer)
    field(:_13, :integer)
    field(:_14, :integer)

This doesn’t work. I get ‘null’ for the ‘grades’ field.

All the other fields are resolving fine as expected, but not ‘grades’.

Any ideas ? Thanks!

Actually the above works just fine. The issue was that when I was testing I had just a handful of records that actually had something in that ‘jsonb’ field, so I was looking and making assumptions based on wrong data :roll_eyes:

Anyway, in the mean time I’ve given up on such a verbose definition for :grades and instead started using a scalar type :json (found on this forum).

So, now I have:

field(:grades, :json)
  scalar :json, description: "JSON field type in postgres" do
    parse(fn input ->
      case Poison.decode(input) do
        {:ok, result} ->
          {:ok, result}

        _ ->

1 Like