Sha256: 32884869c4e7b620e47df286b8665f415e3abbe2cfc4f5c9afc3de5a16087511

Contents?: true

Size: 1.49 KB

Versions: 11

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true
require 'rails/generators'
require 'rails/generators/named_base'
require_relative 'core'

module Graphql
  module Generators
    # TODO: What other options should be supported?
    #
    # @example Generate a `GraphQL::Schema::RelayClassicMutation` by name
    #     rails g graphql:mutation CreatePostMutation
    class OrmMutationsBase < Rails::Generators::NamedBase
      include Core
      include Rails::Generators::ResourceHelpers

      desc "Create a Relay Classic mutation by name"

      class_option :orm, banner: "NAME", type: :string, required: true,
                         desc: "ORM to generate the controller for"

      class_option :namespaced_types,
        type: :boolean,
        required: false,
        default: false,
        banner: "Namespaced",
        desc: "If the generated types will be namespaced"

      def create_mutation_file
        template "mutation_#{operation_type}.erb", File.join(options[:directory], "/mutations/", class_path, "#{file_name}_#{operation_type}.rb")

        sentinel = /class .*MutationType\s*<\s*[^\s]+?\n/m
        in_root do
          path = "#{options[:directory]}/types/mutation_type.rb"
          invoke "graphql:install:mutation_root" unless File.exist?(path)
          inject_into_file "#{options[:directory]}/types/mutation_type.rb", "    field :#{file_name}_#{operation_type}, mutation: Mutations::#{class_name}#{operation_type.classify}\n", after: sentinel, verbose: false, force: false
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
graphql-2.4.4 lib/generators/graphql/orm_mutations_base.rb
graphql-2.4.3 lib/generators/graphql/orm_mutations_base.rb
graphql-2.4.2 lib/generators/graphql/orm_mutations_base.rb
graphql-2.4.1 lib/generators/graphql/orm_mutations_base.rb
graphql-2.4.0 lib/generators/graphql/orm_mutations_base.rb
graphql-2.3.20 lib/generators/graphql/orm_mutations_base.rb
graphql-2.3.19 lib/generators/graphql/orm_mutations_base.rb
graphql-2.3.18 lib/generators/graphql/orm_mutations_base.rb
graphql-2.3.17 lib/generators/graphql/orm_mutations_base.rb
graphql-2.3.16 lib/generators/graphql/orm_mutations_base.rb
graphql-2.3.15 lib/generators/graphql/orm_mutations_base.rb