Skip to content

Commit b864b32

Browse files
authored
Merge pull request #4 from ajsharp/cud-mutations
Create, update and delete mutation generators
2 parents 8e605a9 + b9b43b4 commit b864b32

File tree

8 files changed

+112
-7
lines changed

8 files changed

+112
-7
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,27 @@ module Types
6666
end
6767
```
6868

69+
### gql:mutations MODEL_CLASS
70+
71+
Generate create, update and delete mutations for a model.
72+
73+
```
74+
rails generate gql:mutations Post
75+
```
76+
77+
Result:
78+
```ruby
79+
# app/graphql/types/post_input.rb
80+
module Types
81+
module Input
82+
class PostInput < Types::BaseInputObject
83+
argument :title, String, required: false
84+
argument :body, String, required: false
85+
end
86+
end
87+
end
88+
```
89+
6990
### gql:mutation MUTATION_PREFIX MODEL_NAME
7091

7192
Generate a mutation class from a model.

lib/generators/gql/gql_generator_base.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
require 'rails/generators/base'
2-
# require 'active_support/extend'
2+
33
module Gql
44
module GqlGeneratorBase
55
extend ActiveSupport::Concern
66

77
included do
88
protected
9+
10+
# Generate a namedspaced class name with the mutation prefix
11+
def prefixed_class_name(prefix)
12+
(class_path + ["#{prefix}_#{file_name}"]).map!(&:camelize).join("::")
13+
end
14+
915
def type_map
1016
{
1117
integer: 'Int',

lib/generators/gql/mutation_generator.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
require "rails/generators/named_base"
2+
require_relative 'gql_generator_base'
23

34
module Gql
45
class MutationGenerator < Rails::Generators::NamedBase
6+
include GqlGeneratorBase
57
remove_argument :name # remove name base default arg
68

79
argument :mutation_prefix, type: :string
@@ -12,15 +14,13 @@ class MutationGenerator < Rails::Generators::NamedBase
1214
def name
1315
model_name
1416
end
15-
16-
# Generate a namedspaced class name with the mutation prefix
17-
def prefixed_class_name
18-
(class_path + ["#{mutation_prefix}_#{file_name}"]).map!(&:camelize).join("::")
19-
end
2017

2118
def mutation
2219
file_name = "#{mutation_prefix}_#{singular_name}"
2320
template('model_mutation.rb', "app/graphql/mutations/#{class_path.join('/')}/#{file_name.underscore}.rb")
21+
insert_into_file("app/graphql/types/mutation_type.rb", after: " class MutationType < Types::BaseObject\n") do
22+
"\t\tfield :#{file_name.camelcase(:lower)}, mutation: Mutations::#{prefixed_class_name(mutation_prefix)}\n"
23+
end
2424
end
2525
end
2626

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require "rails/generators/named_base"
2+
require_relative 'gql_generator_base'
3+
4+
module Gql
5+
class MutationsGenerator < Rails::Generators::NamedBase
6+
include GqlGeneratorBase
7+
source_root File.expand_path('../templates', __FILE__)
8+
desc "Generate create, update and delete generators for a model."
9+
10+
def mutations
11+
insert_into_file("app/graphql/mutations/base_mutation.rb", before: "\tend\nend") do
12+
"def model_errors!(model)\n# define me!\n"
13+
end
14+
generate_mutation('update')
15+
generate_mutation('create')
16+
generate_mutation('delete')
17+
end
18+
19+
protected
20+
def generate_mutation(prefix)
21+
file_name = "#{prefix}_#{singular_name}"
22+
template("#{prefix}_mutation.rb", "app/graphql/mutations/#{class_path.join('/')}/#{file_name.underscore}.rb")
23+
insert_into_file("app/graphql/types/mutation_type.rb", after: " class MutationType < Types::BaseObject\n") do
24+
"\t\tfield :#{file_name.camelcase(:lower)}, mutation: Mutations::#{prefixed_class_name(prefix)}\n"
25+
end
26+
end
27+
end
28+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Mutations
2+
class <%= prefixed_class_name('Create') %> < Mutations::BaseMutation
3+
field :<%= singular_name %>, Types::<%= name %>Type, null: true
4+
5+
argument :attributes, Types::Input::<%= name %>Input, required: true
6+
7+
def resolve(attributes:)
8+
model = <%= name %>.new(attributes.to_h)
9+
10+
if model.save
11+
{<%= singular_name %>: model}
12+
else
13+
model_errors!(model)
14+
end
15+
end
16+
end
17+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Mutations
2+
class <%= prefixed_class_name('Delete') %> < Mutations::BaseMutation
3+
field :<%= singular_name %>, Types::<%= name %>Type, null: true
4+
5+
argument :id, Int, required: true
6+
7+
def resolve(id:)
8+
model = <%= class_name %>.find(id)
9+
10+
model.destroy
11+
{<%= singular_name %>: model}
12+
end
13+
end
14+
end

lib/generators/gql/templates/model_mutation.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Mutations
2-
class <%= prefixed_class_name %> < Mutations::BaseMutation
2+
class <%= prefixed_class_name(mutation_prefix) %> < Mutations::BaseMutation
33
field :<%= singular_name %>, Types::<%= @model_name %>Type, null: true
44
55
argument :attributes, Types::Input::<%= @model_name %>Input, required: true
@@ -8,6 +8,7 @@ class <%= prefixed_class_name %> < Mutations::BaseMutation
88
def resolve(attributes:, id: nil)
99
model = find_or_build_model(id)
1010
model.attributes = attributes.to_h
11+
1112
if model.save
1213
{<%= singular_name %>: model}
1314
else
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Mutations
2+
class <%= prefixed_class_name('Update') %> < Mutations::BaseMutation
3+
field :<%= singular_name %>, Types::<%= name %>Type, null: true
4+
5+
argument :id, Int, required: true
6+
argument :attributes, Types::Input::<%= name %>Input, required: true
7+
8+
def resolve(attributes:, id:)
9+
model = <%= class_name %>.find(id)
10+
11+
if model.update_attributes(attributes.to_h)
12+
{<%= singular_name %>: model}
13+
else
14+
model_errors!(model)
15+
end
16+
end
17+
end
18+
end

0 commit comments

Comments
 (0)