【发布时间】:2019-09-28 14:48:43
【问题描述】:
我正在将 Elixir 与 Phoenix 和 Absinthe 一起使用来设置 GraphQL 后端。
理想情况下,我希望有一个如下所示的结构:
{
posts {
published {
title
}
draft {
title
}
}
}
为此,我认为我需要将架构中的posts 字段委托给响应published 和draft 的对象。我是这样做的:
# In my Schema
field :posts, type: :posts_by_state
# In my Post type definitions
object :post do
# ...
end
object :posts_by_state do
field :published, list_of(:post) do
resolve fn _, _, _ -> ... end
end
field :draft, list_of(:post) do
resolve fn _, _, _ -> ... end
end
end
这不起作用,而是为整个 posts 字段返回 null。但是,如果我将架构中的 posts 字段更改为包含“空白”解析器,它会按预期工作:
field :posts, type: :posts_by_state do
resolve fn _, _, _ -> {:ok, []} end
end
这是最佳实践还是有更好的方法来告诉字段完全委托给对象?更一般地说,有没有更好的方法来构建它?
【问题讨论】:
标签: elixir graphql phoenix-framework absinthe