Sha256: 2be6539d95ea62bf5a1482b47b005f6fd5983450a76d91f4e8cdaf920f89cddd

Contents?: true

Size: 709 Bytes

Versions: 1

Compression:

Stored size: 709 Bytes

Contents

module Daidan
  class BaseMutation < GraphQL::Schema::Mutation
    def resolve(**args)
      call_hook(:before_mutation, **args)
      result = execute_mutation(**args)
      call_hook(:after_mutation, **args)
      result
    rescue StandardError => e
      handle_error(e)
    end

    private

    def execute_mutation(**_args)
      raise NotImplementedError, 'Implement mutation logic.'
    end

    def call_hook(hook_name, **args)
      return unless respond_to?(hook_name, true)

      method(hook_name).arity == 0 ? send(hook_name) : send(hook_name, **args)
    end

    def handle_error(error)
      raise GraphQL::ExecutionError, "Error occured during mutation: #{error.message}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
daidan-0.2.0 lib/daidan/graphql/mutations/base_mutation.rb