Class: Ibrain::Graphql::ResolversGenerator

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

Overview

TODO: What other options should be supported?

Examples:

Generate a ‘GraphQL::Schema::Resolver` by name

rails g graphql:resolver PostsResolver

Direct Known Subclasses

ResolverGenerator

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) ⇒ ResolversGenerator

:nodoc:



23
24
25
26
27
28
29
# File 'lib/generators/ibrain/graphql/resolvers_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/resolvers_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/resolvers_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/resolvers_generator.rb', line 31

def model_name
  @model_name
end

#resolver_nameObject (readonly)

Returns the value of attribute resolver_name.



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

def resolver_name
  @resolver_name
end

Instance Method Details

#create_resolver_fileObject



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

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

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

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

  in_root do
    gsub_file "#{options[:directory]}/types/query_type.rb", /  \# TODO: Add Resolvers as fields\s*\n/m, ""
    inject_into_file "#{options[:directory]}/types/query_type.rb", "\n    field :#{field_name}, resolver: Resolvers::#{resolver_name}\n    field :#{field_name}_aggregate, resolver: Resolvers::#{resolver_name}Aggregate \n  ", before: "end\nend", verbose: true, force: true
  end
end