1

我需要帮助。我正在寻找一种方法来解决联合中的 list_of(:object) 。这就是我所做的。

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

在上面的代码中。我试图将 list_of(:person) 放入 resolve_type 并返回这样的错误

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

然后我尝试了这个

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

它返回

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

我也试过把它放在这样的类型中,但也有错误。

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

有没有可能的解决方法?

4

1 回答 1

0
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是一个查询参数,应该被定义为一个字段而不是一个查找人或企业的对象,让我们称之为users。这个定义的字段看起来像返回:search_result

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

  resolve(&CommunityResolver.search/3)
end

您需要定义search_result对象的外观,听起来像是users.

更多关于查询参数的信息在这里

于 2021-05-06T21:07:23.150 回答