graphql-ruby icon indicating copy to clipboard operation
graphql-ruby copied to clipboard

"Errors as data" from within `prepare` blocks

Open eapache-opslevel opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe.

prepared arguments on mutations have no way to return errors as data; they can return a value, or they can raise an exception. Currently, we have to shove values in context and then remember to check for context[:errors].present? at the top of every single mutation.

Describe the solution you'd like

In line with the solution for authorized? (https://graphql-ruby.org/mutations/mutation_authorization.html#adding-errors) it would be nice to be able to return a mutation payload with errors from a prepare block that would halt the mutation.

Since there's no return value that couldn't theoretically be intended as the real prepared value, it might be better to designate a specific new exception, like raise GraphQL::ErrorAsData, { errors: [...] } or something.

cc my non-work account @eapache

@rmosolgo I don't know if this ends up being pretty easy or not... prepare is used for non-mutation arguments too and it's not clear how we would need to handle those. Maybe it's just considered a bug / internal error to raise that exception outside of a mutation context?

eapache-opslevel avatar Aug 01 '23 15:08 eapache-opslevel

👋 Hey!

I can see how this would be useful and raising an error like that makes sense to me, from a practical standpoint.

Regarding non-mutations, I think the same behavior would be OK: if such an error is raised, use it as the field's return value.

rmosolgo avatar Aug 02 '23 14:08 rmosolgo