graphql-rails-api 0.4.1 → 0.4.5

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: 481bdbd77a99147d9ca18b4d019ba21a1c364f6a8506a27f4b966b9396b262c2
4
- data.tar.gz: e9ef1795f16042ef84010d7624bd2773d99b4a3a6f66ea5abe6c0378055efb7b
3
+ metadata.gz: f0d802c95081098c189f0b5ee0fb282169533e275028429879ba4ceb218dca17
4
+ data.tar.gz: ae724c83294886154d99c56d711c1aaa5a64e333ee5a8def9c021b819a3fcbc8
5
5
  SHA512:
6
- metadata.gz: c3f416adfeb436ed9777dafca8880c72014217994ec17e2cf2061e72c454d20b08fe61fbd80f62c536753c13a92b5f9f53e742ec8b94679abdcaf4c59b281ef7
7
- data.tar.gz: c08cd31130934277d362ec035dc1e451b9ab304e2f0bf90e43e682f35413473938b67fc9b693d06dba515838c1d7046e9909e9114cf821b87cae02b6fd95d0b4
6
+ metadata.gz: cf4529de02e31bb8a97b78c94fcc04a7b0e2230ceabcd8e32a49a72ac67233aa1f982ff542b01464038245429584f2ba7edbf1e351a940ba03d72cc84c42fcce
7
+ data.tar.gz: ea107d69a51fa296378a07864f4b05bb7aa46452e0d2fd63edabb9a93394cae6e185737f873e6f3bf304deae2f3016c9ddf83b836ac112b6ecce009b2888b6f2
@@ -0,0 +1,26 @@
1
+ class GraphqlAllConnectionsGenerator < Rails::Generators::NamedBase
2
+
3
+ def generate
4
+ Graphql::Rails::Api::Config.query_resources.each do |resource|
5
+ dir = "app/graphql/#{resource.pluralize}"
6
+ generate_connection(dir, resource)
7
+ end
8
+ end
9
+
10
+ private
11
+
12
+ def generate_connection(dir, resource)
13
+ File.write(
14
+ "#{dir}/connection.rb",
15
+ <<~STRING
16
+ #{resource.pluralize.camelize}::Connection = #{resource.pluralize.camelize}::Type.define_connection do
17
+ name '#{resource.camelize}Connection'
18
+ field :total_count, types.Int do
19
+ resolve ->(obj, _, _) { obj.nodes.count }
20
+ end
21
+ end
22
+ STRING
23
+ )
24
+ end
25
+
26
+ end
@@ -15,7 +15,7 @@ module Graphql
15
15
  def run
16
16
  @model = @model.where(id: @id) if @id
17
17
  plucked = @model.deep_pluck(*hash_to_array_of_hashes(parse_fields(@fields), @model))
18
- result = plucked_attr_to_structs(plucked, model_name.singularize.camelize.constantize)
18
+ result = plucked_attr_to_structs(plucked, model_name.singularize.camelize.constantize)&.compact
19
19
  @id ? result.first : result
20
20
  end
21
21
 
@@ -24,10 +24,15 @@ module Graphql
24
24
  end
25
25
 
26
26
  def hash_to_struct(hash, parent_model)
27
- return if !visibility_hash[parent_model].include?(hash['id']) && @check_visibility
27
+ if @check_visibility &&
28
+ (visibility_hash[parent_model].blank? || !visibility_hash[parent_model].include?(hash['id']))
29
+ return
30
+ end
31
+
28
32
  hash.each_with_object(OpenStruct.new) do |(k, v), struct|
29
33
  next struct[k.to_sym] = plucked_attr_to_structs(v, evaluate_model(parent_model, k)) if v.is_a?(Array)
30
34
  next struct[k.to_sym] = hash_to_struct(v, evaluate_model(parent_model, k)) if v.is_a?(Hash)
35
+
31
36
  struct[k.to_sym] = v
32
37
  end
33
38
  end
@@ -36,15 +41,18 @@ module Graphql
36
41
  @visibility_hash ||= @models.reject(&:blank?).each_with_object({}) do |model, hash|
37
42
  visible = model.constantize.visible_for(user: @user)
38
43
  next if visible.blank?
44
+
39
45
  hash[model.constantize] = visible.pluck(:id)
40
46
  end
41
47
  end
42
48
 
43
49
  def hash_to_array_of_hashes(hash, parent_class)
44
50
  return if parent_class.nil?
51
+
45
52
  hash['id'] = nil if hash['id'].blank?
46
53
  hash.each_with_object([]) do |(k, v), arr|
47
54
  next arr << k if v.blank? && parent_class.new.attributes.key?(k)
55
+
48
56
  klass = evaluate_model(parent_class, k)
49
57
  @models << klass.to_s unless @models.include?(klass.to_s)
50
58
  arr << { k.to_sym => hash_to_array_of_hashes(v, klass) } if klass.present? && v.present?
@@ -65,6 +73,7 @@ module Graphql
65
73
  parent_class_name = parent.to_s.singularize.camelize
66
74
  return child_class_name.constantize if activerecord_model?(child_class_name)
67
75
  return unless activerecord_model?(parent_class_name)
76
+
68
77
  parent_class_name.constantize.reflections[child.to_s.underscore]&.klass
69
78
  end
70
79
 
@@ -1,7 +1,7 @@
1
1
  module Graphql
2
2
  module Rails
3
3
  module Api
4
- VERSION = '0.4.1'.freeze
4
+ VERSION = '0.4.5'.freeze
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-rails-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - poilon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-20 00:00:00.000000000 Z
11
+ date: 2018-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql
@@ -75,6 +75,7 @@ files:
75
75
  - MIT-LICENSE
76
76
  - README.md
77
77
  - Rakefile
78
+ - lib/generators/graphql_connections/graphql_all_connections_generator.rb
78
79
  - lib/generators/graphql_mutations/graphql_mutations_generator.rb
79
80
  - lib/generators/graphql_rails_api/install_generator.rb
80
81
  - lib/generators/graphql_resource/USAGE