graphql_rails 1.2.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +18 -0
  3. data/.hound.yml +1 -1
  4. data/.rubocop.yml +1 -0
  5. data/.ruby-version +1 -1
  6. data/CHANGELOG.md +17 -1
  7. data/Gemfile +3 -3
  8. data/Gemfile.lock +27 -25
  9. data/docs/README.md +1 -1
  10. data/docs/components/model.md +39 -5
  11. data/docs/other_tools/schema_dump.md +8 -8
  12. data/lib/graphql_rails/attributes/attributable.rb +6 -14
  13. data/lib/graphql_rails/attributes/attribute.rb +8 -33
  14. data/lib/graphql_rails/attributes/attribute_configurable.rb +33 -0
  15. data/lib/graphql_rails/attributes/attribute_name_parser.rb +7 -7
  16. data/lib/graphql_rails/attributes/input_attribute.rb +19 -13
  17. data/lib/graphql_rails/attributes/input_type_parser.rb +5 -7
  18. data/lib/graphql_rails/attributes/type_parseable.rb +32 -11
  19. data/lib/graphql_rails/attributes/type_parser.rb +4 -9
  20. data/lib/graphql_rails/concerns/chainable_options.rb +49 -0
  21. data/lib/graphql_rails/input_configurable.rb +10 -8
  22. data/lib/graphql_rails/model/build_connection_type/count_items.rb +1 -1
  23. data/lib/graphql_rails/model/build_connection_type.rb +1 -1
  24. data/lib/graphql_rails/model/configurable.rb +30 -8
  25. data/lib/graphql_rails/model/configuration.rb +26 -12
  26. data/lib/graphql_rails/model/find_or_build_graphql_type.rb +23 -21
  27. data/lib/graphql_rails/model/input.rb +10 -26
  28. data/lib/graphql_rails/model.rb +1 -1
  29. data/lib/graphql_rails/router/route.rb +2 -2
  30. data/lib/graphql_rails/tasks/dump_graphql_schema.rb +14 -28
  31. data/lib/graphql_rails/tasks/dump_graphql_schemas.rb +57 -0
  32. data/lib/graphql_rails/tasks/schema.rake +8 -5
  33. data/lib/graphql_rails/version.rb +1 -1
  34. metadata +11 -8
  35. data/.travis.yml +0 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 27f377528145c3e4c9a6ea37d7bf844531088ecb7b6471ca722f7f84655ca924
4
- data.tar.gz: 3e6b29a745210a0ba453f0c158219013b007e3ef85c006f8eaf5665d5e095659
3
+ metadata.gz: 1d635269612ec07a541e7adebb01a9da7420f74511d8bbc4a30c60cea2eadc3f
4
+ data.tar.gz: 5d8e87e2d4c22bcf679645fa0de355b5c2a1765d357e28ba00560e83279462a0
5
5
  SHA512:
6
- metadata.gz: 2825a1b14839d9fbf8a5958826df14ffae4f9d74c0a4b71015c95d590a9493668b4780727da5b53a6e431397ca399caa3ebb76066e1629d5865660f8566eafb0
7
- data.tar.gz: 3e97009f750ad27ebb66c92696d1cab5aca91dc660f35f340134ceff8b01c5f55cbb70c68247172c2604670cc45e6367deabb7d287ba0e54151d42b8057fb13a
6
+ metadata.gz: 4bdbac43bd69d1ace6bc7b29d6cfba07225950ff9a583f269310eed608fc5554afa934e2e34d4f2001349bdc8385fe4d94b6dc1b8844cb20f5d850276b5c9576
7
+ data.tar.gz: 9bdf1be49c697610f584def6bb016b5bd60455eb813c5b4884b491c39cb7df7c29d277efe56e45e79bc24089c3cf1befa9c178471396fe6eb6ca52c2ec49bffc
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+ on: [push, pull_request]
3
+ jobs:
4
+ specs:
5
+ strategy:
6
+ matrix:
7
+ ruby-version: ['2.6', '2.7', '3.0']
8
+
9
+ runs-on: ubuntu-latest
10
+ env:
11
+ CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
12
+ steps:
13
+ - uses: actions/checkout@v2
14
+ - uses: ruby/setup-ruby@v1
15
+ with:
16
+ ruby-version: ${{ matrix.ruby-version }}
17
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
18
+ - run: bundle exec rake
data/.hound.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  rubocop:
2
2
  config_file: .rubocop.yml
