Absinthe object type for a map with number keys and map value

I have data that looks like:

%{
  1 => %{display: 1, selected: "some string" },
  2 => %{display: 2, selected: "some string" },
  ...,
  25 => %{display: 25, selected: "some string" },
}

Where the key is a number and the value is a map of the form %{display: :integer, selected: :string }.
So for the query, I’ll do something like:

object :the_qury_object do
    field :something, :number_key_map_value_type do
      resolve(fn _, _ ->
        {
          :ok,
          1..50
          |> Enum.map(&{&1, %{display: &1, selected: "something"}})
          |> Enum.into(%{})
        }
      end)
    end
  end

Is there a way I can add an object type for such data?

:wave:

I think you’d need to reshape

%{
  1 => %{display: 1, selected: "some string" },
  2 => %{display: 2, selected: "some string" },
  ...,
  25 => %{display: 25, selected: "some string" },
}

into something like

[
  %{id: 1, value: %{display: 1, selected: "some string" }}
  %{id: 2, value: %{display: 2, selected: "some string" }}
}

and then treat it as a list of some object types.

object :inner_object_type do
  field :display, :integer
  field :selected, :string
end

object :outer_object_type do
  field :id, :integer # or maybe :id?
  field :value, :inner_object_type # or maybe an interface / union?
end

object :the_qury_object do
  field :something, list_of(:number_key_map_value_type) do
    # ...
2 Likes

Thanks @idi527. I’d really love to resolve to a map instead of list, so that in javascript on the client I can do a simple look up data["1"]; data["2"]; //etc. rather than walk the array.

But if there is no other way, I think I will go with your suggestion

I think in javascript a map (object) with numeric keys and an array are the same thing so the lookups should also be possible.