graphql-rails-generators 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9068fe37ce3d14aea970bad494696cb852365038b50782ae4decffd4f70130ac
4
+ data.tar.gz: 9a07980ae9886f1015e4873d9a33b4123dac3cf7aac04bb0cc8035a406e5b45c
5
+ SHA512:
6
+ metadata.gz: 656cb0197471b75bea857458eb6bc59dce4937caba86835fce3b09f2d45ce59210e061f10ead1d9b20784576b775cc010b3d86837ade2c2ee55cd1641d054ba2
7
+ data.tar.gz: d1b073eb0fbf73b19c0ec0d3da6c3d220f17b071a87e7cf69b3b8ae7ebdb146b005c3cff148dcd8f2123845c16ccf4e5842170a6cd61ac727e06e9d39385a977
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Alex Sharp
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,108 @@
1
+ # graphql-rails-generators
2
+
3
+ A few generators to make it easy to integrate your Rails models with [graphql-ruby](https://github.com/rmosolgo/graphql-ruby). I created this because I was wasting too many keystrokes copying my model schema by hand to create graphql types.
4
+
5
+ This project contains three generators that look at your ActiveRecord model schema and generates graphql types for you.
6
+
7
+ * `gql:model_type Post` - Generate a graphql type for a model
8
+ * `gql:input Post` - Generate a graphql input type for a model
9
+ * `gql:mutation Update Post` - Generate a graphql mutation class for a model
10
+
11
+ ## Installation
12
+
13
+ ```
14
+ gem 'graphql-rails-generators', group: :development
15
+ ```
16
+
17
+ ## Requirements
18
+
19
+ This library only supports ActiveRecord, though it would be fairly trivial to add support for other ORMs.
20
+
21
+ ## Usage
22
+
23
+ ### gql:model_type
24
+
25
+ Generate a model type from a model.
26
+
27
+ ```
28
+ $ rails generate gql:model_type MODEL_CLASS
29
+ ```
30
+
31
+ Result:
32
+
33
+ ```ruby
34
+ # app/graphql/post_type.rb
35
+ module Types
36
+ class PostType < Types::BaseObject
37
+ field :id, Int, null: true
38
+ field :title, String, null: true
39
+ field :body, String, null: true
40
+ field :created_at, GraphQL::Types::ISO8601DateTime, null: true
41
+ field :updated_at, GraphQL::Types::ISO8601DateTime, null: true
42
+ end
43
+ end
44
+ ```
45
+
46
+ ### gql:input MODEL_CLASS
47
+
48
+ Generate an input type from a model.
49
+
50
+ ```
51
+ rails generate gql:input Post
52
+ ```
53
+
54
+ Result:
55
+ ```ruby
56
+ # app/graphql/types/post_input.rb
57
+ module Types
58
+ module Input
59
+ class PostInput < Types::BaseInputObject
60
+ argument :title, String, required: false
61
+ argument :body, String, required: false
62
+ end
63
+ end
64
+ end
65
+ ```
66
+
67
+ ### gql:mutation MUTATION_PREFIX MODEL_NAME
68
+
69
+ Generate a mutation class from a model.
70
+
71
+ A quick note about the mutation generator...
72
+
73
+ The mutation generator generates something akin to an "upsert" mutation. It takes two arguments: an optional `id` and an optional `attributes`, which is the input type for the model. If you pass an `id`, it will attempt to find the model by the `id` and update it, otherwise it will initialize a new model and attempt to save it.
74
+
75
+ ```
76
+ rails generate gql:mutation Update Post
77
+ ```
78
+
79
+ Result:
80
+ ```ruby
81
+ # app/graphql/mutations/update_post.rb
82
+ module Mutations
83
+ class UpdatePost < Mutations::BaseMutation
84
+ field :post, Types::PostType, null: true
85
+
86
+ argument :attributes, Types::Input::PostInput, required: true
87
+ argument :id, Int, required: false
88
+
89
+ def resolve(attributes:, id: nil)
90
+ model = find_or_build_model(id)
91
+ model.attributes = attributes.to_h
92
+ if model.save
93
+ {post: model}
94
+ else
95
+ {errors: model.errors.full_messages}
96
+ end
97
+ end
98
+
99
+ def find_or_build_model(id)
100
+ if id
101
+ Post.find(id)
102
+ else
103
+ Post.new
104
+ end
105
+ end
106
+ end
107
+ end
108
+ ```
@@ -0,0 +1,32 @@
1
+ require 'rails/generators/base'
2
+ # require 'active_support/extend'
3
+ module Gql
4
+ module GqlGeneratorBase
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ protected
9
+ def type_map
10
+ {
11
+ integer: 'Int',
12
+ string: 'String',
13
+ boolean: 'Boolean',
14
+ decimal: 'Float',
15
+ datetime: 'GraphQL::Types::ISO8601DateTime',
16
+ date: 'GraphQL::Types::ISO8601Date',
17
+ hstore: 'GraphQL::Types::JSON'
18
+ }
19
+ end
20
+
21
+ def map_model_types(model_name)
22
+ klass = model_name.constantize
23
+ associations = klass.reflect_on_all_associations(:belongs_to)
24
+ bt_columns = associations.map(&:foreign_key)
25
+
26
+ klass.columns
27
+ .reject { |col| bt_columns.include?(col.name) }
28
+ .map { |col| {name: col.name, gql_type: type_map.fetch(col.type)} }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,17 @@
1
+ require_relative 'gql_generator_base'
2
+ module Gql
3
+ class InputGenerator < Rails::Generators::Base
4
+ include GqlGeneratorBase
5
+ source_root File.expand_path('../templates', __FILE__)
6
+ argument :model_name, type: :string
7
+
8
+ def generate_input_type
9
+ file_name = model_name
10
+
11
+ ignore = ['id', 'created_at', 'updated_at']
12
+ @fields = map_model_types(model_name).reject { |field| ignore.include?(field[:name]) }
13
+
14
+ template('input_type.rb', "app/graphql/types/input/#{file_name.underscore}_input.rb")
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'gql_generator_base'
2
+ module Gql
3
+ class ModelTypeGenerator < Rails::Generators::Base
4
+ include GqlGeneratorBase
5
+ source_root File.expand_path('../templates', __FILE__)
6
+ argument :model_name, type: :string
7
+
8
+ def type
9
+ file_name = "#{model_name.underscore}_type"
10
+ @fields = map_model_types(model_name)
11
+ template('model_type.rb', "app/graphql/types/#{file_name}.rb")
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ module Gql
2
+ class MutationGenerator < Rails::Generators::Base
3
+ argument :mutation_prefix, type: :string
4
+ argument :model_name, type: :string
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ def mutation
8
+ file_name = "#{mutation_prefix}#{model_name}"
9
+ template('model_mutation.rb', "app/graphql/mutations/#{file_name.underscore}.rb")
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,9 @@
1
+ module Types
2
+ module Input
3
+ class <%= @model_name %>Input < Types::BaseInputObject
4
+ <% @fields.each do |field| -%>
5
+ argument :<%= field[:name] %>, <%= field[:gql_type] %>, required: false
6
+ <% end %>
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,26 @@
1
+ module Mutations
2
+ class <%= @mutation_prefix %><%= @model_name %> < Mutations::BaseMutation
3
+ field :<%= @model_name.underscore %>, Types::<%= @model_name %>Type, null: true
4
+
5
+ argument :attributes, Types::Input::<%= @model_name %>Input, required: true
6
+ argument :id, Int, required: false
7
+
8
+ def resolve(attributes:, id: nil)
9
+ model = find_or_build_model(id)
10
+ model.attributes = attributes.to_h
11
+ if model.save
12
+ {<%= @model_name.underscore %>: model}
13
+ else
14
+ {errors: model.errors.full_messages}
15
+ end
16
+ end
17
+
18
+ def find_or_build_model(id)
19
+ if id
20
+ <%= @model_name %>.find(id)
21
+ else
22
+ <%= @model_name %>.new
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ module Types
2
+ class <%= @model_name %>Type < Types::BaseObject
3
+ <% @fields.each do |field| -%>
4
+ field :<%= field[:name] %>, <%= field[:gql_type] %>, null: true
5
+ <% end %>
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module GraphqlRailsGenerators
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphql-rails-generators
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex Sharp
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple hello world gem
14
+ email: ajsharp@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README.md
21
+ - lib/generators/gql/gql_generator_base.rb
22
+ - lib/generators/gql/input_generator.rb
23
+ - lib/generators/gql/model_type_generator.rb
24
+ - lib/generators/gql/mutation_generator.rb
25
+ - lib/generators/gql/templates/input_type.rb
26
+ - lib/generators/gql/templates/model_mutation.rb
27
+ - lib/generators/gql/templates/model_type.rb
28
+ - lib/graphql-rails-generators/version.rb
29
+ homepage: https://github.com/ajsharp/graphql-rails-generators
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.7.9
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Hola!
53
+ test_files: []