AbsintheErrorPayload seems not work

This is my mutations

  payload_object(:user_payload, :user)

  object :accounts_mutations do
    field :register, type: :user_payload do
      arg :input, non_null(:register_input)
      resolve &Resolvers.AccountsMutation.register/3
      middleware &build_payload/2
    end

and the response is

{
  "data": {
    "register": null
  },
  "errors": [
    {
      "locations": [
        {
          "column": 0,
          "line": 2
        }
      ],
      "message": "weixin has already been bound",
      "path": [
        "register"
      ]
    }
  ]
}

if I modify the middleware to a module, and print the resolution result

defmodule AwBackendWeb.Schema.Middleware.BuildPayload do
  @behaviour Absinthe.Middleware

  import AbsintheErrorPayload.Payload

  def call(%{errors: errors} = resolution, _config) do
    result = convert_to_payload({:error, errors})
    resolution = %{resolution | state: :resolved, value: result}
    resolution.value |> IO.inspect(label: "value")
    resolution.state |> IO.inspect(label: "state")
    resolution
  end

  def call(%{value: value, errors: []} = resolution, _config) do
    result = convert_to_payload(value)
    Absinthe.Resolution.put_result(resolution, {:ok, result})
  end
end

We’ll see

value: %AbsintheErrorPayload.Payload{
  messages: [
    %AbsintheErrorPayload.ValidationMessage{
      code: :unknown,
      field: nil,
      key: nil,
      message: "weixin has already been bound",
      options: [],
      template: "weixin has already been bound"
    }
  ],
  result: nil,
  successful: false
}
state: :resolved

But my expected result is

{
  "data": {
    "register": {"successful": "false"}
  }
}

I want to know how to fix the middleware.

Finally I found the problem,

the source code of AbsintheErrorPayload only put_result is not enough, we also need to update the errors of resolution.

1 Like