graphql_includable 0.1.5 → 0.1.7

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/graphql_includable.rb +25 -25
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 803a100d8819528d092fd114ab447f1eabcb3872
4
- data.tar.gz: 18b7e345903158ed2bcf003852061a34d6ccaf04
3
+ metadata.gz: 78dc263783852ec3090c807ab090253f08608de1
4
+ data.tar.gz: 46aec07790da54100a3787a6c45df8064c90beda
5
5
  SHA512:
6
- metadata.gz: dd8826b97ddcb70b6701a4da2136593f0b8743f284b8f0115ea547f1b3b8f4cbdc1e25291a5b6efc30bac98a44e8641e1a5a0d2e641144003594f2cb61257b21
7
- data.tar.gz: d5a44484141daeae5667cdc06c550623dd636a3324b273672a0ef5918f0bd65c33561f68af35d386f68a5481a56339393278eec22cf8dd45aa9a05389f4f2195
6
+ metadata.gz: c7ee1838d8167126aecad34b00eaf338ec7515812a2290d724a5f1f7e8eb65c5bcbef139eb0a09a59837938b9cc35030ef239258d584867f95a316d84c3e3255
7
+ data.tar.gz: fb5d17e8a29ecc47d8f819098573e25dfdd82250906273d105f2ae984303941ead7b0f97cf18b99057a11faea779901659b733c567fa018e22a0f3926ca67168
@@ -1,15 +1,16 @@
1
- require "graphql"
2
- require "active_support/concern"
1
+ require 'graphql'
2
+ require 'active_support/concern'
3
3
 
4
- GraphQL::Field.accepts_definitions includes: GraphQL::Define.assign_metadata_key(:includes)
4
+ GraphQL::Field.accepts_definitions(
5
+ includes: GraphQL::Define.assign_metadata_key(:includes)
6
+ )
5
7
 
6
8
  module GraphQLIncludable
7
9
  extend ActiveSupport::Concern
8
10
 
9
11
  module ClassMethods
10
- def includes_from_graphql(query_context)
11
- generated_includes = GraphQLIncludable.generate_includes_from_graphql(query_context, self.model_name.to_s)
12
- puts "final gen: #{generated_includes}"
12
+ def includes_from_graphql(ctx)
13
+ generated_includes = GraphQLIncludable.generate_includes_from_graphql(ctx, model_name.to_s)
13
14
  includes(generated_includes)
14
15
  end
15
16
 
@@ -26,11 +27,9 @@ module GraphQLIncludable
26
27
  end
27
28
  end
28
29
 
29
- private
30
-
31
- def self.generate_includes_from_graphql(query_context, model_name)
32
- matching_node = GraphQLIncludable.find_child_returning_model_name(query_context.irep_node, model_name)
33
- GraphQLIncludable.includes_from_graphql_field(matching_node)
30
+ def self.generate_includes_from_graphql(ctx, model_name)
31
+ matching_node = find_child_returning_model_name(ctx.irep_node, model_name)
32
+ includes_from_graphql_field(matching_node)
34
33
  end
35
34
 
36
35
  def self.find_child_returning_model_name(node, model_name)
@@ -39,7 +38,7 @@ module GraphQLIncludable
39
38
  if return_type.to_s == model_name
40
39
  matching_node = node
41
40
  elsif node.respond_to? :scoped_children
42
- node.scoped_children[return_type].each do |child_name, child_node|
41
+ node.scoped_children[return_type].each do |_child_name, child_node|
43
42
  matching_node = find_child_returning_model_name(child_node, model_name)
44
43
  break if matching_node
45
44
  end
@@ -51,18 +50,16 @@ module GraphQLIncludable
51
50
  includes = []
52
51
  nested_includes = {}
53
52
 
54
- return_type = node_return_type(node)
55
53
  return_model = node_return_class(node)
56
- return [] unless node && return_type && return_model
54
+ return [] unless node && return_model
57
55
 
58
- node.scoped_children[return_type].each do |_child_name, child_node|
56
+ node.scoped_children[node_return_type(node)].each do |_child_name, child_node|
59
57
  child_includes = includes_from_graphql_child(child_node, return_model)
60
58
  if child_includes.is_a?(Hash)
61
59
  nested_includes.merge!(child_includes)
62
- elsif child_includes.is_a?(Array)
60
+ else
61
+ child_includes = [child_includes] unless child_includes.is_a?(Array)
63
62
  includes += child_includes
64
- elsif child_includes
65
- includes << child_includes
66
63
  end
67
64
  end
68
65
 
@@ -89,10 +86,12 @@ module GraphQLIncludable
89
86
  end
90
87
 
91
88
  def self.node_return_class(node)
89
+ # rubocop:disable Lint/HandleExceptions, Style/RedundantBegin
92
90
  begin
93
91
  Object.const_get(node_return_type(node).name)
94
92
  rescue NameError
95
93
  end
94
+ # rubocop:enable Lint/HandleExceptions, Style/RedundantBegin
96
95
  end
97
96
 
98
97
  def self.node_returns_active_record?(node)
@@ -110,7 +109,7 @@ module GraphQLIncludable
110
109
  end
111
110
  end
112
111
 
113
- # unwrap GraphQL ListType and NonNullType wrappers
112
+ # get unwrapped return type from a field, stripping ListType / NonNullType wrappers
114
113
  def self.node_return_type(node)
115
114
  type = node.return_type
116
115
  type = type.of_type while type.respond_to? :of_type
@@ -125,13 +124,14 @@ module GraphQLIncludable
125
124
  # get a 1d array of the chain of delegated model names,
126
125
  # so if model A delegates method B to model C, which delegates method B to model D,
127
126
  # delegated_includes_chain(A, :B) => [:C, :D]
128
- def self.delegated_includes_chain(model, method_name)
127
+ def self.delegated_includes_chain(base_model, method_name)
129
128
  chain = []
130
- delegated_model_name = model.instance_variable_get('@delegate_cache').try(:[], method_name.to_sym)
131
- while delegated_model_name
132
- chain << delegated_model_name
133
- delegated_model = model_name_to_class(delegated_model_name)
134
- delegated_model_name = delegated_model.instance_variable_get('@delegate_cache').try(:[], method_name.to_sym)
129
+ method = method_name.to_sym
130
+ model_name = base_model.instance_variable_get('@delegate_cache').try(:[], method)
131
+ while model_name
132
+ chain << model_name
133
+ model = model_name_to_class(model_name)
134
+ model_name = model.instance_variable_get('@delegate_cache').try(:[], method)
135
135
  end
136
136
  chain
137
137
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql_includable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Rouse