Class: Rails::GraphQL::Field::MutationField

Inherits:
OutputField show all
Defined in:
lib/rails/graphql/field/mutation_field.rb

Overview

GraphQL Mutation Field

This is an extension of a normal output field, which just add extra validation and insurance that the perform step can be executed

Options

  • :call - The alternative method to call to actually perform the mutation. (defaults to nil).

Defined Under Namespace

Modules: Proxied

Constant Summary

Constants included from Helpers::WithCallbacks

Helpers::WithCallbacks::DEFAULT_EVENT_TYPES

Instance Attribute Summary

Attributes included from TypedField

#type

Attributes inherited from Rails::GraphQL::Field

#gql_name, #name, #owner

Instance Method Summary collapse

Methods inherited from OutputField

#=~, #all_events, #all_listeners, #broadcastable?, #entry_point?, #events?, #listeners?, #valid_output?, #validate_output!

Methods included from TypedField

#=~, #all_events, #all_listeners, #as_json, #deserialize, #events?, #initialize_copy, #listeners?, #of_type?, #to_json, #type_klass, #valid_field_types

Methods included from ResolvedField

#dynamic_resolver?, included, #resolve, #resolver

Methods included from AuthorizedField

#authorizable?, #authorize, #authorizer

Methods included from Helpers::WithGlobalID

#to_gid_param, #to_global_id

Methods included from Helpers::WithCallbacks

extended, included, #on

Methods included from Helpers::WithEvents

extended, included, #on

Methods included from Helpers::WithArguments

#=~, #argument, extended, #has_argument?, #has_arguments?, #id_argument, included, #initialize_copy, #ref_argument

Methods inherited from Rails::GraphQL::Field

#=~, #all_owners, #array?, #as_json, #configure, #description, #deserialize, #disable!, #disabled?, #enable!, #enabled?, #initialize_copy, input_type?, #inspect, #internal?, leaf_type?, #method_name, mutation?, #null?, #nullable?, output_type?, proxy?, proxyable_methods, #required!, #required?, #required_items!, subscription?, #to_json, #to_proxy, #valid?, #valid_input?, #valid_output?

Methods included from Helpers::WithDescription

#desc, #description, #description=, #description?

Methods included from Helpers::WithDirectives

#all_directive_events, #all_directive_listeners, #directive_events?, #directive_listeners?, extended, included, #initialize_copy, #use, #using?

Constructor Details

#initialize(*args, call: nil, **xargs, &block) ⇒ MutationField

Intercept the initializer to maybe set the perform_method_name



24
25
26
27
# File 'lib/rails/graphql/field/mutation_field.rb', line 24

def initialize(*args, call: nil, **xargs, &block)
  @perform_method_name = call.to_sym unless call.nil?
  super(*args, **xargs, &block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rails::GraphQL::Field

Instance Method Details

#apply_changes(**xargs, &block) ⇒ Object

Accept changes to the perform method name through the apply_changes



30
31
32
33
# File 'lib/rails/graphql/field/mutation_field.rb', line 30

def apply_changes(**xargs, &block)
  @perform_method_name = xargs.delete(:call) if xargs.key?(:call)
  super
end

#perform(*args, **xargs, &block) ⇒ Object

Add a block or a callable method that is executed before the resolver but after all the before resolve. It returns self for chain purposes



53
54
55
56
# File 'lib/rails/graphql/field/mutation_field.rb', line 53

def perform(*args, **xargs, &block)
  @performer = Callback.new(self, :perform, *args, **xargs, &block)
  self
end

#perform_method_nameObject

Allows overrides for the default bang method



36
37
38
39
40
41
42
43
44
# File 'lib/rails/graphql/field/mutation_field.rb', line 36

def perform_method_name
  if defined?(@perform_method_name)
    @perform_method_name
  elsif from_alternative?
    :perform
  else
    :"#{method_name}!"
  end
end

#performerObject

Get the performer that can be already defined or used through the method_name if that is callable



60
61
62
63
64
65
# File 'lib/rails/graphql/field/mutation_field.rb', line 60

def performer
  return @performer if defined?(@performer)

  @performer = callable?(perform_method_name)
  @performer = Callback.new(self, :perform, perform_method_name) if @performer
end

#schema_typeObject

Change the schema type of the field



47
48
49
# File 'lib/rails/graphql/field/mutation_field.rb', line 47

def schema_type
  :mutation
end

#validate!Object

Ensures that the performer is defined

Raises:



68
69
70
71
72
73
74
75
# File 'lib/rails/graphql/field/mutation_field.rb', line 68

def validate!(*)
  super if defined? super

  raise ValidationError, (+<<~MSG).squish unless performer.present?
    The "#{gql_name}" mutation field must have a perform action through a given
    block or a method named #{method_name} on #{owner.class.name}.
  MSG
end