【问题标题】:Absinthe union type resolver providing an empty map to object field resolver苦艾酒联合类型解析器为对象字段解析器提供空映射
【发布时间】:2021-04-07 02:30:13
【问题描述】:

我正在尝试在 Absinthe 中实现 GraphQL 联合类型。

我们有两个对象,:foo:bar 和一个联合类型 :info,它解析为其中一种类型。

object :contact do
  field :info, list_of(:info)
end

object :foo do
  field :phone, non_null(:string),
    resolve: fn foo, _ ->
      IO.inspect(foo) # <-- is %{}
      {:ok, "hardcodedfornow"}
    end
end

object :bar do
  field :id, non_null(:string),
    resolve: fn id, _b ->
      IO.inspect(id) # <-- is %{}
      {:ok, "hardcodedfornow"}
    end
end

union :info do
  description("")

  types([:foo, :bar])

  resolve_type(fn
    %{"info" => "foo"}, _ ->
      :foo

    %{"info" => "bar"}, _ ->
      :bar
  end)
end

由于某种原因,每个自定义解析器都接收一个空映射 %{} 作为其第一个参数。

在解析类型时,我们确实有一个带有 "info" 字段(以及更多字段)的映射。

为什么自定义解析器没有收到与联合类型解析器相同的映射?

【问题讨论】:

    标签: graphql elixir absinthe


    【解决方案1】:

    问题是我没有正确解析对象的字段。

    这是正确的:

    object :foo do
      field :phone, non_null(:string) do
        resolve(fn foo, _, _ ->
          {:ok, foo["phone"]}
        end)
      end
    end
    

    这是不正确的:

    object :foo do
      field :phone, non_null(:string),
        resolve: fn foo, _ ->
          {:ok, foo["phone"]}
        end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-08
      • 2020-03-30
      • 2018-02-28
      • 2021-01-13
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      相关资源
      最近更新 更多