Sha256: da798038d41f781a6f370b8db75f8bfd3e5f67fd1996912eb61f8b98479b2dd6

Contents?: true

Size: 1.57 KB

Versions: 33

Compression:

Stored size: 1.57 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)
          mutation_result = begin
            @resolve.call(obj, args[:input], ctx)
          rescue GraphQL::ExecutionError => err
            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 mutation_result.nil?
            nil
          elsif @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

33 entries across 33 versions & 1 rubygems

Version Path
graphql-1.7.14 lib/graphql/relay/mutation/resolve.rb
graphql-1.8.0.pre9 lib/graphql/relay/mutation/resolve.rb
graphql-1.8.0.pre8 lib/graphql/relay/mutation/resolve.rb
graphql-1.7.13 lib/graphql/relay/mutation/resolve.rb
graphql-1.8.0.pre7 lib/graphql/relay/mutation/resolve.rb
graphql-1.7.12 lib/graphql/relay/mutation/resolve.rb
graphql-1.7.11 lib/graphql/relay/mutation/resolve.rb
graphql-1.7.10 lib/graphql/relay/mutation/resolve.rb
graphql-1.8.0.pre6 lib/graphql/relay/mutation/resolve.rb
graphql-1.8.0.pre5 lib/graphql/relay/mutation/resolve.rb
graphql-1.7.9 lib/graphql/relay/mutation/resolve.rb
graphql-1.8.0.pre4 lib/graphql/relay/mutation/resolve.rb
graphql-1.8.0.pre3 lib/graphql/relay/mutation/resolve.rb
graphql-1.7.8 lib/graphql/relay/mutation/resolve.rb
graphql-1.8.0.pre2 lib/graphql/relay/mutation/resolve.rb
graphql-1.7.7 lib/graphql/relay/mutation/resolve.rb
graphql-1.8.0.pre1 lib/graphql/relay/mutation/resolve.rb
graphql-1.7.6 lib/graphql/relay/mutation/resolve.rb
graphql-1.7.5 lib/graphql/relay/mutation/resolve.rb
graphql-1.7.4 lib/graphql/relay/mutation/resolve.rb