Elixir Absinthe GraphQL, Union resolve_type list_of/1

507 views Asked by At

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?

1

There are 1 answers

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

    resolve(&CommunityResolver.search/3)
  end
end

:community_queries is a query argument and should be defined as a field instead of an object which looks up either a person or business, lets call them users. This defined field looks like it returns :search_result.

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

  resolve(&CommunityResolver.search/3)
end

You'll need to define what the search_result object looks like, which sounds like it's a list of users.

More about query arguments here