AshGraphql Errors Encountered During Ash 3.0 Migration

Hi I’ve recently been working on an Ash 3.0 migration.

== Compilation error in file lib/dentallog_web/schema.ex ==
** (Protocol.UndefinedError) protocol Enumerable not implemented for nil of type Atom
    (elixir 1.16.1) lib/enum.ex:1: Enumerable.impl_for!/1
    (elixir 1.16.1) lib/enum.ex:166: Enumerable.reduce/3
    (elixir 1.16.1) lib/enum.ex:4399: Enum.reduce/3
    (ash_graphql 1.0.0-rc.3) lib/resource/resource.ex:2785: anonymous fn/5 in AshGraphql.Resource.define_map_types/5
    (elixir 1.16.1) lib/enum.ex:4329: Enum.flat_map_list/2
    (ash_graphql 1.0.0-rc.3) lib/resource/resource.ex:2766: anonymous fn/4 in AshGraphql.Resource.map_definitions/3
    (elixir 1.16.1) lib/enum.ex:4329: Enum.flat_map_list/2
    (ash_graphql 1.0.0-rc.3) lib/resource/resource.ex:1685: AshGraphql.Resource.type_definitions/4

However, during the migration, I get the above error.
I think it’s probably related to Ash.Type.NewType.

Here are the types I’m using

types.ex

defmodule Dentallog.DentalDiary.Types.GroupByDateDiary do
  use Ash.Type.NewType,
    subtype_of: :map

  use AshGraphql.Type

  @impl AshGraphql.Type
  def graphql_type(_), do: :group_by_date_diary
end

schema.ex

defmodule DentallogWeb.Schema do
  use Absinthe.Schema

  @domains [
    Dentallog.Accounts,
    Dentallog.DentalDiary,
    Dentallog.QuestionAnswer,
    Dentallog.DentalLab
  ]

  use AshGraphql, domains: @domains

  query do
  end

  mutation do
  end

  object :group_by_date_diary do
    field :visited_date, :string
    field :diaries, list_of(:dental_diary)
  end

  object :cancel_thumb_up_response do
    field :count_of_thumb_up, :integer
  end
end

I saw the above documentation and realised that it no longer supports the :map type.

defmodule Dentallog.DentalDiary.Types.GroupByDateDiary do
  use Ash.Type.NewType,
    subtype_of: :union, constraints: [
      types: [
        visited_date: [
          type: :string
        ],
        diaries: [
          type: {:array, :dental_diary}
        ]
      ],

    ]

  use AshGraphql.Type

  @impl AshGraphql.Type
  def graphql_type(_), do: :group_by_date_diary
end

Actually, I still don’t know, and it would be great if you could shed some light on this.

Hmm…so there should be no issue with your map type as you have it :thinking:. I think this is a bug in our map type processing. I’ve pushed up what should be a simple fix to main of ash_graphql. Can you try it out and let me know if it helps?

1 Like

I think the Upgrade Guide is Just missing the example for :maps and the errors is because you have not defined any constraints on the new type

In this case they are overriding the type with a type they created…which is actually kind of interesting as it would break as soon as they add the fields constraint…I think I’lll need to add a define_type?/1 callback that defaults to true to control this behavior.

mix.exs

      {:ash_graphql, git: "https://github.com/ash-project/ash_graphql.git"},

After making the above changes, the associated error is no longer called.
Thank you!
:saluting_face:

Excellent. I’ve also added the ability to handle the scenario I was mentioning above, where you don’t want a type to be derived automatically for you in certain cases (you won’t need this, because we only define a type for a map if it has the :fields constraint, but putting it here for others)

@impl true
def graphql_define_type?(_), do: false
1 Like