3
- version: 0.91.0
3
+ version: 1.5.2
4
4
  fail_on_violations: true
data/.rubocop.yml CHANGED
@@ -35,6 +35,7 @@ Style/ClassAndModuleChildren:
35
35
  - spec/**/*_spec.rb
36
36
 
37
37
  AllCops:
38
+ NewCops: disable # TODO: enable
38
39
  TargetRubyVersion: 2.7
39
40
  Exclude:
40
41
  - bin/*
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.7.1
1
+ 3.0.1
data/CHANGELOG.md CHANGED
@@ -9,9 +9,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  * Added/Changed/Deprecated/Removed/Fixed/Security: YOUR CHANGE HERE
11
11
 
12
+ ## [2.0.0](2021-12-03)
13
+
14
+ * Added: support for generating multiple schema dumps with `rake graphql_rails:schema:dump`.
15
+ * Added: support for using chainable syntax for input attributes.
16
+ * Changed: changed default `predicate method type from `Boolean` to `Boolean!`
17
+ * Changed: changed error message when trying to paginate not supported types
18
+ * Added: support defining graphql-ruby types as strings.
19
+
20
+ ## [1.2.4](2021-05-05)
21
+
22
+ * Fixed: Dynamic types definition where type A references type B referencing type A.
23
+
24
+ ## [1.2.3](2021-04-12)
25
+
26
+ * Fixed: Total count on paginated resources
27
+
12
28
  ## [1.2.2](2021-02-19)
13
29
 
14
- * Fixed: Incorrect type resolution for required list type fields in model declaration.
30
+ * Fixed: Incorrect type resolution for required list type fields in model declaration.
15
31
 
16
32
  ## [1.2.1](2021-02-17)
17
33
 
data/Gemfile CHANGED
@@ -5,9 +5,9 @@ source 'https://rubygems.org'
5
5
  git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
6
 
7
7
  group :development do
8
- gem 'rubocop', '0.91.0'
9
- gem 'rubocop-performance', '~> 1.8', '>= 1.8.1'
10
- gem 'rubocop-rspec', '~> 1.44', '>= 1.44.1'
8
+ gem 'rubocop', '1.5.2'
9
+ gem 'rubocop-performance'
10
+ gem 'rubocop-rspec'
11
11
  end
12
12
 
13
13
  group :test do
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- graphql_rails (1.2.2)
4
+ graphql_rails (2.0.0)
5
5
  activesupport (>= 4)
6
6
  graphql (~> 1.12, >= 1.12.4)
7
7
 
@@ -63,7 +63,7 @@ GEM
63
63
  minitest (~> 5.1)
64
64
  tzinfo (~> 1.1)
65
65
  zeitwerk (~> 2.2, >= 2.2.2)
66
- ast (2.4.1)
66
+ ast (2.4.2)
67
67
  bson (4.11.0)
68
68
  builder (3.2.4)
69
69
  byebug (11.1.3)
@@ -78,7 +78,7 @@ GEM
78
78
  erubi (1.9.0)
79
79
  globalid (0.4.2)
80
80
  activesupport (>= 4.2.0)
81
- graphql (1.12.4)
81
+ graphql (1.12.10)
82
82
  i18n (1.8.5)
83
83
  concurrent-ruby (~> 1.0)
84
84
  json (2.3.1)
@@ -90,9 +90,11 @@ GEM
90
90
  marcel (0.3.3)
91
91
  mimemagic (~> 0.3.2)
92
92
  method_source (1.0.0)
93
- mimemagic (0.3.5)
93
+ mimemagic (0.3.10)
94
+ nokogiri (~> 1)
95
+ rake
94
96
  mini_mime (1.0.2)
95
- mini_portile2 (2.5.0)
97
+ mini_portile2 (2.6.1)
96
98
  minitest (5.14.2)
97
99
  mongo (2.13.1)
98
100
  bson (>= 4.8.2, < 5.0.0)
@@ -100,11 +102,11 @@ GEM
100
102
  activemodel (>= 5.1, < 6.1)
101
103
  mongo (>= 2.7.0, < 3.0.0)
102
104
  nio4r (2.5.4)
103
- nokogiri (1.11.1)
104
- mini_portile2 (~> 2.5.0)
105
+ nokogiri (1.12.5)
106
+ mini_portile2 (~> 2.6.1)
105
107
  racc (~> 1.4)
106
- parallel (1.19.2)
107
- parser (2.7.2.0)
108
+ parallel (1.20.1)
109
+ parser (3.0.1.1)
108
110
  ast (~> 2.4.1)
109
111
  pry (0.13.1)
110
112
  coderay (~> 1.1)
@@ -144,8 +146,8 @@ GEM
144
146
  thor (>= 0.20.3, < 2.0)
145
147
  rainbow (3.0.0)
146
148
  rake (13.0.1)
147
- regexp_parser (1.8.2)
148
- rexml (3.2.4)
149
+ regexp_parser (2.1.1)
150
+ rexml (3.2.5)
149
151
  rspec (3.10.0)
150
152
  rspec-core (~> 3.10.0)
151
153
  rspec-expectations (~> 3.10.0)
@@ -159,24 +161,24 @@ GEM
159
161
  diff-lcs (>= 1.2.0, < 2.0)
160
162
  rspec-support (~> 3.10.0)
161
163
  rspec-support (3.10.0)
162
- rubocop (0.91.0)
164
+ rubocop (1.5.2)
163
165
  parallel (~> 1.10)
164
- parser (>= 2.7.1.1)
166
+ parser (>= 2.7.1.5)
165
167
  rainbow (>= 2.2.2, < 4.0)
166
- regexp_parser (>= 1.7)
168
+ regexp_parser (>= 1.8, < 3.0)
167
169
  rexml
168
- rubocop-ast (>= 0.4.0, < 1.0)
170
+ rubocop-ast (>= 1.2.0, < 2.0)
169
171
  ruby-progressbar (~> 1.7)
170
172
  unicode-display_width (>= 1.4.0, < 2.0)
171
- rubocop-ast (0.8.0)
172
- parser (>= 2.7.1.5)
173
+ rubocop-ast (1.5.0)
174
+ parser (>= 3.0.1.1)
173
175
  rubocop-performance (1.8.1)
174
176
  rubocop (>= 0.87.0)
175
177
  rubocop-ast (>= 0.4.0)
176
- rubocop-rspec (1.44.1)
177
- rubocop (~> 0.87)
178
- rubocop-ast (>= 0.7.1)
179
- ruby-progressbar (1.10.1)
178
+ rubocop-rspec (2.3.0)
179
+ rubocop (~> 1.0)
180
+ rubocop-ast (>= 1.1.0)
181
+ ruby-progressbar (1.11.0)
180
182
  simplecov (0.19.1)
181
183
  docile (~> 1.1)
182
184
  simplecov-html (~> 0.11)
@@ -211,10 +213,10 @@ DEPENDENCIES
211
213
  rails (~> 6)
212
214
  rake (~> 13.0)
213
215
  rspec (~> 3.0)
214
- rubocop (= 0.91.0)
215
- rubocop-performance (~> 1.8, >= 1.8.1)
216
- rubocop-rspec (~> 1.44, >= 1.44.1)
216
+ rubocop (= 1.5.2)
217
+ rubocop-performance
218
+ rubocop-rspec
217
219
  simplecov
218
220
 
219
221
  BUNDLED WITH
220
- 2.1.4
222
+ 2.2.28
data/docs/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # GraphqlRails
2
2
 
3
- [![Build Status](https://travis-ci.org/samesystem/graphql_rails.svg?branch=master)](https://travis-ci.org/samesystem/graphql_rails)
3
+ ![Build Status](https://github.com/samesystem/graphql_rails/workflows/Ruby/badge.svg?branch=master)
4
4
  [![codecov](https://codecov.io/gh/samesystem/graphql_rails/branch/master/graph/badge.svg)](https://codecov.io/gh/samesystem/graphql_rails)
5
5
  [![Documentation](https://readthedocs.org/projects/ansicolortags/badge/?version=latest)](https://samesystem.github.io/graphql_rails)
6
6
 
@@ -27,8 +27,8 @@ Most commonly you will use `attribute` to make your model methods and attributes
27
27
 
28
28
  Some types can be determined by attribute name, so you can skip this attribute:
29
29
 
30
- * attributes which ends with name `*_id` has `ID` type
31
- * attributes which ends with `?` has `Boolean` type
30
+ * attributes which ends with name `*_id` has `ID!` type
31
+ * attributes which ends with `?` has `Boolean!` type
32
32
  * all other attributes without type are considered to be `String`
33
33
 
34
34
  available types are:
@@ -45,9 +45,9 @@ class User
45
45
  include GraphqlRails::Model
46
46
 
47
47
  graphql do |c|
48
- c.attribute :shop_id # ID type
48
+ c.attribute :shop_id # ID! type
49
49
  c.attribute :full_name # String type
50
- c.attribute :admin? # Boolean type
50
+ c.attribute :admin? # Boolean! type
51
51
  c.attribute :level, type: 'integer'
52
52
  c.attribute :money, type: 'float'
53
53
  end
@@ -72,7 +72,7 @@ end
72
72
  class User
73
73
  include GraphqlRails::Model
74
74
 
75
- graphql.attribute :address, type: AddressType, required: true
75
+ graphql.attribute :address, type: 'AddressType!', required: true
76
76
  end
77
77
  ```
78
78
 
@@ -243,6 +243,24 @@ class User
243
243
  end
244
244
  ```
245
245
 
246
+ ### attribute.with
247
+
248
+ When you want to define some options dynamically, it's quite handy to use "Attribute#with" method:
249
+
250
+ ```ruby
251
+ class User
252
+ include GraphqlRails::Model
253
+
254
+ graphql do |c|
255
+ c.attribute(:shop_id).with(type: 'ID', description: 'references to shop')
256
+ # same as:
257
+ # c.attribute(:shop_id, type: 'ID', description: 'references to shop')
258
+ # also same as:
259
+ # c.attribute(:shop_id).type('ID').description('references to shop')
260
+ end
261
+ end
262
+ ```
263
+
246
264
  ### "attribute" configuration with chainable methods
247
265
 
248
266
  If your attribute definition is complex, you can define attribute in more eye-friendly chainable way with:
@@ -361,6 +379,22 @@ class User
361
379
  end
362
380
  ```
363
381
 
382
+ ### "input.attribute" configuration with chainable methods
383
+
384
+ If your input attribute definition is complex, you can define attribute in more eye-friendly chainable way with:
385
+
386
+ ```ruby
387
+ class User
388
+ include GraphqlRails::Model
389
+
390
+ graphql.input do |c|
391
+ c.attribute(:friends_count)
392
+ .type(:integer!)
393
+ .description('Can not be zero or less')
394
+ end
395
+ end
396
+ ```
397
+
364
398
  #### required type
365
399
 
366
400
  There are few ways how to mark field as required.
@@ -6,24 +6,24 @@ GraphqlRails includes rake task to allow creating schema snapshots easier:
6
6
  rake graphql_rails:schema:dump
7
7
  ```
8
8
 
9
- ## Dumping non default schema
9
+ ## Dumping only selected schema groups
10
10
 
11
- You can have multiple graphql schemas. Read more about this in [routes section](components/routes). In order to generate schema for one of groups, provide optional `name` argument:
11
+ You can specify which schema groups you want to dump. In order to do so, provide groups list as rake task argument and separate group names by comma:
12
12
 
13
13
  ```bash
14
- rake graphql_rails:schema:dump['your_group_name']
14
+ rake graphql_rails:schema:dump['your_group_name, your_group_name2']
15
15
  ```
16
16
 
17
- or using env variable `SCHEMA_GROUP_NAME`:
17
+ You can do this also by using ENV variable `SCHEMA_GROUP_NAME`:
18
18
 
19
19
  ```bash
20
- SCHEMA_GROUP_NAME=your_group_name rake graphql_rails:schema:dump
20
+ SCHEMA_GROUP_NAME="your_group_name, your_group_name2" rake graphql_rails:schema:dump
21
21
  ```
22
22
 
23
- ## Dumping schema in to non default path
23
+ ## Dumping schema in to non default folder
24
24
 
25
- By default schema will be dumped to `spec/fixtures/graphql_schema.graphql` path. If you want different schema path, add `GRAPHQL_SCHEMA_DUMP_PATH` env variable, like this:
25
+ By default schema will be dumped to `spec/fixtures` directory. If you want different schema path, add `GRAPHQL_SCHEMA_DUMP_DIR` env variable, like this:
26
26
 
27
27
  ```bash
28
- GRAPHQL_SCHEMA_DUMP_PATH='path/to/my/schema.graphql' rake graphql_rails:schema:dump
28
+ GRAPHQL_SCHEMA_DUMP_DIR='path/to/graphql/dumps' rake graphql_rails:schema:dump
29
29
  ```
@@ -6,14 +6,14 @@ require 'graphql_rails/attributes/attribute_name_parser'
6
6
  module GraphqlRails
7
7
  module Attributes
8
8
  # contains methods which are shared between various attribute-like classes
9
- # expects `initial_name` and `initial_type` to be defined
9
+ # expects `initial_name` and `type` to be defined
10
10
  module Attributable
11
11
  def field_name
12
12
  attribute_name_parser.field_name
13
13
  end
14
14
 
15
15
  def type_name
16
- @type_name ||= initial_type.to_s
16
+ type.to_s
17
17
  end
18
18
 
19
19
  def name
@@ -23,17 +23,9 @@ module GraphqlRails
23
23
  def required?
24
24
  return @required unless @required.nil?
25
25
 
26
- attribute_name_parser.required? || !initial_type.to_s[/!$/].nil? || initial_type.is_a?(GraphQL::Schema::NonNull)
27
- end
28
-
29
- def required
30
- @required = true
31
- self
32
- end
33
-
34
- def optional
35
- @required = false
36
- self
26
+ (type.nil? && attribute_name_parser.required?) ||
27
+ type.to_s[/!$/].present? ||
28
+ type.is_a?(GraphQL::Schema::NonNull)
37
29
  end
38
30
 
39
31
  def graphql_model
@@ -52,7 +44,7 @@ module GraphqlRails
52
44
 
53
45
  def type_parser
54
46
  @type_parser ||= begin
55
- type_for_parser = initial_type || attribute_name_parser.graphql_type
47
+ type_for_parser = type || attribute_name_parser.graphql_type
56
48
  TypeParser.new(type_for_parser, paginated: paginated?)
57
49
  end
58
50
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'graphql'
4
4
  require 'graphql_rails/attributes/attributable'
5
+ require 'graphql_rails/attributes/attribute_configurable'
5
6
  require 'graphql_rails/input_configurable'
6
7
 
7
8
  module GraphqlRails
@@ -9,47 +10,21 @@ module GraphqlRails
9
10
  # contains info about single graphql attribute
10
11
  class Attribute
11
12
  include Attributable
13
+ include AttributeConfigurable
12
14
  include InputConfigurable
13
15
 
14
16
  attr_reader :attributes
15
17
 
16
- # rubocop:disable Metrics/ParameterLists
17
- def initialize(name, type = nil, description: nil, property: name, required: nil, options: {})
18
- @initial_type = type
18
+ def initialize(name)
19
19
  @initial_name = name
20
- @options = options
21
- @description = description
22
- @property = property.to_s
23
- @required = required
20
+ @property = name.to_s
24
21
  @attributes ||= {}
25
22
  end
26
- # rubocop:enable Metrics/ParameterLists
27
23
 
28
- def type(new_type = nil)
29
- return @initial_type if new_type.nil?
24
+ def property(new_value = NOT_SET)
25
+ return @property if new_value == NOT_SET
30
26
 
31
- @initial_type = new_type
32
- self
33
- end
34
-
35
- def description(new_description = nil)
36
- return @description if new_description.nil?
37
-
38
- @description = new_description
39
- self
40
- end
41
-
42
- def property(new_property = nil)
43
- return @property if new_property.nil?
44
-
45
- @property = new_property.to_s
46
- self
47
- end
48
-
49
- def options(new_options = {})
50
- return @options if new_options.blank?
51
-
52
- @options = new_options
27
+ @property = new_value.to_s
53
28
  self
54
29
  end
55
30
 
@@ -82,7 +57,7 @@ module GraphqlRails
82
57
 
83
58
  protected
84
59
 
85
- attr_reader :initial_type, :initial_name
60
+ attr_reader :initial_name
86
61
 
87
62
  private
88
63
 
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'graphql_rails/attributes/type_parser'
4
+ require 'graphql_rails/attributes/attribute_name_parser'
5
+ require 'graphql_rails/model/build_enum_type'
6
+ require 'graphql_rails/concerns/chainable_options'
7
+ require 'active_support/concern'
8
+
9
+ module GraphqlRails
10
+ module Attributes
11
+ # Allows to set or get various attribute parameters
12
+ module AttributeConfigurable
13
+ extend ActiveSupport::Concern
14
+
15
+ included do
16
+ include GraphqlRails::ChainableOptions
17
+
18
+ chainable_option :description
19
+ chainable_option :options, default: {}
20
+ chainable_option :type
21
+ end
22
+
23
+ def required(new_value = true) # rubocop:disable Style/OptionalBooleanParameter
24
+ @required = new_value
25
+ self
26
+ end
27
+
28
+ def optional(new_value = true) # rubocop:disable Style/OptionalBooleanParameter
29
+ required(!new_value)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -5,12 +5,8 @@ module GraphqlRails
5
5
  # Parses attribute name and can generates graphql scalar type,
6
6
  # grapqhl name and etc. based on that
7
7
  class AttributeNameParser
8
- attr_reader :name
9
-
10
8
  def initialize(original_name, options: {})
11
- name = original_name.to_s
12
- @required = !name['!'].nil?
13
- @name = name.tr('!', '')
9
+ @original_name = original_name.to_s
14
10
  @options = options
15
11
  end
16
12
 
@@ -36,12 +32,16 @@ module GraphqlRails
36
32
  end
37
33
 
38
34
  def required?
39
- @required
35
+ original_name['!'].present? || original_name.end_with?('?')
36
+ end
37
+
38
+ def name
39
+ @name ||= original_name.tr('!', '')
40
40
  end
41
41
 
42
42
  private
43
43
 
44
- attr_reader :options
44
+ attr_reader :options, :original_name
45
45
 
46
46
  def original_format?
47
47
  options[:input_format] == :original || options[:attribute_name_format] == :original
@@ -4,22 +4,19 @@ module GraphqlRails
4
4
  module Attributes
5
5
  # contains info about single graphql input attribute
6
6
  class InputAttribute
7
+ require 'graphql_rails/model/build_enum_type'
7
8
  require_relative './input_type_parser'
8
9
  require_relative './attribute_name_parser'
9
10
  include Attributable
11
+ include AttributeConfigurable
10
12
 
11
- attr_reader :description
13
+ chainable_option :subtype
14
+ chainable_option :enum
12
15
 
13
- # rubocop:disable Metrics/ParameterLists
14
- def initialize(name, type: nil, description: nil, subtype: nil, required: nil, options: {})
16
+ def initialize(name, config:)
17
+ @config = config
15
18
  @initial_name = name
16
- @initial_type = type
17
- @description = description
18
- @options = options
19
- @subtype = subtype
20
- @required = required
21
19
  end
22
- # rubocop:enable Metrics/ParameterLists
23
20
 
24
21
  def input_argument_args
25
22
  type = raw_input_type || input_type_parser.input_type_arg
@@ -37,7 +34,7 @@ module GraphqlRails
37
34
 
38
35
  private
39
36
 
40
- attr_reader :initial_name, :initial_type, :options, :subtype
37
+ attr_reader :initial_name, :config
41
38
 
42
39
  def attribute_name_parser
43
40
  @attribute_name_parser ||= AttributeNameParser.new(
@@ -51,14 +48,23 @@ module GraphqlRails
51
48
 
52
49
  def input_type_parser
53
50
  @input_type_parser ||= begin
54
- initial_parseable_type = initial_type || attribute_name_parser.graphql_type
51
+ initial_parseable_type = type || enum_type || attribute_name_parser.graphql_type
55
52
  InputTypeParser.new(initial_parseable_type, subtype: subtype)
56
53
  end
57
54
  end
58
55
 
56
+ def enum_type
57
+ return if enum.blank?
58
+
59
+ ::GraphqlRails::Model::BuildEnumType.call(
60
+ "#{config.name}_#{initial_name}_enum",
61
+ allowed_values: enum
62
+ )
63
+ end
64
+
59
65
  def raw_input_type
60
- return initial_type if initial_type.is_a?(GraphQL::InputObjectType)
61
- return initial_type.graphql_input_type if initial_type.is_a?(Model::Input)
66
+ return type if type.is_a?(GraphQL::InputObjectType)
67
+ return type.graphql_input_type if type.is_a?(Model::Input)
62
68
  end
63
69
  end
64
70
  end
@@ -15,12 +15,6 @@ module GraphqlRails
15
15
  @subtype = subtype
16
16
  end
17
17
 
18
- def graphql_type
19
- return nil if unparsed_type.nil?
20
-
21
- partly_parsed_type || parsed_type
22
- end
23
-
24
18
  def input_type_arg
25
19
  if list?
26
20
  list_type_arg
@@ -34,7 +28,11 @@ module GraphqlRails
34
28
  attr_reader :unparsed_type, :subtype
35
29
 
36
30
  def unwrapped_type
37
- raw_unwrapped_type || unwrapped_scalar_type || unwrapped_model_input_type || raise_not_supported_type_error
31
+ raw_unwrapped_type ||
32
+ unwrapped_scalar_type ||
33
+ unwrapped_model_input_type ||
34
+ graphql_type_object ||
35
+ raise_not_supported_type_error
38
36
  end
39
37
 
40
38
  def raw_unwrapped_type
@@ -58,25 +58,46 @@ module GraphqlRails
58
58
  end
59
59
 
60
60
  def core_scalar_type?
61
- unwrapped_scalar_type.in?(TYPE_MAPPING.values)
61
+ unwrapped_scalar_type.present?
62
62
  end
63
63
 
64
64
  def graphql_model
65
- type_class = \
66
- if unparsed_type.is_a?(Class) && unparsed_type < GraphqlRails::Model
67
- unparsed_type
68
- else
69
- nullable_inner_name.safe_constantize
70
- end
71
-
72
- return if type_class.nil?
73
- return unless type_class < GraphqlRails::Model
65
+ extract_type_class_if { |type| graphql_model?(type) }
66
+ end
74
67
 
75
- type_class
68
+ def graphql_type_object
69
+ type_object = extract_type_class_if { |type| graphql_type_object?(type) }
70
+ type_object || graphql_model&.graphql&.graphql_type
76
71
  end
77
72
 
78
73
  protected
79
74
 
75
+ def extract_type_class_if
76
+ return unparsed_type if yield(unparsed_type)
77
+
78
+ type_class = nullable_inner_name.safe_constantize
79
+ return type_class if yield(type_class)
80
+
81
+ nil
82
+ end
83
+
84
+ def graphql_model?(type_class)
85
+ type_class.is_a?(Class) && type_class < GraphqlRails::Model
86
+ end
87
+
88
+ def graphql_type_object?(type_class)
89
+ return false unless type_class.is_a?(Class)
90
+
91
+ type_class < GraphQL::Schema::Object || type_class < GraphQL::Schema::Scalar
92
+ end
93
+
94
+ def applicable_graphql_type?(type)
95
+ return false unless type.is_a?(Class)
96
+ return true if type < GraphqlRails::Model
97
+
98
+ false
99
+ end
100
+
80
101
  def unwrap_type(type)
81
102
  unwrappable = type
82
103
  unwrappable = unwrappable.of_type while wrapped_type?(unwrappable)