Sha256: a8820215b681a856dc013c4d2b8820971a778f33439fd9f2a9f76a6894633516
Contents?: true
Size: 1.48 KB
Versions: 9
Compression:
Stored size: 1.48 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 create_mutation_root_type template "mutation.erb", "app/graphql/mutations/#{file_name}.rb" sentinel = /name "Mutation"\s*\n/m in_root do gsub_file "app/graphql/types/mutation_type.rb", / \# TODO\: Add Mutations as fields\s*\n/m, "" inject_into_file "app/graphql/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
9 entries across 9 versions & 1 rubygems