View Source JetExt.Absinthe.OneOf.Phase (jet_ext v0.2.2)
The homemade one_of
phase for Absinthe.
使用方法
在 Router 中添加如下代码:
defmodule MyApp.Router do
use Phoenix.Router
# ...
scope "/api" do
pipe_through :api
forward "/admin/explorer", Absinthe.Plug.GraphiQL,
interface: :playground,
schema: MyApp.GraphQL.Schema.Admin,
# 添加下面这一行
pipeline: {__MODULE__, :pipeline}
forward "/admin", Absinthe.Plug,
json_codec: Jason,
schema: MyApp.GraphQL.Schema.Admin,
# 添加下面这一行
pipeline: {__MODULE__, :pipeline}
end
@spec pipeline(map(), keyword()) :: Absinthe.Pipeline.t()
def pipeline(config, opts) do
config.schema_mod
|> Absinthe.Pipeline.for_document(opts)
|> Absinthe.Pipeline.insert_after(
Absinthe.Phase.Document.Validation.OnlyOneSubscription,
JetExt.Absinthe.OneOf.Phase
)
end
end
开发笔记
参考:https://maartenvanvliet.nl/2022/04/28/absinthe_input_union/ 他的方案只能校验最上层的 input_object 的 one_of,因此做了修改能够递归地校验 input_object 下层的 one_of。