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.



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

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.



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

def context
  @context
end

#field_mapObject

Returns the value of attribute field_map.



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

def field_map
  @field_map
end

#inputsObject

Returns the value of attribute inputs.



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

def inputs
  @inputs
end

#root_modelObject

Returns the value of attribute root_model.



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

def root_model
  @root_model
end

Instance Method Details

#apply_changesObject



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

def apply_changes
  fail StandardError.new("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



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

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

#changed_modelsObject



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

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

#save!Object



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

def save!
  fail StandardError.new("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



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

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