graphql-rails-api 0.2 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d57fa3b16ccdcf04df7d00d4fe67475c52023335a4655a2efa0261023a4cf78c
4
- data.tar.gz: 13a7ae285e5f4594322f9960d400f9657c06326e57bfde6322633764b573e3b0
3
+ metadata.gz: a1281132c1de853a10666be60aba3a29e7e4e6d83342c550435dec84829c3c42
4
+ data.tar.gz: 02d99ace4e2fbf81ad86a6704acf254c164dc0335c2c54d97e52f61e5edd38d4
5
5
  SHA512:
6
- metadata.gz: 525754f37def8d961cceb0ae151d44e00369b8f2099f5ad6648f5ab276759b9de36ad1e1ed0e50f1ec48130f5c0088be7193d5bbd8d881c699fa3e1f84f16d95
7
- data.tar.gz: 9d5d568d8e8e22a50ef9b4760629cb1efbcf6a536c397510bc4d170bba5d4e178962a3ef933cba12ab8c4a45078319c45e33bc9824a9d16ddf1707bc2a6fdf66
6
+ metadata.gz: aa12edf3276a69cf22677fa055acc59c2387700d6b61445da2f2b5e628904b626b40025509234d61f2d0099c9d1797576cda526c9b3932495111e7cb4cd2cb50
7
+ data.tar.gz: 9f8a42aa10470ed57a530ed790d237a37ef125ba9e85e771d6d31f0f65ed2fb5a9c881a798224465a4b3cecf58f487b074b9ca3fa6165d71c14b36f508b46b36
@@ -67,7 +67,7 @@ module GraphqlRailsApi
67
67
  write_at(
68
68
  'config/application.rb',
69
69
  5,
70
- "require 'graphql/hydrate_query'\nrequire 'graphql/visibility_hash'\n"
70
+ "require 'graphql/hydrate_query'\n"
71
71
  )
72
72
  end
73
73
 
@@ -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(*array)
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
@@ -1,7 +1,7 @@
1
1
  module Graphql
2
2
  module Rails
3
3
  module Api
4
- VERSION = '0.2'.freeze
4
+ VERSION = '0.2.2'.freeze
5
5
  end
6
6
  end
7
7
  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: '0.2'
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