我正在将 Elixir 与 Phoenix 和 Absinthe 一起使用来设置 GraphQL 后端。
理想情况下,我希望有一个如下所示的结构:
{
posts {
published {
title
}
draft {
title
}
}
}
为此,我认为我需要将posts架构中的字段委托给响应publishedand的对象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
这不起作用,而是返回null整个posts字段。但是,如果我更改posts架构中的字段以包含“空白”解析器,它会按预期工作:
field :posts, type: :posts_by_state do
resolve fn _, _, _ -> {:ok, []} end
end
这是最佳实践还是有更好的方法来告诉字段完全委托给对象?更一般地说,有没有更好的方法来构建它?