graphiti 1.0.rc.21 → 1.0.rc.22
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.standard.yml +14 -0
- data/.travis.yml +31 -2
- data/Appraisals +16 -10
- data/Gemfile +5 -5
- data/Guardfile +2 -2
- data/README.md +1 -1
- data/Rakefile +4 -4
- data/exe/graphiti +1 -1
- data/gemfiles/rails_4.gemfile +0 -1
- data/gemfiles/rails_5.gemfile +0 -1
- data/gemfiles/rails_6.gemfile +19 -0
- data/graphiti.gemspec +15 -14
- data/lib/generators/graphiti/api_test_generator.rb +16 -16
- data/lib/generators/graphiti/generator_mixin.rb +7 -7
- data/lib/generators/graphiti/install_generator.rb +19 -19
- data/lib/generators/graphiti/resource_generator.rb +19 -19
- data/lib/generators/graphiti/resource_test_generator.rb +10 -10
- data/lib/graphiti.rb +24 -26
- data/lib/graphiti/adapters/abstract.rb +25 -39
- data/lib/graphiti/adapters/active_record.rb +43 -49
- data/lib/graphiti/adapters/active_record/many_to_many_sideload.rb +33 -11
- data/lib/graphiti/adapters/graphiti_api.rb +16 -16
- data/lib/graphiti/adapters/null.rb +0 -12
- data/lib/graphiti/cli.rb +7 -7
- data/lib/graphiti/configuration.rb +14 -15
- data/lib/graphiti/context.rb +1 -1
- data/lib/graphiti/debugger.rb +22 -26
- data/lib/graphiti/delegates/pagination.rb +9 -9
- data/lib/graphiti/deserializer.rb +7 -9
- data/lib/graphiti/errors.rb +119 -119
- data/lib/graphiti/extensions/boolean_attribute.rb +1 -1
- data/lib/graphiti/extensions/extra_attribute.rb +1 -1
- data/lib/graphiti/extensions/temp_id.rb +1 -1
- data/lib/graphiti/filter_operators.rb +6 -0
- data/lib/graphiti/hash_renderer.rb +15 -15
- data/lib/graphiti/jsonapi_serializable_ext.rb +1 -1
- data/lib/graphiti/query.rb +35 -35
- data/lib/graphiti/railtie.rb +14 -10
- data/lib/graphiti/renderer.rb +2 -2
- data/lib/graphiti/resource.rb +4 -6
- data/lib/graphiti/resource/configuration.rb +9 -13
- data/lib/graphiti/resource/documentation.rb +4 -2
- data/lib/graphiti/resource/dsl.rb +21 -25
- data/lib/graphiti/resource/interface.rb +3 -3
- data/lib/graphiti/resource/links.rb +14 -14
- data/lib/graphiti/resource/persistence.rb +9 -9
- data/lib/graphiti/resource/polymorphism.rb +3 -3
- data/lib/graphiti/resource/remote.rb +3 -3
- data/lib/graphiti/resource/sideloading.rb +5 -5
- data/lib/graphiti/resource_proxy.rb +12 -12
- data/lib/graphiti/schema.rb +27 -26
- data/lib/graphiti/schema_diff.rb +10 -10
- data/lib/graphiti/scope.rb +12 -16
- data/lib/graphiti/scoping/base.rb +5 -5
- data/lib/graphiti/scoping/default_filter.rb +1 -1
- data/lib/graphiti/scoping/filter.rb +15 -24
- data/lib/graphiti/scoping/filterable.rb +5 -5
- data/lib/graphiti/scoping/paginate.rb +1 -1
- data/lib/graphiti/scoping/sort.rb +7 -7
- data/lib/graphiti/serializer.rb +8 -4
- data/lib/graphiti/sideload.rb +23 -39
- data/lib/graphiti/sideload/belongs_to.rb +2 -2
- data/lib/graphiti/sideload/has_many.rb +1 -1
- data/lib/graphiti/sideload/many_to_many.rb +18 -2
- data/lib/graphiti/sideload/polymorphic_belongs_to.rb +14 -8
- data/lib/graphiti/stats/dsl.rb +7 -1
- data/lib/graphiti/tasks.rb +10 -10
- data/lib/graphiti/types.rb +98 -98
- data/lib/graphiti/util/attribute_check.rb +1 -1
- data/lib/graphiti/util/class.rb +3 -3
- data/lib/graphiti/util/field_params.rb +1 -1
- data/lib/graphiti/util/hash.rb +19 -1
- data/lib/graphiti/util/link.rb +15 -19
- data/lib/graphiti/util/persistence.rb +17 -22
- data/lib/graphiti/util/relationship_payload.rb +2 -2
- data/lib/graphiti/util/remote_params.rb +12 -12
- data/lib/graphiti/util/remote_serializer.rb +2 -2
- data/lib/graphiti/util/serializer_attributes.rb +20 -25
- data/lib/graphiti/util/serializer_relationships.rb +17 -18
- data/lib/graphiti/util/sideload.rb +1 -1
- data/lib/graphiti/util/transaction_hooks_recorder.rb +3 -2
- data/lib/graphiti/version.rb +1 -1
- metadata +22 -6
@@ -18,6 +18,7 @@ module Graphiti
|
|
18
18
|
desc ||= ::I18n.t :description,
|
19
19
|
scope: i18n_type_scope,
|
20
20
|
default: nil
|
21
|
+
desc
|
21
22
|
end
|
22
23
|
end
|
23
24
|
|
@@ -43,13 +44,14 @@ module Graphiti
|
|
43
44
|
desc ||= ::I18n.t :description,
|
44
45
|
scope: [*i18n_resource_scope, field_type, name],
|
45
46
|
default: nil
|
47
|
+
desc
|
46
48
|
end
|
47
49
|
end
|
48
50
|
|
49
51
|
private
|
50
52
|
|
51
53
|
def underscored_resource_name
|
52
|
-
|
54
|
+
name.gsub(/Resource$/, "").underscore
|
53
55
|
end
|
54
56
|
|
55
57
|
def i18n_resource_scope
|
@@ -62,4 +64,4 @@ module Graphiti
|
|
62
64
|
end
|
63
65
|
end
|
64
66
|
end
|
65
|
-
end
|
67
|
+
end
|
@@ -8,7 +8,7 @@ module Graphiti
|
|
8
8
|
opts = args.extract_options!
|
9
9
|
type_override = args[0]
|
10
10
|
|
11
|
-
if att = get_attr(name, :filterable, raise_error: :only_unsupported)
|
11
|
+
if (att = get_attr(name, :filterable, raise_error: :only_unsupported))
|
12
12
|
aliases = [name, opts[:aliases]].flatten.compact
|
13
13
|
operators = FilterOperators.build(self, att[:type], opts, &blk)
|
14
14
|
|
@@ -26,15 +26,13 @@ module Graphiti
|
|
26
26
|
single: !!opts[:single],
|
27
27
|
dependencies: opts[:dependent],
|
28
28
|
required: required,
|
29
|
-
operators: operators.to_hash
|
29
|
+
operators: operators.to_hash,
|
30
30
|
}
|
31
|
+
elsif (type = args[0])
|
32
|
+
attribute name, type, only: [:filterable]
|
33
|
+
filter(name, opts, &blk)
|
31
34
|
else
|
32
|
-
|
33
|
-
attribute name, type, only: [:filterable]
|
34
|
-
filter(name, opts, &blk)
|
35
|
-
else
|
36
|
-
raise Errors::ImplicitFilterTypeMissing.new(self, name)
|
37
|
-
end
|
35
|
+
raise Errors::ImplicitFilterTypeMissing.new(self, name)
|
38
36
|
end
|
39
37
|
end
|
40
38
|
|
@@ -51,15 +49,13 @@ module Graphiti
|
|
51
49
|
|
52
50
|
if get_attr(name, :sortable, raise_error: :only_unsupported)
|
53
51
|
config[:sorts][name] = {
|
54
|
-
proc: blk
|
52
|
+
proc: blk,
|
55
53
|
}.merge(opts.slice(:only))
|
54
|
+
elsif (type = args[0])
|
55
|
+
attribute name, type, only: [:sortable]
|
56
|
+
sort(name, opts, &blk)
|
56
57
|
else
|
57
|
-
|
58
|
-
attribute name, type, only: [:sortable]
|
59
|
-
sort(name, opts, &blk)
|
60
|
-
else
|
61
|
-
raise Errors::ImplicitSortTypeMissing.new(self, name)
|
62
|
-
end
|
58
|
+
raise Errors::ImplicitSortTypeMissing.new(self, name)
|
63
59
|
end
|
64
60
|
end
|
65
61
|
|
@@ -76,7 +72,7 @@ module Graphiti
|
|
76
72
|
def default_filter(name = nil, &blk)
|
77
73
|
name ||= :__default
|
78
74
|
config[:default_filters][name.to_sym] = {
|
79
|
-
filter: blk
|
75
|
+
filter: blk,
|
80
76
|
}
|
81
77
|
end
|
82
78
|
|
@@ -116,7 +112,7 @@ module Graphiti
|
|
116
112
|
readable: true,
|
117
113
|
writable: false,
|
118
114
|
sortable: false,
|
119
|
-
filterable: false
|
115
|
+
filterable: false,
|
120
116
|
}
|
121
117
|
options = defaults.merge(options)
|
122
118
|
config[:extra_attributes][name] = options
|
@@ -147,19 +143,19 @@ module Graphiti
|
|
147
143
|
|
148
144
|
def attribute_option(options, name)
|
149
145
|
if options[name] != false
|
150
|
-
default = if only = options[:only]
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
146
|
+
default = if (only = options[:only])
|
147
|
+
Array(only).include?(name) ? true : false
|
148
|
+
elsif (except = options[:except])
|
149
|
+
Array(except).include?(name) ? false : true
|
150
|
+
else
|
151
|
+
send(:"attributes_#{name}_by_default")
|
152
|
+
end
|
157
153
|
options[name] ||= default
|
158
154
|
end
|
159
155
|
end
|
160
156
|
private :attribute_option
|
161
157
|
|
162
|
-
|
158
|
+
def relationship_option(options, name)
|
163
159
|
if options[name] != false
|
164
160
|
options[name] ||= send(:"relationships_#{name}_by_default")
|
165
161
|
end
|
@@ -25,7 +25,7 @@ module Graphiti
|
|
25
25
|
def _find(params = {}, base_scope = nil)
|
26
26
|
id = params[:data].try(:[], :id) || params.delete(:id)
|
27
27
|
params[:filter] ||= {}
|
28
|
-
params[:filter]
|
28
|
+
params[:filter][:id] = id if id
|
29
29
|
|
30
30
|
runner = Runner.new(self, params)
|
31
31
|
runner.proxy(base_scope, single: true, raise_on_missing: true)
|
@@ -42,8 +42,8 @@ module Graphiti
|
|
42
42
|
def validate!(params)
|
43
43
|
return unless validate_endpoints?
|
44
44
|
|
45
|
-
if context
|
46
|
-
path = context.request.env[
|
45
|
+
if context&.respond_to?(:request)
|
46
|
+
path = context.request.env["PATH_INFO"]
|
47
47
|
unless allow_request?(path, params, context_namespace)
|
48
48
|
raise Errors::InvalidEndpoint.new(self, path, context_namespace)
|
49
49
|
end
|
@@ -6,7 +6,7 @@ module Graphiti
|
|
6
6
|
|
7
7
|
module Overrides
|
8
8
|
def endpoint
|
9
|
-
if endpoint = super
|
9
|
+
if (endpoint = super)
|
10
10
|
endpoint
|
11
11
|
else
|
12
12
|
self.endpoint = infer_endpoint
|
@@ -34,12 +34,12 @@ module Graphiti
|
|
34
34
|
def infer_endpoint
|
35
35
|
return unless name
|
36
36
|
|
37
|
-
path = "/#{name.gsub(
|
37
|
+
path = "/#{name.gsub("Resource", "").pluralize.underscore}".to_sym
|
38
38
|
{
|
39
39
|
path: path,
|
40
40
|
full_path: full_path_for(path),
|
41
41
|
url: url_for(path),
|
42
|
-
actions: DEFAULT_ACTIONS.dup
|
42
|
+
actions: DEFAULT_ACTIONS.dup,
|
43
43
|
}
|
44
44
|
end
|
45
45
|
|
@@ -49,7 +49,7 @@ module Graphiti
|
|
49
49
|
path: path,
|
50
50
|
full_path: full_path_for(path),
|
51
51
|
url: url_for(path),
|
52
|
-
actions: actions
|
52
|
+
actions: actions,
|
53
53
|
}
|
54
54
|
end
|
55
55
|
|
@@ -60,7 +60,7 @@ module Graphiti
|
|
60
60
|
path: path,
|
61
61
|
full_path: full_path_for(path),
|
62
62
|
url: url_for(path),
|
63
|
-
actions: actions
|
63
|
+
actions: actions,
|
64
64
|
}]
|
65
65
|
end
|
66
66
|
|
@@ -68,29 +68,29 @@ module Graphiti
|
|
68
68
|
([endpoint] + secondary_endpoints).compact
|
69
69
|
end
|
70
70
|
|
71
|
-
def allow_request?(
|
72
|
-
|
71
|
+
def allow_request?(request_path, params, action)
|
72
|
+
request_path = request_path.split(".")[0]
|
73
73
|
|
74
74
|
endpoints.any? do |e|
|
75
75
|
has_id = params[:id] || params[:data].try(:[], :id)
|
76
|
-
|
76
|
+
path = request_path
|
77
77
|
if [:update, :show, :destroy].include?(context_namespace) && has_id
|
78
|
-
|
79
|
-
|
80
|
-
|
78
|
+
path = request_path.split("/")
|
79
|
+
path.pop
|
80
|
+
path = path.join("/")
|
81
81
|
end
|
82
|
-
e[:full_path].to_s ==
|
82
|
+
e[:full_path].to_s == path && e[:actions].include?(context_namespace)
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
86
|
private
|
87
87
|
|
88
88
|
def full_path_for(path)
|
89
|
-
[endpoint_namespace, path].join(
|
89
|
+
[endpoint_namespace, path].join("").to_sym
|
90
90
|
end
|
91
91
|
|
92
92
|
def url_for(path)
|
93
|
-
[base_url, full_path_for(path)].join(
|
93
|
+
[base_url, full_path_for(path)].join("").to_sym
|
94
94
|
end
|
95
95
|
end
|
96
96
|
end
|
@@ -30,7 +30,7 @@ module Graphiti
|
|
30
30
|
|
31
31
|
def around_attributes(method = nil, only: [:create, :update], &blk)
|
32
32
|
if blk
|
33
|
-
raise Errors::AroundCallbackProc.new(self,
|
33
|
+
raise Errors::AroundCallbackProc.new(self, "around_attributes")
|
34
34
|
else
|
35
35
|
add_callback(:attributes, :around, method, only, &blk)
|
36
36
|
end
|
@@ -38,7 +38,7 @@ module Graphiti
|
|
38
38
|
|
39
39
|
def around_save(method = nil, only: [:create, :update], &blk)
|
40
40
|
if blk
|
41
|
-
raise Errors::AroundCallbackProc.new(self,
|
41
|
+
raise Errors::AroundCallbackProc.new(self, "around_save")
|
42
42
|
else
|
43
43
|
add_callback(:save, :around, method, only, &blk)
|
44
44
|
end
|
@@ -46,7 +46,7 @@ module Graphiti
|
|
46
46
|
|
47
47
|
def around_persistence(method = nil, only: [:create, :update], &blk)
|
48
48
|
if blk
|
49
|
-
raise Errors::AroundCallbackProc.new(self,
|
49
|
+
raise Errors::AroundCallbackProc.new(self, "around_persistence")
|
50
50
|
else
|
51
51
|
add_callback(:persistence, :around, method, only, &blk)
|
52
52
|
end
|
@@ -54,7 +54,7 @@ module Graphiti
|
|
54
54
|
|
55
55
|
def around_destroy(method = nil, &blk)
|
56
56
|
if blk
|
57
|
-
raise Errors::AroundCallbackProc.new(self,
|
57
|
+
raise Errors::AroundCallbackProc.new(self, "around_destroy")
|
58
58
|
else
|
59
59
|
add_callback(:destroy, :around, method, [:destroy], &blk)
|
60
60
|
end
|
@@ -62,10 +62,10 @@ module Graphiti
|
|
62
62
|
|
63
63
|
private
|
64
64
|
|
65
|
-
def add_callback(kind, lifecycle, method
|
65
|
+
def add_callback(kind, lifecycle, method, only, &blk)
|
66
66
|
config[:callbacks][kind] ||= {}
|
67
67
|
config[:callbacks][kind][lifecycle] ||= []
|
68
|
-
config[:callbacks][kind][lifecycle] << {
|
68
|
+
config[:callbacks][kind][lifecycle] << {callback: (method || blk), only: Array(only)}
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
@@ -136,13 +136,13 @@ module Graphiti
|
|
136
136
|
fire_around_callbacks(kind, action, *args) do |*yieldargs|
|
137
137
|
fire_callbacks(kind, :before, action, *yieldargs)
|
138
138
|
result = yield(*yieldargs)
|
139
|
-
fire_callbacks(kind, :after, action,
|
139
|
+
fire_callbacks(kind, :after, action, result, args.last)
|
140
140
|
result
|
141
141
|
end
|
142
142
|
end
|
143
143
|
|
144
144
|
def fire_callbacks(kind, lifecycle, action, *args)
|
145
|
-
if callbacks = self.class.config[:callbacks][kind]
|
145
|
+
if (callbacks = self.class.config[:callbacks][kind])
|
146
146
|
callbacks = callbacks[lifecycle] || []
|
147
147
|
callbacks.each do |config|
|
148
148
|
callback = config[:callback]
|
@@ -187,7 +187,7 @@ module Graphiti
|
|
187
187
|
r = nil
|
188
188
|
args = args[0..0] if method(method_name).arity == 1
|
189
189
|
send(method_name, *args) do |r2|
|
190
|
-
wrapped = around_callback_proc(callbacks, index+1, r2, &blk)
|
190
|
+
wrapped = around_callback_proc(callbacks, index + 1, r2, &blk)
|
191
191
|
r = instance_exec(r2, &wrapped)
|
192
192
|
end
|
193
193
|
r
|
@@ -45,16 +45,16 @@ module Graphiti
|
|
45
45
|
sl = super
|
46
46
|
if !polymorphic_child? && sl.nil?
|
47
47
|
children.each do |c|
|
48
|
-
break if sl = c.sideloads[name]
|
48
|
+
break if (sl = c.sideloads[name])
|
49
49
|
end
|
50
50
|
end
|
51
51
|
sl
|
52
52
|
end
|
53
53
|
|
54
54
|
def children
|
55
|
-
@children ||= polymorphic.map
|
55
|
+
@children ||= polymorphic.map { |klass|
|
56
56
|
klass.is_a?(String) ? klass.safe_constantize : klass
|
57
|
-
|
57
|
+
}
|
58
58
|
end
|
59
59
|
|
60
60
|
def resource_for_type(type)
|
@@ -36,8 +36,8 @@ module Graphiti
|
|
36
36
|
{}.tap do |headers|
|
37
37
|
if defined?(Rails) && context
|
38
38
|
raw = context.request.headers.to_h
|
39
|
-
if auth = raw[
|
40
|
-
headers[
|
39
|
+
if (auth = raw["HTTP_AUTHORIZATION"])
|
40
|
+
headers["Authorization"] = auth
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
@@ -49,7 +49,7 @@ module Graphiti
|
|
49
49
|
|
50
50
|
def make_request(url)
|
51
51
|
headers = request_headers.dup
|
52
|
-
headers[
|
52
|
+
headers["Content-Type"] = "application/vnd.api+json"
|
53
53
|
faraday.get(url, nil, headers) do |req|
|
54
54
|
yield req if block_given? # for super do ... end
|
55
55
|
req.options.timeout = timeout if timeout
|
@@ -13,7 +13,7 @@ module Graphiti
|
|
13
13
|
relationship_option(opts, :readable)
|
14
14
|
relationship_option(opts, :writable)
|
15
15
|
sideload = klass.new(name, opts)
|
16
|
-
if parent = opts[:parent]
|
16
|
+
if (parent = opts[:parent])
|
17
17
|
parent.children[name] = sideload
|
18
18
|
else
|
19
19
|
config[:sideloads][name] = sideload
|
@@ -51,10 +51,10 @@ module Graphiti
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def polymorphic_belongs_to(name, opts = {}, &blk)
|
54
|
-
opts[:resource] ||= Class.new(::Graphiti::Resource)
|
54
|
+
opts[:resource] ||= Class.new(::Graphiti::Resource) {
|
55
55
|
self.polymorphic = []
|
56
56
|
self.abstract_class = true
|
57
|
-
|
57
|
+
}
|
58
58
|
# adapters *probably* don't need to override this, but it's allowed
|
59
59
|
opts[:class] ||= adapter.sideloading_classes[:polymorphic_belongs_to]
|
60
60
|
opts[:class] ||= ::Graphiti::Sideload::PolymorphicBelongsTo
|
@@ -65,10 +65,10 @@ module Graphiti
|
|
65
65
|
as = opts.delete(:as)
|
66
66
|
opts[:foreign_key] ||= :"#{as}_id"
|
67
67
|
opts[:polymorphic_as] ||= as
|
68
|
-
|
68
|
+
model_ref = model
|
69
69
|
has_many name, opts do
|
70
70
|
params do |hash|
|
71
|
-
hash[:filter][:"#{as}_type"] =
|
71
|
+
hash[:filter][:"#{as}_type"] = model_ref.name
|
72
72
|
end
|
73
73
|
|
74
74
|
instance_eval(&blk) if block_given?
|
@@ -8,12 +8,12 @@ module Graphiti
|
|
8
8
|
payload: nil,
|
9
9
|
single: false,
|
10
10
|
raise_on_missing: false)
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
@resource = resource
|
12
|
+
@scope = scope
|
13
|
+
@query = query
|
14
|
+
@payload = payload
|
15
|
+
@single = single
|
16
|
+
@raise_on_missing = raise_on_missing
|
17
17
|
end
|
18
18
|
|
19
19
|
def single?
|
@@ -62,7 +62,7 @@ module Graphiti
|
|
62
62
|
records
|
63
63
|
end
|
64
64
|
end
|
65
|
-
alias
|
65
|
+
alias to_a data
|
66
66
|
|
67
67
|
def each(&blk)
|
68
68
|
to_a.each(&blk)
|
@@ -90,12 +90,12 @@ module Graphiti
|
|
90
90
|
original = Graphiti.context[:namespace]
|
91
91
|
begin
|
92
92
|
Graphiti.context[:namespace] = action
|
93
|
-
validator = persist
|
93
|
+
validator = persist {
|
94
94
|
@resource.persist_with_relationships \
|
95
95
|
@payload.meta(action: action),
|
96
96
|
@payload.attributes,
|
97
97
|
@payload.relationships
|
98
|
-
|
98
|
+
}
|
99
99
|
ensure
|
100
100
|
Graphiti.context[:namespace] = original
|
101
101
|
end
|
@@ -115,7 +115,7 @@ module Graphiti
|
|
115
115
|
|
116
116
|
def destroy
|
117
117
|
transaction_response = @resource.transaction do
|
118
|
-
metadata = {
|
118
|
+
metadata = {method: :destroy}
|
119
119
|
model = @resource.destroy(@query.filters[:id], metadata)
|
120
120
|
model.instance_variable_set(:@__serializer_klass, @resource.serializer)
|
121
121
|
validator = ::Graphiti::Util::ValidationResponse.new \
|
@@ -123,7 +123,7 @@ module Graphiti
|
|
123
123
|
validator.validate!
|
124
124
|
@resource.before_commit(model, metadata)
|
125
125
|
|
126
|
-
{
|
126
|
+
{result: validator}
|
127
127
|
end
|
128
128
|
@data, success = transaction_response[:result].to_a
|
129
129
|
success
|
@@ -165,7 +165,7 @@ module Graphiti
|
|
165
165
|
end
|
166
166
|
end
|
167
167
|
|
168
|
-
|
168
|
+
_data, success = transaction_response[:result].to_a
|
169
169
|
if success
|
170
170
|
transaction_response[:after_commit_hooks].each do |hook|
|
171
171
|
hook.call
|
data/lib/graphiti/schema.rb
CHANGED
@@ -12,12 +12,12 @@ module Graphiti
|
|
12
12
|
def self.generate!(resources = nil)
|
13
13
|
schema = generate(resources)
|
14
14
|
|
15
|
-
if ENV[
|
15
|
+
if ENV["FORCE_SCHEMA"] != "true" && File.exist?(Graphiti.config.schema_path)
|
16
16
|
old = JSON.parse(File.read(Graphiti.config.schema_path))
|
17
17
|
errors = Graphiti::SchemaDiff.new(old, schema).compare
|
18
18
|
return errors if errors.any?
|
19
19
|
end
|
20
|
-
FileUtils.mkdir_p(Graphiti.config.schema_path.gsub(
|
20
|
+
FileUtils.mkdir_p(Graphiti.config.schema_path.gsub("/schema.json", ""))
|
21
21
|
File.write(Graphiti.config.schema_path, JSON.pretty_generate(schema))
|
22
22
|
[]
|
23
23
|
end
|
@@ -32,7 +32,7 @@ module Graphiti
|
|
32
32
|
{
|
33
33
|
resources: generate_resources,
|
34
34
|
endpoints: generate_endpoints,
|
35
|
-
types: generate_types
|
35
|
+
types: generate_types,
|
36
36
|
}
|
37
37
|
end
|
38
38
|
|
@@ -52,16 +52,16 @@ module Graphiti
|
|
52
52
|
r.endpoints.each do |e|
|
53
53
|
actions = {}
|
54
54
|
e[:actions].each do |a|
|
55
|
-
next unless ctx = context_for(e[:full_path], a)
|
55
|
+
next unless (ctx = context_for(e[:full_path], a))
|
56
56
|
|
57
57
|
existing = endpoints[e[:full_path]]
|
58
|
-
if existing && config = existing[:actions][a]
|
58
|
+
if existing && (config = existing[:actions][a])
|
59
59
|
raise Errors::ResourceEndpointConflict.new \
|
60
60
|
e[:full_path], a, r.name, config[:resource]
|
61
61
|
end
|
62
62
|
|
63
|
-
actions[a] = {
|
64
|
-
if allowlist = ctx.sideload_allowlist
|
63
|
+
actions[a] = {resource: r.name}
|
64
|
+
if (allowlist = ctx.sideload_allowlist)
|
65
65
|
if allowlist[a]
|
66
66
|
actions[a].merge!(sideload_allowlist: allowlist[a])
|
67
67
|
end
|
@@ -69,7 +69,7 @@ module Graphiti
|
|
69
69
|
end
|
70
70
|
|
71
71
|
unless actions.empty?
|
72
|
-
endpoints[e[:full_path]] ||= {
|
72
|
+
endpoints[e[:full_path]] ||= {actions: {}}
|
73
73
|
endpoints[e[:full_path]][:actions].merge!(actions)
|
74
74
|
end
|
75
75
|
end
|
@@ -82,7 +82,7 @@ module Graphiti
|
|
82
82
|
end
|
83
83
|
|
84
84
|
def generate_resources
|
85
|
-
arr = @local_resources.map
|
85
|
+
arr = @local_resources.map { |r|
|
86
86
|
config = {
|
87
87
|
name: r.name,
|
88
88
|
type: r.type.to_s,
|
@@ -91,13 +91,13 @@ module Graphiti
|
|
91
91
|
extra_attributes: extra_attributes(r),
|
92
92
|
sorts: sorts(r),
|
93
93
|
filters: filters(r),
|
94
|
-
relationships: relationships(r)
|
94
|
+
relationships: relationships(r),
|
95
95
|
}
|
96
96
|
|
97
97
|
if r.default_sort
|
98
|
-
default_sort = r.default_sort.map
|
99
|
-
{
|
100
|
-
|
98
|
+
default_sort = r.default_sort.map { |s|
|
99
|
+
{s.keys.first.to_s => s.values.first.to_s}
|
100
|
+
}
|
101
101
|
config[:default_sort] = default_sort
|
102
102
|
end
|
103
103
|
|
@@ -106,20 +106,21 @@ module Graphiti
|
|
106
106
|
end
|
107
107
|
|
108
108
|
if r.polymorphic?
|
109
|
-
config
|
109
|
+
config[:polymorphic] = true
|
110
|
+
config[:children] = r.children.map(&:name)
|
110
111
|
end
|
111
112
|
|
112
113
|
config
|
113
|
-
|
114
|
+
}
|
114
115
|
|
115
|
-
arr |= @remote_resources.map
|
116
|
+
arr |= @remote_resources.map { |r|
|
116
117
|
{
|
117
118
|
name: r.name,
|
118
119
|
description: r.description,
|
119
120
|
remote: r.remote_url,
|
120
|
-
relationships: relationships(r)
|
121
|
+
relationships: relationships(r),
|
121
122
|
}
|
122
|
-
|
123
|
+
}
|
123
124
|
|
124
125
|
arr
|
125
126
|
end
|
@@ -129,9 +130,9 @@ module Graphiti
|
|
129
130
|
resource.attributes.each_pair do |name, config|
|
130
131
|
if config.values_at(:readable, :writable).any?
|
131
132
|
attrs[name] = {
|
132
|
-
type:
|
133
|
-
readable:
|
134
|
-
writable:
|
133
|
+
type: config[:type].to_s,
|
134
|
+
readable: flag(config[:readable]),
|
135
|
+
writable: flag(config[:writable]),
|
135
136
|
description: resource.attribute_description(name),
|
136
137
|
}
|
137
138
|
end
|
@@ -143,7 +144,7 @@ module Graphiti
|
|
143
144
|
{}.tap do |attrs|
|
144
145
|
resource.extra_attributes.each_pair do |name, config|
|
145
146
|
attrs[name] = {
|
146
|
-
type:
|
147
|
+
type: config[:type].to_s,
|
147
148
|
readable: flag(config[:readable]),
|
148
149
|
description: resource.attribute_description(name),
|
149
150
|
}
|
@@ -151,9 +152,9 @@ module Graphiti
|
|
151
152
|
end
|
152
153
|
end
|
153
154
|
|
154
|
-
def flag(value)
|
155
|
+
def flag(value)
|
155
156
|
if value.is_a?(Symbol)
|
156
|
-
|
157
|
+
"guarded"
|
157
158
|
else
|
158
159
|
!!value
|
159
160
|
end
|
@@ -178,7 +179,7 @@ module Graphiti
|
|
178
179
|
resource.filters.each_pair do |name, filter|
|
179
180
|
config = {
|
180
181
|
type: filter[:type].to_s,
|
181
|
-
operators: filter[:operators].keys.map(&:to_s)
|
182
|
+
operators: filter[:operators].keys.map(&:to_s),
|
182
183
|
}
|
183
184
|
|
184
185
|
config[:single] = true if filter[:single]
|
@@ -202,7 +203,7 @@ module Graphiti
|
|
202
203
|
def relationships(resource)
|
203
204
|
{}.tap do |r|
|
204
205
|
resource.sideloads.each_pair do |name, config|
|
205
|
-
schema = {
|
206
|
+
schema = {type: config.type.to_s, description: config.description}
|
206
207
|
if config.type == :polymorphic_belongs_to
|
207
208
|
schema[:resources] = config.children.values
|
208
209
|
.map(&:resource).map(&:class).map(&:name)
|