graphql-rails-api 0.2 → 0.2.2
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1281132c1de853a10666be60aba3a29e7e4e6d83342c550435dec84829c3c42
|
4
|
+
data.tar.gz: 02d99ace4e2fbf81ad86a6704acf254c164dc0335c2c54d97e52f61e5edd38d4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa12edf3276a69cf22677fa055acc59c2387700d6b61445da2f2b5e628904b626b40025509234d61f2d0099c9d1797576cda526c9b3932495111e7cb4cd2cb50
|
7
|
+
data.tar.gz: 9f8a42aa10470ed57a530ed790d237a37ef125ba9e85e771d6d31f0f65ed2fb5a9c881a798224465a4b3cecf58f487b074b9ca3fa6165d71c14b36f508b46b36
|
@@ -1,39 +1,48 @@
|
|
1
1
|
module Graphql
|
2
2
|
class HydrateQuery
|
3
3
|
|
4
|
-
def initialize(model, context, id: nil, user: nil)
|
4
|
+
def initialize(model, context, check_visibility: true, id: nil, user: nil)
|
5
5
|
@fields = context&.irep_node&.scoped_children&.values&.first
|
6
6
|
@model = model
|
7
|
+
@models = [model_name.singularize.camelize]
|
8
|
+
@check_visibility = check_visibility
|
7
9
|
@id = id
|
8
10
|
@user = user
|
9
11
|
end
|
10
12
|
|
11
13
|
def run
|
12
|
-
hash = parse_fields(@fields)
|
13
|
-
array = hash_to_array_of_hashes(hash, @model)
|
14
14
|
@model = @model.where(id: @id) if @id
|
15
|
-
plucked = @model.deep_pluck(*
|
16
|
-
result = plucked_attr_to_structs(plucked)
|
15
|
+
plucked = @model.deep_pluck(*hash_to_array_of_hashes(parse_fields(@fields), @model))
|
16
|
+
result = plucked_attr_to_structs(plucked, model_name.singularize.camelize.constantize)
|
17
17
|
@id ? result.first : result
|
18
18
|
end
|
19
19
|
|
20
|
-
def plucked_attr_to_structs(arr)
|
21
|
-
arr.map { |e| hash_to_struct(e) }
|
20
|
+
def plucked_attr_to_structs(arr, parent_model)
|
21
|
+
arr.map { |e| hash_to_struct(e, parent_model) }
|
22
22
|
end
|
23
23
|
|
24
|
-
def hash_to_struct(hash)
|
24
|
+
def hash_to_struct(hash, parent_model)
|
25
|
+
return if !visibility_hash[parent_model].include?(hash['id']) && @check_visibility
|
25
26
|
hash.each_with_object(OpenStruct.new) do |(k, v), struct|
|
26
|
-
next struct[k.to_sym] = plucked_attr_to_structs(v) if v.is_a?(Array)
|
27
|
-
next struct[k.to_sym] = hash_to_struct(v) if v.is_a?(Hash)
|
27
|
+
next struct[k.to_sym] = plucked_attr_to_structs(v, evaluate_model(parent_model, k)) if v.is_a?(Array)
|
28
|
+
next struct[k.to_sym] = hash_to_struct(v, evaluate_model(parent_model, k)) if v.is_a?(Hash)
|
28
29
|
struct[k.to_sym] = v
|
29
30
|
end
|
30
31
|
end
|
31
32
|
|
33
|
+
def visibility_hash
|
34
|
+
@visibility_hash ||= @models.each_with_object({}) do |model, hash|
|
35
|
+
hash[model.constantize] = model.constantize.visible_for(user: @user).pluck(:id)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
32
39
|
def hash_to_array_of_hashes(hash, parent_class)
|
33
40
|
return if parent_class.nil?
|
41
|
+
hash['id'] = nil if hash['id'].blank?
|
34
42
|
hash.each_with_object([]) do |(k, v), arr|
|
35
43
|
next arr << k if v.blank? && parent_class.new.attributes.key?(k)
|
36
44
|
klass = evaluate_model(parent_class, k)
|
45
|
+
@models << klass.to_s unless @models.include?(klass.to_s)
|
37
46
|
arr << { k.to_sym => hash_to_array_of_hashes(v, klass) } if klass.present? && v.present?
|
38
47
|
end
|
39
48
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql-rails-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- poilon
|
@@ -70,7 +70,6 @@ files:
|
|
70
70
|
- lib/graphql/hydrate_query.rb
|
71
71
|
- lib/graphql/rails/api/config.rb
|
72
72
|
- lib/graphql/rails/api/version.rb
|
73
|
-
- lib/graphql/visibility_hash.rb
|
74
73
|
- lib/tasks/graphql/rails/api_tasks.rake
|
75
74
|
homepage: https://github.com/poilon/graphql-rails-api
|
76
75
|
licenses:
|
@@ -1,26 +0,0 @@
|
|
1
|
-
module Graphql
|
2
|
-
class VisibilityHash
|
3
|
-
|
4
|
-
def initialize(joins, user)
|
5
|
-
@joins = joins
|
6
|
-
@user = user
|
7
|
-
end
|
8
|
-
|
9
|
-
def run
|
10
|
-
fetch_models(@joins).each_with_object({}) do |model, hash|
|
11
|
-
hash[model.to_s.underscore.to_sym] = model.visible_for(user: @user).pluck(:id)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def fetch_models(hash)
|
16
|
-
model_names = hash.each_with_object([]) do |(k, v), keys|
|
17
|
-
keys << k
|
18
|
-
keys.concat(fetch_models(v)) if v.is_a? Hash
|
19
|
-
end.map { |m| m.to_s.singularize.camelize }.uniq
|
20
|
-
model_names.select do |m|
|
21
|
-
Object.const_defined?(m) && m.constantize.ancestors.include?(ActiveRecord::Base)
|
22
|
-
end.map(&:constantize)
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
end
|