ar_serializer 1.2.1 → 1.2.3
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 +4 -4
- data/.github/workflows/test.yml +3 -3
- data/Gemfile +2 -1
- data/gemfiles/Gemfile-rails-6 +6 -0
- data/gemfiles/{Gemfile-rails-7-0 → Gemfile-rails-7} +2 -1
- data/gemfiles/{Gemfile-rails-7-1 → Gemfile-rails-8} +3 -2
- data/lib/ar_serializer/field.rb +51 -9
- data/lib/ar_serializer/graphql/parser.rb +4 -4
- data/lib/ar_serializer/graphql/types.rb +101 -172
- data/lib/ar_serializer/type_script.rb +1 -1
- data/lib/ar_serializer/version.rb +1 -1
- data/lib/ar_serializer.rb +2 -0
- metadata +5 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 82a536d1726d005b8fe7606ba04dd252bd169a88686d67b6eb6fdb5f4f2e791d
|
|
4
|
+
data.tar.gz: fe03fd5cc99e41f4b9002fe43351f619e65f8a692049bd0af959432fe301e1d8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 86729d4da8d17051e42dcab42dec965163ed478e738459eed8633942f29ff5262e7691b726d785db4fdcbc2781c286d93d4972b400fa8bbfaf62d4dc8e196b30
|
|
7
|
+
data.tar.gz: 13d2d9d8c85004a4c02cb0988797395a1d2819535c03d8cb753ee0a2882177ea8a8d090cc1902289afc96ca89440a6530a71f840308d8e33ce9983af16203062
|
data/.github/workflows/test.yml
CHANGED
|
@@ -5,11 +5,11 @@ jobs:
|
|
|
5
5
|
strategy:
|
|
6
6
|
fail-fast: false
|
|
7
7
|
matrix:
|
|
8
|
-
ruby: [ '3.
|
|
8
|
+
ruby: [ '3.2', '3.3', '3.4', '4.0' ]
|
|
9
9
|
gemfiles:
|
|
10
10
|
- gemfiles/Gemfile-rails-6
|
|
11
|
-
- gemfiles/Gemfile-rails-7
|
|
12
|
-
- gemfiles/Gemfile-rails-
|
|
11
|
+
- gemfiles/Gemfile-rails-7
|
|
12
|
+
- gemfiles/Gemfile-rails-8
|
|
13
13
|
runs-on: ubuntu-latest
|
|
14
14
|
steps:
|
|
15
15
|
- uses: actions/checkout@v4
|
data/Gemfile
CHANGED
data/gemfiles/Gemfile-rails-6
CHANGED
|
@@ -5,5 +5,11 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
|
|
5
5
|
# Specify your gem's dependencies in ar_serializer.gemspec
|
|
6
6
|
gemspec path: ".."
|
|
7
7
|
|
|
8
|
+
gem "bigdecimal"
|
|
9
|
+
gem "base64"
|
|
10
|
+
gem "mutex_m"
|
|
11
|
+
gem "logger"
|
|
12
|
+
gem "concurrent-ruby", "1.3.4"
|
|
8
13
|
gem "sqlite3", "~> 1.4"
|
|
9
14
|
gem "activerecord", "~> 6.0"
|
|
15
|
+
gem "benchmark"
|
|
@@ -5,5 +5,6 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
|
|
5
5
|
# Specify your gem's dependencies in ar_serializer.gemspec
|
|
6
6
|
gemspec path: ".."
|
|
7
7
|
|
|
8
|
-
gem "sqlite3"
|
|
9
|
-
gem "activerecord", "~>
|
|
8
|
+
gem "sqlite3"
|
|
9
|
+
gem "activerecord", "~> 8.0"
|
|
10
|
+
gem "benchmark"
|
data/lib/ar_serializer/field.rb
CHANGED
|
@@ -4,7 +4,25 @@ require 'set'
|
|
|
4
4
|
|
|
5
5
|
class ArSerializer::Field
|
|
6
6
|
attr_reader :includes, :preloaders, :data_block, :only, :except, :scoped_access, :order_column, :permission, :fallback
|
|
7
|
-
def initialize
|
|
7
|
+
def initialize(
|
|
8
|
+
klass,
|
|
9
|
+
name,
|
|
10
|
+
includes: nil,
|
|
11
|
+
preloaders: [],
|
|
12
|
+
data_block:,
|
|
13
|
+
only: nil,
|
|
14
|
+
except: nil,
|
|
15
|
+
private: false,
|
|
16
|
+
scoped_access: nil,
|
|
17
|
+
permission: nil,
|
|
18
|
+
fallback: nil,
|
|
19
|
+
order_column: nil,
|
|
20
|
+
orderable: nil,
|
|
21
|
+
type: nil,
|
|
22
|
+
ts_type: nil,
|
|
23
|
+
params_type: nil,
|
|
24
|
+
ts_params_type: nil
|
|
25
|
+
)
|
|
8
26
|
@klass = klass
|
|
9
27
|
@name = name
|
|
10
28
|
@includes = includes
|
|
@@ -18,8 +36,8 @@ class ArSerializer::Field
|
|
|
18
36
|
@data_block = data_block
|
|
19
37
|
@order_column = order_column
|
|
20
38
|
@orderable = orderable
|
|
21
|
-
@type = type
|
|
22
|
-
@params_type = params_type
|
|
39
|
+
@type = ts_type ? ArSerializer::TSType.new(ts_type) : type
|
|
40
|
+
@params_type = ts_params_type ? ArSerializer::TSType.new(ts_params_type) : params_type
|
|
23
41
|
end
|
|
24
42
|
|
|
25
43
|
def orderable?
|
|
@@ -50,8 +68,8 @@ class ArSerializer::Field
|
|
|
50
68
|
splat.call type
|
|
51
69
|
end
|
|
52
70
|
|
|
53
|
-
def
|
|
54
|
-
return @params_type.is_a?(Proc) ? @params_type.call : @params_type if @params_type
|
|
71
|
+
def arguments_type
|
|
72
|
+
return ArSerializer::GraphQL::TypeClass.from(@params_type.is_a?(Proc) ? @params_type.call : @params_type) if @params_type
|
|
55
73
|
@preloaders.size
|
|
56
74
|
@data_block.parameters
|
|
57
75
|
parameters_list = [@data_block.parameters.drop(@preloaders.size + 1)]
|
|
@@ -79,8 +97,8 @@ class ArSerializer::Field
|
|
|
79
97
|
end
|
|
80
98
|
end
|
|
81
99
|
end
|
|
82
|
-
return :any if any && arguments.empty?
|
|
83
|
-
arguments.
|
|
100
|
+
return ArSerializer::GraphQL::TypeClass.from(:any) if any && arguments.empty?
|
|
101
|
+
hash_args = arguments.to_h do |key, req|
|
|
84
102
|
camelcase = key.to_s.camelcase :lower
|
|
85
103
|
type = (
|
|
86
104
|
case key
|
|
@@ -94,7 +112,8 @@ class ArSerializer::Field
|
|
|
94
112
|
end
|
|
95
113
|
)
|
|
96
114
|
[req ? camelcase : "#{camelcase}?", type]
|
|
97
|
-
end
|
|
115
|
+
end
|
|
116
|
+
ArSerializer::GraphQL::TypeClass.from(hash_args)
|
|
98
117
|
end
|
|
99
118
|
|
|
100
119
|
def validate_attributes(attributes)
|
|
@@ -145,7 +164,26 @@ class ArSerializer::Field
|
|
|
145
164
|
}[attr_type.type]
|
|
146
165
|
end
|
|
147
166
|
|
|
148
|
-
def self.create(
|
|
167
|
+
def self.create(
|
|
168
|
+
klass,
|
|
169
|
+
name,
|
|
170
|
+
ts_type: nil,
|
|
171
|
+
type: nil,
|
|
172
|
+
ts_params_type: nil,
|
|
173
|
+
params_type: nil,
|
|
174
|
+
count_of: nil,
|
|
175
|
+
includes: nil,
|
|
176
|
+
preload: nil,
|
|
177
|
+
only: nil,
|
|
178
|
+
except: nil,
|
|
179
|
+
private: nil,
|
|
180
|
+
scoped_access: nil,
|
|
181
|
+
permission: nil,
|
|
182
|
+
fallback: nil,
|
|
183
|
+
order_column: nil,
|
|
184
|
+
orderable: nil,
|
|
185
|
+
&data_block
|
|
186
|
+
)
|
|
149
187
|
name = name.to_s
|
|
150
188
|
if count_of
|
|
151
189
|
if includes || preload || data_block || only || except || order_column || orderable || scoped_access != nil || fallback
|
|
@@ -153,6 +191,10 @@ class ArSerializer::Field
|
|
|
153
191
|
end
|
|
154
192
|
return count_field klass, name, count_of, permission: permission
|
|
155
193
|
end
|
|
194
|
+
|
|
195
|
+
type = ArSerializer::TSType.new(ts_type) if ts_type
|
|
196
|
+
params_type = ArSerializer::TSType.new(ts_params_type) if ts_params_type
|
|
197
|
+
|
|
156
198
|
association = klass.reflect_on_association name.underscore if klass.respond_to? :reflect_on_association
|
|
157
199
|
if association
|
|
158
200
|
if association.collection?
|
|
@@ -73,7 +73,7 @@ class ArSerializer::GraphQL::Parser
|
|
|
73
73
|
end
|
|
74
74
|
|
|
75
75
|
def parse_name
|
|
76
|
-
name = ''
|
|
76
|
+
name = +''
|
|
77
77
|
name << chars.shift while chars.first && chars.first =~ /[a-zA-Z0-9_]/
|
|
78
78
|
name unless name.empty?
|
|
79
79
|
end
|
|
@@ -94,7 +94,7 @@ class ArSerializer::GraphQL::Parser
|
|
|
94
94
|
case chars.first
|
|
95
95
|
when '"'
|
|
96
96
|
chars.shift
|
|
97
|
-
s = ''
|
|
97
|
+
s = +''
|
|
98
98
|
loop do
|
|
99
99
|
if chars.first == '\\'
|
|
100
100
|
s << chars.shift
|
|
@@ -132,7 +132,7 @@ class ArSerializer::GraphQL::Parser
|
|
|
132
132
|
name = parse_name
|
|
133
133
|
variables[name]
|
|
134
134
|
when /[0-9+\-]/
|
|
135
|
-
s = ''
|
|
135
|
+
s = +''
|
|
136
136
|
s << chars.shift while chars.first.match?(/[0-9.e+\-]/)
|
|
137
137
|
s.match?(/\.|e/) ? s.to_f : s.to_i
|
|
138
138
|
when /[a-zA-Z]/
|
|
@@ -216,7 +216,7 @@ class ArSerializer::GraphQL::Parser
|
|
|
216
216
|
def parse_definition
|
|
217
217
|
type = parse_name
|
|
218
218
|
consume_blank
|
|
219
|
-
args_text = ''
|
|
219
|
+
args_text = +''
|
|
220
220
|
if type
|
|
221
221
|
args_text << chars.shift while chars.first && chars.first != '{'
|
|
222
222
|
end
|
|
@@ -5,7 +5,7 @@ module ArSerializer::GraphQL
|
|
|
5
5
|
def initialize(name, type)
|
|
6
6
|
@optional = name.to_s.end_with? '?' # TODO: refactor
|
|
7
7
|
@name = name.to_s.delete '?'
|
|
8
|
-
@type =
|
|
8
|
+
@type = type
|
|
9
9
|
end
|
|
10
10
|
serializer_field :name
|
|
11
11
|
serializer_field :type, except: :fields
|
|
@@ -22,8 +22,10 @@ module ArSerializer::GraphQL
|
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
def args
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
arguments = field.arguments_type
|
|
26
|
+
return [] unless arguments.is_a?(HashTypeClass)
|
|
27
|
+
|
|
28
|
+
arguments.type.map do |key, type|
|
|
27
29
|
ArgClass.new key, type
|
|
28
30
|
end
|
|
29
31
|
end
|
|
@@ -33,24 +35,26 @@ module ArSerializer::GraphQL
|
|
|
33
35
|
end
|
|
34
36
|
|
|
35
37
|
def collect_types(types)
|
|
36
|
-
|
|
37
|
-
args.each { |arg| arg.type.collect_types types }
|
|
38
|
+
field.arguments_type.collect_types types
|
|
38
39
|
type.collect_types types
|
|
39
40
|
end
|
|
40
41
|
|
|
41
42
|
def args_required?
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
arguments_type = field.arguments_type
|
|
44
|
+
case arguments_type
|
|
45
|
+
when TSTypeClass
|
|
46
|
+
true
|
|
47
|
+
when HashTypeClass
|
|
48
|
+
arguments_type.type.any? do |k, v|
|
|
49
|
+
!k.end_with?('?') && !v.is_a?(OptionalTypeClass)
|
|
50
|
+
end
|
|
51
|
+
else
|
|
52
|
+
false
|
|
45
53
|
end
|
|
46
54
|
end
|
|
47
55
|
|
|
48
56
|
def args_ts_type
|
|
49
|
-
|
|
50
|
-
arg_types = field.arguments.map do |key, type|
|
|
51
|
-
"#{key}: #{TypeClass.from(type).ts_type}"
|
|
52
|
-
end
|
|
53
|
-
"{ #{arg_types.join '; '} }"
|
|
57
|
+
field.arguments_type.ts_type
|
|
54
58
|
end
|
|
55
59
|
|
|
56
60
|
serializer_field :name, :args
|
|
@@ -92,46 +96,16 @@ module ArSerializer::GraphQL
|
|
|
92
96
|
|
|
93
97
|
class TypeClass
|
|
94
98
|
include ::ArSerializer::Serializable
|
|
95
|
-
attr_reader :type
|
|
96
|
-
def initialize(type
|
|
99
|
+
attr_reader :type
|
|
100
|
+
def initialize(type)
|
|
97
101
|
@type = type
|
|
98
|
-
@only = only
|
|
99
|
-
@except = except
|
|
100
|
-
validate!
|
|
101
102
|
end
|
|
102
103
|
|
|
103
104
|
class InvalidType < StandardError; end
|
|
104
105
|
|
|
105
|
-
def validate!
|
|
106
|
-
valid_symbols = %i[number int float string boolean any unknown]
|
|
107
|
-
invalids = []
|
|
108
|
-
recursive_validate = lambda do |t|
|
|
109
|
-
case t
|
|
110
|
-
when Array
|
|
111
|
-
t.each { |v| recursive_validate.call v }
|
|
112
|
-
when Hash
|
|
113
|
-
t.each_value { |v| recursive_validate.call v }
|
|
114
|
-
when String, Numeric, true, false, nil
|
|
115
|
-
return
|
|
116
|
-
when Class
|
|
117
|
-
invalids << t unless t.ancestors.include? ArSerializer::Serializable
|
|
118
|
-
when Symbol
|
|
119
|
-
invalids << t unless valid_symbols.include? t.to_s.gsub(/\?$/, '').to_sym
|
|
120
|
-
else
|
|
121
|
-
invalids << t
|
|
122
|
-
end
|
|
123
|
-
end
|
|
124
|
-
recursive_validate.call type
|
|
125
|
-
return if invalids.empty?
|
|
126
|
-
message = "Valid types are String, Numeric, Hash, Array, ArSerializer::Serializable, true, false, nil and Symbol#{valid_symbols}"
|
|
127
|
-
raise InvalidType, "Invalid type: #{invalids.map(&:inspect).join(', ')}. #{message}"
|
|
128
|
-
end
|
|
129
|
-
|
|
130
106
|
def collect_types(types); end
|
|
131
107
|
|
|
132
|
-
def description
|
|
133
|
-
ts_type
|
|
134
|
-
end
|
|
108
|
+
def description = ts_type
|
|
135
109
|
|
|
136
110
|
def name; end
|
|
137
111
|
|
|
@@ -139,8 +113,6 @@ module ArSerializer::GraphQL
|
|
|
139
113
|
|
|
140
114
|
def fields; end
|
|
141
115
|
|
|
142
|
-
def sample; end
|
|
143
|
-
|
|
144
116
|
def ts_type; end
|
|
145
117
|
|
|
146
118
|
def association_type; end
|
|
@@ -154,35 +126,59 @@ module ArSerializer::GraphQL
|
|
|
154
126
|
|
|
155
127
|
def self.from(type, only = nil, except = nil)
|
|
156
128
|
type = [type[0...-1].to_sym, nil] if type.is_a?(Symbol) && type.to_s.end_with?('?')
|
|
157
|
-
type = [type[0...-1], nil] if type.is_a?(String) && type.end_with?('?')
|
|
129
|
+
type = [type[0...-1], nil] if type.is_a?(String) && type.end_with?('?') # ??
|
|
158
130
|
case type
|
|
159
131
|
when Class
|
|
132
|
+
raise InvalidType, "#{type} must include ArSerializer::Serializable" unless type.ancestors.include? ArSerializer::Serializable
|
|
133
|
+
|
|
160
134
|
SerializableTypeClass.new type, only, except
|
|
161
|
-
when
|
|
135
|
+
when :number, :int, :float, :string, :boolean, :any, :unknown
|
|
136
|
+
ScalarTypeClass.new type
|
|
137
|
+
when String, Numeric, true, false, nil
|
|
162
138
|
ScalarTypeClass.new type
|
|
163
139
|
when Array
|
|
164
140
|
if type.size == 1
|
|
165
|
-
ListTypeClass.new type.first, only, except
|
|
141
|
+
ListTypeClass.new from(type.first, only, except)
|
|
166
142
|
elsif type.size == 2 && type.last.nil?
|
|
167
|
-
OptionalTypeClass.new type, only, except
|
|
143
|
+
OptionalTypeClass.new from(type.first, only, except)
|
|
168
144
|
else
|
|
169
|
-
OrTypeClass.new type, only, except
|
|
145
|
+
OrTypeClass.new type.map {|v| from(v, only, except) }
|
|
170
146
|
end
|
|
171
147
|
when Hash
|
|
172
|
-
HashTypeClass.new type, only, except
|
|
148
|
+
HashTypeClass.new type.transform_values {|v| from(v, only, except) }
|
|
149
|
+
when ArSerializer::TSType
|
|
150
|
+
TSTypeClass.new type.type
|
|
151
|
+
else
|
|
152
|
+
raise InvalidType, "Invalid type: #{type}"
|
|
173
153
|
end
|
|
174
154
|
end
|
|
175
155
|
end
|
|
176
156
|
|
|
177
|
-
class
|
|
157
|
+
class TSTypeClass < TypeClass
|
|
178
158
|
def initialize(type)
|
|
179
159
|
@type = type
|
|
180
160
|
end
|
|
181
161
|
|
|
182
|
-
def kind
|
|
183
|
-
|
|
162
|
+
def kind = 'SCALAR'
|
|
163
|
+
|
|
164
|
+
def name = :other
|
|
165
|
+
|
|
166
|
+
def collect_types(types)
|
|
167
|
+
types[:other] = true
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def gql_type = 'SCALAR'
|
|
171
|
+
|
|
172
|
+
def ts_type = @type
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
class ScalarTypeClass < TypeClass
|
|
176
|
+
def initialize(type)
|
|
177
|
+
@type = type
|
|
184
178
|
end
|
|
185
179
|
|
|
180
|
+
def kind = 'SCALAR'
|
|
181
|
+
|
|
186
182
|
def name
|
|
187
183
|
case type
|
|
188
184
|
when String, :string
|
|
@@ -206,24 +202,7 @@ module ArSerializer::GraphQL
|
|
|
206
202
|
types[name] = true
|
|
207
203
|
end
|
|
208
204
|
|
|
209
|
-
def gql_type
|
|
210
|
-
type
|
|
211
|
-
end
|
|
212
|
-
|
|
213
|
-
def sample
|
|
214
|
-
case ts_type
|
|
215
|
-
when 'number'
|
|
216
|
-
0
|
|
217
|
-
when 'string'
|
|
218
|
-
''
|
|
219
|
-
when 'boolean'
|
|
220
|
-
true
|
|
221
|
-
when 'any', 'unknown'
|
|
222
|
-
nil
|
|
223
|
-
else
|
|
224
|
-
type
|
|
225
|
-
end
|
|
226
|
-
end
|
|
205
|
+
def gql_type = type
|
|
227
206
|
|
|
228
207
|
def ts_type
|
|
229
208
|
case type
|
|
@@ -240,49 +219,53 @@ module ArSerializer::GraphQL
|
|
|
240
219
|
end
|
|
241
220
|
|
|
242
221
|
class HashTypeClass < TypeClass
|
|
243
|
-
def kind
|
|
244
|
-
'SCALAR'
|
|
245
|
-
end
|
|
222
|
+
def kind = 'SCALAR'
|
|
246
223
|
|
|
247
|
-
def name
|
|
248
|
-
:other
|
|
249
|
-
end
|
|
224
|
+
def name = :other
|
|
250
225
|
|
|
251
226
|
def collect_types(types)
|
|
252
227
|
types[:other] = true
|
|
253
|
-
type.values.
|
|
254
|
-
|
|
228
|
+
type.values.each do |v|
|
|
229
|
+
v.collect_types(types)
|
|
255
230
|
end
|
|
256
231
|
end
|
|
257
232
|
|
|
258
233
|
def association_type
|
|
259
234
|
type.values.each do |v|
|
|
260
|
-
t =
|
|
235
|
+
t = v.association_type
|
|
261
236
|
return t if t
|
|
262
237
|
end
|
|
263
238
|
nil
|
|
264
239
|
end
|
|
265
240
|
|
|
266
|
-
def gql_type
|
|
267
|
-
'OBJECT'
|
|
268
|
-
end
|
|
269
|
-
|
|
270
|
-
def sample
|
|
271
|
-
type.reject { |k| k.to_s.end_with? '?' }.transform_values do |v|
|
|
272
|
-
TypeClass.from(v).sample
|
|
273
|
-
end
|
|
274
|
-
end
|
|
241
|
+
def gql_type = 'OBJECT'
|
|
275
242
|
|
|
276
243
|
def ts_type
|
|
244
|
+
return 'Record<string, never>' if type.empty?
|
|
245
|
+
|
|
277
246
|
fields = type.map do |key, value|
|
|
278
247
|
k = key.to_s == '*' ? '[key: string]' : key
|
|
279
|
-
"#{k}: #{
|
|
248
|
+
"#{k}: #{value.ts_type}"
|
|
280
249
|
end
|
|
281
250
|
"{ #{fields.join('; ')} }"
|
|
282
251
|
end
|
|
252
|
+
|
|
253
|
+
def to_gql_args
|
|
254
|
+
type.map do |key, type|
|
|
255
|
+
ArgClass.new key, type
|
|
256
|
+
end
|
|
257
|
+
end
|
|
283
258
|
end
|
|
284
259
|
|
|
285
260
|
class SerializableTypeClass < TypeClass
|
|
261
|
+
attr_reader :only, :except
|
|
262
|
+
|
|
263
|
+
def initialize(type, only = nil, except = nil)
|
|
264
|
+
super type
|
|
265
|
+
@only = only
|
|
266
|
+
@except = except
|
|
267
|
+
end
|
|
268
|
+
|
|
286
269
|
def field_only
|
|
287
270
|
[*only].map(&:to_s)
|
|
288
271
|
end
|
|
@@ -291,9 +274,7 @@ module ArSerializer::GraphQL
|
|
|
291
274
|
[*except].map(&:to_s)
|
|
292
275
|
end
|
|
293
276
|
|
|
294
|
-
def kind
|
|
295
|
-
'OBJECT'
|
|
296
|
-
end
|
|
277
|
+
def kind = 'OBJECT'
|
|
297
278
|
|
|
298
279
|
def name
|
|
299
280
|
name_segments = [type.name.delete(':')]
|
|
@@ -322,17 +303,11 @@ module ArSerializer::GraphQL
|
|
|
322
303
|
fields.each { |field| field.collect_types types }
|
|
323
304
|
end
|
|
324
305
|
|
|
325
|
-
def association_type
|
|
326
|
-
self
|
|
327
|
-
end
|
|
306
|
+
def association_type = self
|
|
328
307
|
|
|
329
|
-
def gql_type
|
|
330
|
-
name
|
|
331
|
-
end
|
|
308
|
+
def gql_type = name
|
|
332
309
|
|
|
333
|
-
def ts_type
|
|
334
|
-
"Type#{name}"
|
|
335
|
-
end
|
|
310
|
+
def ts_type = "Type#{name}"
|
|
336
311
|
|
|
337
312
|
def eql?(t)
|
|
338
313
|
self.class == t.class && self.compare_elements == t.compare_elements
|
|
@@ -352,101 +327,55 @@ module ArSerializer::GraphQL
|
|
|
352
327
|
end
|
|
353
328
|
|
|
354
329
|
class OptionalTypeClass < TypeClass
|
|
355
|
-
def kind
|
|
356
|
-
of_type.kind
|
|
357
|
-
end
|
|
330
|
+
def kind = type.kind
|
|
358
331
|
|
|
359
|
-
def name
|
|
360
|
-
of_type.name
|
|
361
|
-
end
|
|
332
|
+
def name = type.name
|
|
362
333
|
|
|
363
|
-
def of_type
|
|
364
|
-
TypeClass.from type.first, only, except
|
|
365
|
-
end
|
|
334
|
+
def of_type = type
|
|
366
335
|
|
|
367
|
-
def association_type
|
|
368
|
-
of_type.association_type
|
|
369
|
-
end
|
|
336
|
+
def association_type = type.association_type
|
|
370
337
|
|
|
371
338
|
def collect_types(types)
|
|
372
|
-
|
|
339
|
+
type.collect_types types
|
|
373
340
|
end
|
|
374
341
|
|
|
375
|
-
def gql_type
|
|
376
|
-
of_type.gql_type
|
|
377
|
-
end
|
|
342
|
+
def gql_type = type.gql_type
|
|
378
343
|
|
|
379
|
-
def
|
|
380
|
-
nil
|
|
381
|
-
end
|
|
382
|
-
|
|
383
|
-
def ts_type
|
|
384
|
-
"(#{of_type.ts_type} | null)"
|
|
385
|
-
end
|
|
344
|
+
def ts_type = "(#{type.ts_type} | null)"
|
|
386
345
|
end
|
|
387
346
|
|
|
388
347
|
class OrTypeClass < TypeClass
|
|
389
|
-
def kind
|
|
390
|
-
'OBJECT'
|
|
391
|
-
end
|
|
348
|
+
def kind = 'OBJECT'
|
|
392
349
|
|
|
393
|
-
def name
|
|
394
|
-
:other
|
|
395
|
-
end
|
|
350
|
+
def name = :other
|
|
396
351
|
|
|
397
|
-
def of_types
|
|
398
|
-
type.map { |t| TypeClass.from t, only, except }
|
|
399
|
-
end
|
|
352
|
+
def of_types = type
|
|
400
353
|
|
|
401
354
|
def collect_types(types)
|
|
402
355
|
types[:other] = true
|
|
403
|
-
|
|
404
|
-
end
|
|
405
|
-
|
|
406
|
-
def gql_type
|
|
407
|
-
kind
|
|
356
|
+
type.map { |t| t.collect_types types }
|
|
408
357
|
end
|
|
409
358
|
|
|
410
|
-
def
|
|
411
|
-
of_types.first.sample
|
|
412
|
-
end
|
|
359
|
+
def gql_type = kind
|
|
413
360
|
|
|
414
|
-
def ts_type
|
|
415
|
-
'(' + of_types.map(&:ts_type).join(' | ') + ')'
|
|
416
|
-
end
|
|
361
|
+
def ts_type = "(#{type.map(&:ts_type).join(' | ')})"
|
|
417
362
|
end
|
|
418
363
|
|
|
419
364
|
class ListTypeClass < TypeClass
|
|
420
|
-
def kind
|
|
421
|
-
'LIST'
|
|
422
|
-
end
|
|
365
|
+
def kind = 'LIST'
|
|
423
366
|
|
|
424
|
-
def name
|
|
425
|
-
'LIST'
|
|
426
|
-
end
|
|
367
|
+
def name = 'LIST'
|
|
427
368
|
|
|
428
|
-
def of_type
|
|
429
|
-
TypeClass.from type, only, except
|
|
430
|
-
end
|
|
369
|
+
def of_type = type
|
|
431
370
|
|
|
432
371
|
def collect_types(types)
|
|
433
|
-
|
|
434
|
-
end
|
|
435
|
-
|
|
436
|
-
def association_type
|
|
437
|
-
of_type.association_type
|
|
372
|
+
type.collect_types types
|
|
438
373
|
end
|
|
439
374
|
|
|
440
|
-
def
|
|
441
|
-
"[#{of_type.gql_type}]"
|
|
442
|
-
end
|
|
375
|
+
def association_type = type.association_type
|
|
443
376
|
|
|
444
|
-
def
|
|
445
|
-
[]
|
|
446
|
-
end
|
|
377
|
+
def gql_type = "[#{type.gql_type}]"
|
|
447
378
|
|
|
448
|
-
def ts_type
|
|
449
|
-
"(#{of_type.ts_type} [])"
|
|
450
|
-
end
|
|
379
|
+
def ts_type = "(#{type.ts_type} [])"
|
|
451
380
|
end
|
|
452
381
|
end
|
|
@@ -16,7 +16,7 @@ module ArSerializer::TypeScript
|
|
|
16
16
|
field_definitions = type.fields.map do |field|
|
|
17
17
|
association_type = field.type.association_type
|
|
18
18
|
query_type = "Type#{association_type.name}Query" if association_type
|
|
19
|
-
params_type = field.args_ts_type
|
|
19
|
+
params_type = field.args_ts_type
|
|
20
20
|
params_required = field.args_required?
|
|
21
21
|
attrs = []
|
|
22
22
|
attrs << "query?: #{query_type}" if query_type
|
data/lib/ar_serializer.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ar_serializer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.2.
|
|
4
|
+
version: 1.2.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- tompng
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: activerecord
|
|
@@ -112,8 +111,8 @@ files:
|
|
|
112
111
|
- bin/console
|
|
113
112
|
- bin/setup
|
|
114
113
|
- gemfiles/Gemfile-rails-6
|
|
115
|
-
- gemfiles/Gemfile-rails-7
|
|
116
|
-
- gemfiles/Gemfile-rails-
|
|
114
|
+
- gemfiles/Gemfile-rails-7
|
|
115
|
+
- gemfiles/Gemfile-rails-8
|
|
117
116
|
- lib/ar_serializer.rb
|
|
118
117
|
- lib/ar_serializer/error.rb
|
|
119
118
|
- lib/ar_serializer/field.rb
|
|
@@ -127,7 +126,6 @@ homepage: https://github.com/tompng/ar_serializer
|
|
|
127
126
|
licenses:
|
|
128
127
|
- MIT
|
|
129
128
|
metadata: {}
|
|
130
|
-
post_install_message:
|
|
131
129
|
rdoc_options: []
|
|
132
130
|
require_paths:
|
|
133
131
|
- lib
|
|
@@ -142,8 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
142
140
|
- !ruby/object:Gem::Version
|
|
143
141
|
version: '0'
|
|
144
142
|
requirements: []
|
|
145
|
-
rubygems_version:
|
|
146
|
-
signing_key:
|
|
143
|
+
rubygems_version: 4.0.3
|
|
147
144
|
specification_version: 4
|
|
148
145
|
summary: ActiveRecord serializer, avoid N+1
|
|
149
146
|
test_files: []
|