graphql-rails-generators 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b58b52d9aedd9eede25bf252549e66273bdd59f60dcfadae71c858390f6b6c2
4
- data.tar.gz: e1ce9f70a44fb26d0943498882c31631e390518bc9d699b93b8baaf6803ec05a
3
+ metadata.gz: 38981ddd6d3252268196ca9c333672436e63ee24badf5fa54e72915c4bbcfeb5
4
+ data.tar.gz: cbe8ba3110f66a30c2dbba7e2301b33e7ab52bd7deb2b6f7e4d37afc0b1bb9f0
5
5
  SHA512:
6
- metadata.gz: 2cf3908b610b74597d747de8b10d5c7f9d45297aad9fd0fd1664379fc5a3da9a2d191737b07d742d58a376b018bf11baa1bb7c5c8cd31d0cbc6334165f410427
7
- data.tar.gz: 5eb8fe1b19b233ae06bafde06c0cbe96699c1042b20d5b21f9066cee3e4634da62924f4fd26d3ab46436070e2d7b534296131a1803f9406f29fc2396fa13bacd
6
+ metadata.gz: e359081f8e02097e84af9d00fdc507e6d42fdc6d2b9727fb8732fc5f8b420da9d6279e62b99f79ab6f5b5bdf055484499a74df2cc9c4b9f829533afa1d046949
7
+ data.tar.gz: d28b879ad5152567bd3e66b58dfe5713c3920f315553c5b94e0e8836f790914de8c1b5b5bd4886abd908f076712967e479eadc053bbd0d6f58738265f3446d57
data/README.md CHANGED
@@ -64,6 +64,27 @@ module Types
64
64
  end
65
65
  ```
66
66
 
67
+ ### gql:mutations MODEL_CLASS
68
+
69
+ Generate create, update and delete mutations for a model.
70
+
71
+ ```
72
+ rails generate gql:mutations Post
73
+ ```
74
+
75
+ Result:
76
+ ```ruby
77
+ # app/graphql/types/post_input.rb
78
+ module Types
79
+ module Input
80
+ class PostInput < Types::BaseInputObject
81
+ argument :title, String, required: false
82
+ argument :body, String, required: false
83
+ end
84
+ end
85
+ end
86
+ ```
87
+
67
88
  ### gql:mutation MUTATION_PREFIX MODEL_NAME
68
89
 
69
90
  Generate a mutation class from a model.
@@ -1,11 +1,17 @@
1
1
  require 'rails/generators/base'
2
- # require 'active_support/extend'
2
+
3
3
  module Gql
4
4
  module GqlGeneratorBase
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  included do
8
8
  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
+
9
15
  def type_map
10
16
  {
11
17
  integer: 'Int',
@@ -14,7 +20,9 @@ module Gql
14
20
  decimal: 'Float',
15
21
  datetime: 'GraphQL::Types::ISO8601DateTime',
16
22
  date: 'GraphQL::Types::ISO8601Date',
17
- hstore: 'GraphQL::Types::JSON'
23
+ hstore: 'GraphQL::Types::JSON',
24
+ text: 'String',
25
+ json: 'GraphQL::Types::JSON'
18
26
  }
19
27
  end
20
28
 
@@ -25,7 +33,8 @@ module Gql
25
33
 
26
34
  klass.columns
27
35
  .reject { |col| bt_columns.include?(col.name) }
28
- .map { |col| {name: col.name, gql_type: type_map.fetch(col.type)} }
36
+ .reject { |col| type_map[col.type].nil? }
37
+ .map { |col| {name: col.name, gql_type: type_map[col.type]} }
29
38
  end
30
39
  end
31
40
  end
@@ -1,7 +1,9 @@
1
1
  require "rails/generators/named_base"
2
+ require_relative 'gql_generator_base'
2
3
 
3
4
  module Gql
4
5
  class MutationGenerator < Rails::Generators::NamedBase
6
+ include GqlGeneratorBase
5
7
  remove_argument :name # remove name base default arg
6
8
 
7
9
  argument :mutation_prefix, type: :string
@@ -16,6 +18,9 @@ module Gql
16
18
  def mutation
17
19
  file_name = "#{mutation_prefix}_#{singular_name}"
18
20
  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
19
24
  end
20
25
  end
21
26
 
@@ -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
@@ -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
@@ -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
@@ -1,5 +1,5 @@
1
1
  module Mutations
2
- class <%= @mutation_prefix %><%= @model_name %> < Mutations::BaseMutation
2
+ class <%= prefixed_class_name(mutation_prefix) %> < Mutations::BaseMutation
3
3
  field :<%= singular_name %>, Types::<%= @model_name %>Type, null: true
4
4
 
5
5
  argument :attributes, Types::Input::<%= @model_name %>Input, required: true
@@ -8,6 +8,7 @@ module Mutations
8
8
  def resolve(attributes:, id: nil)
9
9
  model = find_or_build_model(id)
10
10
  model.attributes = attributes.to_h
11
+
11
12
  if model.save
12
13
  {<%= singular_name %>: model}
13
14
  else
@@ -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
@@ -1,3 +1,3 @@
1
1
  module GraphqlRailsGenerators
2
- VERSION = '1.0.1'
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-rails-generators
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Sharp
@@ -22,9 +22,13 @@ files:
22
22
  - lib/generators/gql/input_generator.rb
23
23
  - lib/generators/gql/model_type_generator.rb
24
24
  - lib/generators/gql/mutation_generator.rb
25
+ - lib/generators/gql/mutations_generator.rb
26
+ - lib/generators/gql/templates/create_mutation.rb
27
+ - lib/generators/gql/templates/delete_mutation.rb
25
28
  - lib/generators/gql/templates/input_type.rb
26
29
  - lib/generators/gql/templates/model_mutation.rb
27
30
  - lib/generators/gql/templates/model_type.rb
31
+ - lib/generators/gql/templates/update_mutation.rb
28
32
  - lib/graphql-rails-generators/version.rb
29
33
  homepage: https://github.com/ajsharp/graphql-rails-generators
30
34
  licenses: