Sha256: 72336bb013987ebcf996c955467424135059ef67b21530b1f920449e53e58f36

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true
module GraphQL
  module Relay
    class Mutation
      # Wrap a user-provided resolve function,
      # wrapping the returned value in a {Mutation::Result}.
      # Also, pass the `clientMutationId` to that result object.
      # @api private
      class Resolve
        def initialize(mutation, resolve)
          @mutation = mutation
          @resolve = resolve
          @wrap_result = mutation.has_generated_return_type?
        end

        def call(obj, args, ctx)
          begin
            mutation_result = @resolve.call(obj, args[:input], ctx)
          rescue GraphQL::ExecutionError => err
            mutation_result = err
          end

          if ctx.schema.lazy?(mutation_result)
            mutation_result
          else
            build_result(mutation_result, args, ctx)
          end
        end

        private

        def build_result(mutation_result, args, ctx)
          if mutation_result.is_a?(GraphQL::ExecutionError)
            ctx.add_error(mutation_result)
            mutation_result = nil
          end

          if @wrap_result
            if mutation_result && !mutation_result.is_a?(Hash)
              raise StandardError, "Expected `#{mutation_result}` to be a Hash."\
                " Return a hash when using `return_field` or specify a custom `return_type`."
            end

            @mutation.result_class.new(client_mutation_id: args[:input][:clientMutationId], result: mutation_result)
          else
            mutation_result
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
graphql-1.5.12 lib/graphql/relay/mutation/resolve.rb
graphql-1.5.11 lib/graphql/relay/mutation/resolve.rb
graphql-1.5.10 lib/graphql/relay/mutation/resolve.rb