Class: Graphql::Generators::MutationGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Core
Defined in:
lib/generators/graphql/mutation_generator.rb

Overview

TODO: What other options should be supported?

Examples:

Generate a GraphQL::Schema::RelayClassicMutation by name

rails g graphql:mutation CreatePostMutation

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Core

#create_dir, #create_mutation_root_type, #insert_root_type, #schema_file_path

Constructor Details

#initialize(args, *options) ⇒ MutationGenerator

:nodoc:



19
20
21
22
23
24
25
# File 'lib/generators/graphql/mutation_generator.rb', line 19

def initialize(args, *options) #:nodoc:
  # Unfreeze name in case it's given as a frozen string
  args[0] = args[0].dup if args[0].is_a?(String) && args[0].frozen?
  super

  assign_names!(name)
end

Instance Attribute Details

#field_nameObject (readonly)

Returns the value of attribute field_name.



27
28
29
# File 'lib/generators/graphql/mutation_generator.rb', line 27

def field_name
  @field_name
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



27
28
29
# File 'lib/generators/graphql/mutation_generator.rb', line 27

def file_name
  @file_name
end

#mutation_nameObject (readonly)

Returns the value of attribute mutation_name.



27
28
29
# File 'lib/generators/graphql/mutation_generator.rb', line 27

def mutation_name
  @mutation_name
end

Instance Method Details

#create_mutation_fileObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/generators/graphql/mutation_generator.rb', line 29

def create_mutation_file
  unless @behavior == :revoke
    create_mutation_root_type
  else
    log :gsub, "#{options[:directory]}/types/mutation_type.rb"
  end

  template "mutation.erb", "#{options[:directory]}/mutations/#{file_name}.rb"

  sentinel = /class .*MutationType\s*<\s*[^\s]+?\n/m
  in_root do
    gsub_file "#{options[:directory]}/types/mutation_type.rb", /  \# TODO\: Add Mutations as fields\s*\n/m, ""
    inject_into_file "#{options[:directory]}/types/mutation_type.rb", "    field :#{field_name}, mutation: Mutations::#{mutation_name}\n", after: sentinel, verbose: false, force: false
  end
end