Absinthe union resolve_type list_of/1

Hi guys! I need help. I’m looking for a way to resolve a list_of(:object) in union.
Here’s what I did.

object :community_queries do
  field :search, type: :search_result do
    arg(:keyword, non_null(:string))

    resolve(&CommunityResolver.search/3)
  end
end

union :search_result do
  description "A search result"

  types [:person, :business]
  resolve_type fn
    %Person{}, _ -> list_of(:person)
    %Business{}, _ -> list_of(:business)
  end
end

In the code above. I tried to put a list_of(:person) in resolve_type and it returns error like this

invalid quoted expression: %Absinthe.Blueprint.TypeReference.List{errors: [], of_type: :person}

Then I tried this one

object :community_queries do
  field :search, type: list_of(:search_result) do
    arg(:keyword, non_null(:string))

    resolve(&CommunityResolver.search/3)
  end
end

union :search_result do
  description "A search result"

  types [:person, :business]
  resolve_type fn
    %Person{}, _ -> :person
    %Business{}, _ -> :business
  end
end

It returns

 (BadMapError) expected a map, got: [%{name: "John"}, ...

I’ve also tried putting it in types like this but also got errors.

...
types [list_of(:person), list_of(:business)]
  resolve_type fn
...

Is there a possible workaround for this?

@knvjun you should make the field :search a list eg:

field :search, list_of(:search_result)

Then your union is just:

union :search_result do
  description "A search result"

  types [:person, :business]
  resolve_type fn
    %Person{}, _ -> :person
    %Business{}, _ -> :business
  end
end
3 Likes

Thanks @benwilson512!
Another question, Is there a way to resolve a union type like this?

union :search_result do
  description "A search result"

  types [:person, :business]
  resolve_type fn
    %Person{}, _ -> :person,  resolve: &redact_some_info/2
    %Business{}, _ -> :business
  end
end

defp redact_some_info(_args, %{source: source}) do
  # redact ops.
end

@knvjun If you are looking to redact values you’ll need to do that in the resolver which returns a search_result, there is no way to attach a resolver directly to a union type.