Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
622 views
in Technique[技术] by (71.8m points)

graphql - Absinthe union type resolver providing an empty map to object field resolver

I'm trying to implement a GraphQL union type in Absinthe.

We have two objects, :foo and :bar and a union type :info that resolves to one of those types.

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

For some reason, each custom resolver is receiving an empty map %{} as its first argument.

When resolving the type, we do have a map with an "info" field (as well as more fields).

Why are the custom resolvers not receiving the same map that the union type resolver receives?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue was that I wasn't resolving the object's field properly.

This is correct:

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

This is incorrect:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...