File tree Expand file tree Collapse file tree 8 files changed +112
-7
lines changed Expand file tree Collapse file tree 8 files changed +112
-7
lines changed Original file line number Diff line number Diff line change @@ -66,6 +66,27 @@ module Types
6666end
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
7192Generate a mutation class from a model.
Original file line number Diff line number Diff line change 11require 'rails/generators/base'
2- # require 'active_support/extend'
2+
33module 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' ,
Original file line number Diff line number Diff line change 11require "rails/generators/named_base"
2+ require_relative 'gql_generator_base'
23
34module 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 \t field :#{ file_name . camelcase ( :lower ) } , mutation: Mutations::#{ prefixed_class_name ( mutation_prefix ) } \n "
23+ end
2424 end
2525 end
2626
Original file line number Diff line number Diff line change 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 : "\t end\n end" ) 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 \t field :#{ file_name . camelcase ( :lower ) } , mutation: Mutations::#{ prefixed_class_name ( prefix ) } \n "
25+ end
26+ end
27+ end
28+ end
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 11module 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments