Class: GraphQL::Models::Mutator

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/models/mutator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_map, root_model, inputs, context) ⇒ Mutator

Returns a new instance of Mutator.



6
7
8
9
10
11
# File 'lib/graphql/models/mutator.rb', line 6

def initialize(field_map, root_model, inputs, context)
  @field_map = field_map
  @root_model = root_model
  @inputs = inputs
  @context = context
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



4
5
6
# File 'lib/graphql/models/mutator.rb', line 4

def context
  @context
end

#field_mapObject

Returns the value of attribute field_map.



4
5
6
# File 'lib/graphql/models/mutator.rb', line 4

def field_map
  @field_map
end

#inputsObject

Returns the value of attribute inputs.



4
5
6
# File 'lib/graphql/models/mutator.rb', line 4

def inputs
  @inputs
end

#root_modelObject

Returns the value of attribute root_model.



4
5
6
# File 'lib/graphql/models/mutator.rb', line 4

def root_model
  @root_model
end

Instance Method Details

#apply_changesObject

Raises:

  • (StandardError)


13
14
15
16
17
# File 'lib/graphql/models/mutator.rb', line 13

def apply_changes
  raise StandardError, "Called apply_changes twice for the same mutator" if @all_changes
  @all_changes = MutationHelpers.apply_changes(field_map, root_model, inputs, context)
  changed_models
end

#authorize!Object

Raises:

  • (StandardError)


29
30
31
32
# File 'lib/graphql/models/mutator.rb', line 29

def authorize!
  raise StandardError, "Need to call apply_changes before #{__method__}" unless @all_changes
  MutationHelpers.authorize_changes(context, @all_changes)
end

#changed_modelsObject

Raises:

  • (StandardError)


19
20
21
22
# File 'lib/graphql/models/mutator.rb', line 19

def changed_models
  raise StandardError, "Need to call apply_changes before #{__method__}" unless @all_changes
  @all_changes.map { |c| c[:model_instance] }.uniq
end

#save!Object

Raises:

  • (StandardError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/graphql/models/mutator.rb', line 34

def save!
  raise StandardError, "Need to call apply_changes before #{__method__}" unless @all_changes

  ActiveRecord::Base.transaction(requires_new: true) do
    changed_models.each do |model|
      next if model.destroyed?

      if model.marked_for_destruction?
        model.destroy
      else
        model.save!
      end
    end

    changed_models.reject(&:destroyed?)
  end
end

#validate!Object

Raises:

  • (StandardError)


24
25
26
27
# File 'lib/graphql/models/mutator.rb', line 24

def validate!
  raise StandardError, "Need to call apply_changes before #{__method__}" unless @all_changes
  MutationHelpers.validate_changes(inputs, field_map, root_model, context, @all_changes)
end