Last active
September 17, 2020 18:16
-
-
Save rmosolgo/e728e532826b494e6c7d5e859c78b498 to your computer and use it in GitHub Desktop.
A base mutation that checks for selections on `errors`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A base mutation that adds an errors field to all subclasses, and before resolving, it checks to make sure that the caller selected `errors`. | |
# | |
# (You could use `GraphQL::Schema::Mutation` as a base class, too.) | |
class Mutations::BaseMutation < GraphQL::Schema::RelayClassicMutation | |
# Add the errors field to all mutations | |
field :errors, [Types::MutationError], null: false | |
# Inject `lookahead` to the resolve method | |
extras [:lookahead] | |
def resolve_with_support(lookahead:, **kwargs) | |
if !lookahead.selects?(:errors) | |
raise GraphQL::ExecutionError, "Add a selection on `errors { ... }` to ensure that mutation errors are handled correctly" | |
end | |
super(**kwargs) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment