在Absinthe的resolver中,我们可以手动将返回值设为一个空数组,而不是由Absinthe自动将空数组转换为包含一个null值的数组。例如:
defmodule MyApp.Schema.Resolvers do
use Absinthe.Resolver
# ...
field :example_field, [String], null: true
# ...
def resolve_example_field(_parent, _args, _ctx) do
# some logic to retrieve strings
strings = retrieve_strings()
if Enum.count(strings) == 0 do
[]
else
strings
end
end
# ...
end
在上面的例子中,我们在resolver中检查要返回的数组是否为空,如果是空的,我们就手动将其设为一个空数组。这样,即使Absinthe在转换时将空数组转换为包含一个null值的数组,我们也可以确保返回一个真正的空数组。