Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
Last active September 17, 2020 18:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmosolgo/e728e532826b494e6c7d5e859c78b498 to your computer and use it in GitHub Desktop.
Save rmosolgo/e728e532826b494e6c7d5e859c78b498 to your computer and use it in GitHub Desktop.
A base mutation that checks for selections on `errors`
# 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