Sha256: 5448050b6fda6e5ee4d2ecd67c3f01b11364f53655ef9343fc33b227362f865a

Contents?: true

Size: 1.65 KB

Versions: 20

Compression:

Stored size: 1.65 KB

Contents

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

module Graphql
  module Generators
    # TODO: What other options should be supported?
    #
    # @example Generate a `Relay::Mutation` by name
    #     rails g graphql:mutation CreatePostMutation
    class MutationGenerator < Rails::Generators::Base
      include Core

      desc "Create a Relay mutation by name"
      source_root File.expand_path('../templates', __FILE__)

      argument :name, type: :string

      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

      attr_reader :file_name, :mutation_name, :field_name

      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 = /name "Mutation"\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}, Mutations::#{mutation_name}.field\n", after: sentinel, verbose: false, force: false
        end
      end

      private

      def assign_names!(name)
        @field_name = name.camelize(:lower)
        @mutation_name = name.camelize(:upper)
        @file_name = name.camelize.underscore
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
graphql-1.8.1 lib/generators/graphql/mutation_generator.rb
graphql-1.8.0 lib/generators/graphql/mutation_generator.rb
graphql-1.8.0.pre11 lib/generators/graphql/mutation_generator.rb
graphql-1.8.0.pre10 lib/generators/graphql/mutation_generator.rb
graphql-1.7.14 lib/generators/graphql/mutation_generator.rb
graphql-1.8.0.pre9 lib/generators/graphql/mutation_generator.rb
graphql-1.8.0.pre8 lib/generators/graphql/mutation_generator.rb
graphql-1.7.13 lib/generators/graphql/mutation_generator.rb
graphql-1.8.0.pre7 lib/generators/graphql/mutation_generator.rb
graphql-1.7.12 lib/generators/graphql/mutation_generator.rb
graphql-1.7.11 lib/generators/graphql/mutation_generator.rb
graphql-1.7.10 lib/generators/graphql/mutation_generator.rb
graphql-1.8.0.pre6 lib/generators/graphql/mutation_generator.rb
graphql-1.8.0.pre5 lib/generators/graphql/mutation_generator.rb
graphql-1.7.9 lib/generators/graphql/mutation_generator.rb
graphql-1.8.0.pre4 lib/generators/graphql/mutation_generator.rb
graphql-1.8.0.pre3 lib/generators/graphql/mutation_generator.rb
graphql-1.7.8 lib/generators/graphql/mutation_generator.rb
graphql-1.8.0.pre2 lib/generators/graphql/mutation_generator.rb
graphql-1.7.7 lib/generators/graphql/mutation_generator.rb