【发布时间】:2019-04-27 15:10:40
【问题描述】:
注意:这是一个大大简化的例子,但问题是一样的。
我正在尝试使用 GraphQL 接口包装现有的 HTTP 服务 /blog-posts。仅当我传入查询参数extra-data=true 时,服务才会在其响应中返回一些额外数据。所以,
-
GET /blog-posts: 获取 ID 和标题 -
GET /blog-posts?extra-data=true:获取 ID、标题和extra-data字段
我有一个 Absinthe 架构,类似于以下内容:
query do
field :blog_posts, non_null(list_of(non_null(:blog_post)))
resolve &MyAppWeb.Resolvers.Blog.posts/3
end
object :blog_post do
field :id, non_null(:id)
field :title, non_null(:string)
field :extra_data, :string,
resolve: &MyAppWeb.Resolvers.Blog.post_extra_data/3
end
我的问题是我不知道如何实现extra_data 解析器,这样它就不会在已经调用/blog-posts 之后对/blog-posts?extra-data=true 进行冗余调用。有一个中间件 https://hexdocs.pm/absinthe/Absinthe.Middleware.Batch.html 旨在帮助解决类似的问题,N+1 查询,但我不知道如何在我的案例中应用它。
有什么建议吗?
【问题讨论】: