graphql_includable 0.1.0 → 0.1.1

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 +18 -24
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13f8e1de4b38eac1e571c18ecc7ca78dbb16327f
4
- data.tar.gz: 8b8318def58be25d950473392715d16f73b0d829
3
+ metadata.gz: 21599908b740ed6950c8337474da5b68d936311a
4
+ data.tar.gz: b02f6f9dad596fa35f74e98f13bbf1241d456ed3
5
5
  SHA512:
6
- metadata.gz: c105a43187fb8d9db518cce6ca824ef96f17f279303c9a5c459e8708370ded44ee22c593d6b5d183dfb108fbafc442ef0bedf0d343a2eb1eff5f5e67c1dcaf9a
7
- data.tar.gz: 2745bfa726da7f5ebc806afcc6c438d532a176f19ca9631894e3a62e9132ec814f4447012532359d2675acc370deb9265903aa6a020f7a16bc191d1a6a2d9318
6
+ metadata.gz: 9529b08e11f10ccbc5ae07985895ded5fb26f7c7e7dc9d7c5d46e66e7f80032306deeb379842cb840f282a4563e515b5c22a506569561300845048de27f0f2ff
7
+ data.tar.gz: 716d7d4ba07e5af8eec16ae812f59d8ac25f7153428336cdaf67c2c76e1e8e983798e151eb244e447cbc8b5bd3034881071d30a8b0e24fe713697fab5b2ab1c1
@@ -1,5 +1,6 @@
1
1
  require "graphql"
2
2
  require "active_support/concern"
3
+ require "byebug"
3
4
 
4
5
  GraphQL::Field.accepts_definitions includes: GraphQL::Define.assign_metadata_key(:includes)
5
6
 
@@ -55,18 +56,27 @@ module GraphQLIncludable
55
56
  return [] unless node && return_type && return_model
56
57
 
57
58
  node.scoped_children[return_type].each do |child_name, child_node|
58
- child_association_name, explicit_includes = suggested_association_name(child_name, child_node)
59
- association, delegated_model_name = find_association(return_model, child_association_name)
59
+ specified_includes = child_node.definitions[0].metadata[:includes]
60
+ raw_association_name = specified_includes || (node.definitions[0].property || child_name).to_sym
61
+ delegated_model_name = get_delegated_model(return_model, raw_association_name)
62
+ association_name = delegated_model_name || raw_association_name
63
+ association = return_model.reflect_on_association(association_name)
64
+
60
65
  if association
61
66
  child_includes = includes_from_irep_node(child_node)
62
67
 
63
68
  if node_has_active_record_children(child_node) && child_includes.size > 0
64
- nested_includes[delegated_model_name || child_association_name] = wrap_delegate(child_includes, delegated_model_name, child_association_name)
69
+ if delegated_model_name
70
+ nested_includes[delegated_model_name] = {}
71
+ nested_includes[delegated_model_name][raw_association_name] = child_includes
72
+ else
73
+ nested_includes[association_name] = child_includes
74
+ end
65
75
  else
66
- includes << wrap_delegate(child_association_name, delegated_model_name)
76
+ includes << association_name
67
77
  end
68
- elsif explicit_includes
69
- includes << explicit_includes
78
+ elsif specified_includes
79
+ includes << specified_includes
70
80
  end
71
81
  end
72
82
 
@@ -108,24 +118,8 @@ module GraphQLIncludable
108
118
  type
109
119
  end
110
120
 
111
- # find a valid association on return_model, following method delegation
112
- def self.find_association(return_model, child_name)
113
- delegated_model = return_model.instance_variable_get('@delegate_cache').try(:[], child_name.to_sym)
114
- association_model = delegated_model ? return_model.reflect_on_association(delegated_model).klass : return_model
115
- association = association_model.reflect_on_association(child_name)
116
-
117
- [association, delegated_model]
118
- end
119
-
120
- # find the association to look for based on a field definition
121
- # precedence is `includes` key, then `property` key, then the field name
122
- def self.suggested_association_name(name, node)
123
- includes_metadata = node.definitions[0].metadata[:includes]
124
-
125
- assoc_name = node.definitions[0].property || name
126
- assoc_name = includes_metadata if includes_metadata && includes_metadata.is_a?(Symbol)
127
-
128
- [assoc_name.to_sym, includes_metadata]
121
+ def self.get_delegated_model(model, method_name)
122
+ model.instance_variable_get('@delegate_cache').try(:[], method_name.to_sym)
129
123
  end
130
124
 
131
125
  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.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Rouse
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-08-28 00:00:00.000000000 Z
12
+ date: 2017-11-07 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: