Skip to content

Commit e443ec3

Browse files
authored
Merge pull request #2 from ajsharp/namedspace-mutation-fixes
Namedspace mutation fixes
2 parents 4358496 + b9a4504 commit e443ec3

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

graphql-rails-generators.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Gem::Specification.new do |s|
44
s.version = GraphqlRailsGenerators::VERSION
55
s.platform = Gem::Platform::RUBY
66
s.date = '2019-11-26'
7-
s.summary = "Hola!"
8-
s.description = "A simple hello world gem"
7+
s.summary = "Rails graphql generators"
8+
s.description = "Rails graphql generators"
99
s.authors = ["Alex Sharp"]
1010
s.email = '[email protected]'
1111
s.files = Dir["{lib}/**/*.rb", "LICENSE", "*.md"]

lib/generators/gql/mutation_generator.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
require "rails/generators/named_base"
2+
13
module Gql
2-
class MutationGenerator < Rails::Generators::Base
4+
class MutationGenerator < Rails::Generators::NamedBase
5+
remove_argument :name # remove name base default arg
6+
37
argument :mutation_prefix, type: :string
48
argument :model_name, type: :string
59
source_root File.expand_path('../templates', __FILE__)
10+
11+
# hack to keep NamedBase helpers working
12+
def name
13+
model_name
14+
end
615

716
def mutation
8-
file_name = "#{mutation_prefix}#{model_name}"
9-
template('model_mutation.rb', "app/graphql/mutations/#{file_name.underscore}.rb")
17+
file_name = "#{mutation_prefix}_#{singular_name}"
18+
template('model_mutation.rb', "app/graphql/mutations/#{class_path.join('/')}/#{file_name.underscore}.rb")
1019
end
1120
end
1221

lib/generators/gql/templates/model_mutation.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Mutations
22
class <%= @mutation_prefix %><%= @model_name %> < Mutations::BaseMutation
3-
field :<%= @model_name.underscore %>, Types::<%= @model_name %>Type, null: true
3+
field :<%= singular_name %>, Types::<%= @model_name %>Type, null: true
44
55
argument :attributes, Types::Input::<%= @model_name %>Input, required: true
66
argument :id, Int, required: false
@@ -9,7 +9,7 @@ def resolve(attributes:, id: nil)
99
model = find_or_build_model(id)
1010
model.attributes = attributes.to_h
1111
if model.save
12-
{<%= @model_name.underscore %>: model}
12+
{<%= singular_name %>: model}
1313
else
1414
{errors: model.errors.full_messages}
1515
end

0 commit comments

Comments
 (0)