module GraphQL::Define::AssignMutationFunction

Public Class Methods

call(target, function) click to toggle source
# File lib/graphql/define/assign_mutation_function.rb, line 4
def self.call(target, function)
  # TODO: get all this logic somewhere easier to test

  if !function.type.is_a?(GraphQL::ObjectType)
    raise "Mutation functions must return object types (not #{function.type.unwrap})"
  end

  target.return_type = function.type.redefine {
    name(target.name + "Payload")
    field :clientMutationId, types.String, "A unique identifier for the client performing the mutation.", property: :client_mutation_id
  }

  target.arguments = function.arguments
  target.description = function.description
  target.resolve = ->(o, a, c) {
    res = function.call(o, a, c)
    ResultProxy.new(res, a[:clientMutationId])
  }
end