itopiz
How to stop a field resolution after a middleware returns an error?
In my GraphQL schema, I am using an authentication middleware similar to the one in the documentation. This middleware is set for all mutation fields.
In some mutation fields, I also want to use the Absinthe.Relay.Node.ParseIDs middleware.
In case a user is not authenticated and submits a mutation with an input that does not pass the ParseIDs middleware, the server returns the error message:
In argument \"input\": In field \"userId\": Could not decode ID value `foobar'
but I was expecting:
unauthenticated
After reading the Absinthe.Middleware documentation, I understand that both the authentication and ParseIDs middlewares are run anyway.
All middleware on a field are always run, make sure to pattern match on the state if you care.
When no user is authenticated, is there a way to receive the error message “unauthenticated” regardless of the input provided?
Most Liked
tspenov
resolution
|> Absinthe.Resolution.put_result({:error, unauthenticated()})
changes the state of the Resolution struct to resolved.
So you can add a clause in ParseIDs middleware to catch this case and pass like this:
def call(%Resolution{state: :resolved} = resolution, _) do
resolution
end
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








