Class: Ibrain::Graphql::MutationGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Core
Defined in:
lib/generators/ibrain/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, #create_repository_root_type, #create_resolver_root_type, included, #insert_root_type, #module_namespacing_when_supported, #schema_file_path

Constructor Details

#initialize(args, *options) ⇒ MutationGenerator

:nodoc:



23
24
25
26
27
28
29
# File 'lib/generators/ibrain/graphql/mutation_generator.rb', line 23

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.



31
32
33
# File 'lib/generators/ibrain/graphql/mutation_generator.rb', line 31

def field_name
  @field_name
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



31
32
33
# File 'lib/generators/ibrain/graphql/mutation_generator.rb', line 31

def file_name
  @file_name
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



31
32
33
# File 'lib/generators/ibrain/graphql/mutation_generator.rb', line 31

def model_name
  @model_name
end

#mutation_nameObject (readonly)

Returns the value of attribute mutation_name.



31
32
33
# File 'lib/generators/ibrain/graphql/mutation_generator.rb', line 31

def mutation_name
  @mutation_name
end

Instance Method Details

#create_mutation_fileObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/generators/ibrain/graphql/mutation_generator.rb', line 33

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

  if options[:model].present?
    system("bundle exec rails generate ibrain:graphql:object #{options[:model].underscore}")
  end

  template "mutation.erb", "#{options[:directory]}/mutations/#{file_name}.rb"
  return unless ::Ibrain::Config.is_auto_append_mutation

  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", "\n    field :#{field_name}, mutation: Mutations::#{mutation_name} \n  ", before: "end\nend", verbose: true, force: true
  end
end