@@ -6,61 +6,71 @@ module Relay
6
6
# - {#paged_nodes}, which applies `first` & `last` limits
7
7
#
8
8
# In a subclass, you have access to
9
- # - {#object }, the object which the connection will wrap
9
+ # - {#nodes }, the collection which the connection will wrap
10
10
# - {#first}, {#after}, {#last}, {#before} (arguments passed to the field)
11
11
# - {#max_page_size} (the specified maximum page size that can be returned from a connection)
12
12
#
13
13
class BaseConnection
14
+ extend Gem ::Deprecate
15
+
14
16
# Just to encode data in the cursor, use something that won't conflict
15
17
CURSOR_SEPARATOR = "---"
16
18
17
19
# Map of collection class names -> connection_classes
18
20
# eg {"Array" => ArrayConnection}
19
21
CONNECTION_IMPLEMENTATIONS = { }
20
22
21
- # Find a connection implementation suitable for exposing `items`
22
- #
23
- # @param [Object] A collection of items (eg, Array, AR::Relation)
24
- # @return [subclass of BaseConnection] a connection Class for wrapping `items`
25
- def self . connection_for_items ( items )
26
- # We check class membership by comparing class names rather than
27
- # identity to prevent this from being broken by Rails autoloading.
28
- # Changes to the source file for ItemsClass in Rails apps cause it to be
29
- # reloaded as a new object, so if we were to use `is_a?` here, it would
30
- # no longer match any registered custom connection types.
31
- ancestor_names = items . class . ancestors . map ( &:name )
32
- implementation = CONNECTION_IMPLEMENTATIONS . find do |items_class_name , connection_class |
33
- ancestor_names . include? items_class_name
23
+ class << self
24
+ extend Gem ::Deprecate
25
+
26
+ # Find a connection implementation suitable for exposing `nodes`
27
+ #
28
+ # @param [Object] A collection of nodes (eg, Array, AR::Relation)
29
+ # @return [subclass of BaseConnection] a connection Class for wrapping `nodes`
30
+ def connection_for_nodes ( nodes )
31
+ # Check for class _names_ because classes can be redefined in Rails development
32
+ ancestor_names = nodes . class . ancestors . map ( &:name )
33
+ implementation = CONNECTION_IMPLEMENTATIONS . find do |nodes_class_name , connection_class |
34
+ ancestor_names . include? nodes_class_name
35
+ end
36
+ if implementation . nil?
37
+ raise ( "No connection implementation to wrap #{ nodes . class } (#{ nodes } )" )
38
+ else
39
+ implementation [ 1 ]
40
+ end
34
41
end
35
- if implementation . nil?
36
- raise ( "No connection implementation to wrap #{ items . class } (#{ items } )" )
37
- else
38
- implementation [ 1 ]
42
+
43
+ # Add `connection_class` as the connection wrapper for `nodes_class`
44
+ # eg, `RelationConnection` is the implementation for `AR::Relation`
45
+ # @param [Class] A class representing a collection (eg, Array, AR::Relation)
46
+ # @param [Class] A class implementing Connection methods
47
+ def register_connection_implementation ( nodes_class , connection_class )
48
+ CONNECTION_IMPLEMENTATIONS [ nodes_class . name ] = connection_class
39
49
end
40
- end
41
50
42
- # Add `connection_class` as the connection wrapper for `items_class`
43
- # eg, `RelationConnection` is the implementation for `AR::Relation`
44
- # @param [Class] A class representing a collection (eg, Array, AR::Relation)
45
- # @param [Class] A class implementing Connection methods
46
- def self . register_connection_implementation ( items_class , connection_class )
47
- CONNECTION_IMPLEMENTATIONS [ items_class . name ] = connection_class
51
+ # @deprecated use {#connection_for_nodes} instead
52
+ alias :connection_for_items :connection_for_nodes
53
+ deprecate ( :connection_for_items , :connection_for_nodes , 2016 , 9 )
48
54
end
49
55
50
- attr_reader :object , :arguments , :max_page_size , :parent
56
+ attr_reader :nodes , :arguments , :max_page_size , :parent
51
57
52
- # Make a connection, wrapping `object `
53
- # @param The collection of results
58
+ # Make a connection, wrapping `nodes `
59
+ # @param [Object] The collection of nodes
54
60
# @param Query arguments
55
61
# @param max_page_size [Int] The maximum number of results to return
56
62
# @param parent [Object] The object which this collection belongs to
57
- def initialize ( object , arguments , max_page_size : nil , parent : nil )
58
- @object = object
63
+ def initialize ( nodes , arguments , max_page_size : nil , parent : nil )
64
+ @nodes = nodes
59
65
@arguments = arguments
60
66
@max_page_size = max_page_size
61
67
@parent = parent
62
68
end
63
69
70
+ # @deprecated use {#nodes} instead
71
+ alias :object :nodes
72
+ deprecate ( :object , :nodes , 2016 , 9 )
73
+
64
74
# Provide easy access to provided arguments:
65
75
METHODS_FROM_ARGUMENTS = [ :first , :after , :last , :before , :order ]
66
76
@@ -80,7 +90,7 @@ def initialize(object, arguments, max_page_size: nil, parent: nil)
80
90
end
81
91
end
82
92
83
- # These are the items to render for this connection,
93
+ # These are the nodes to render for this connection,
84
94
# probably wrapped by {GraphQL::Relay::Edge}
85
95
def edge_nodes
86
96
@edge_nodes ||= paged_nodes
@@ -127,11 +137,11 @@ def cursor_from_node(object)
127
137
private
128
138
129
139
def paged_nodes
130
- raise NotImplementedError , "must return items for this connection after paging"
140
+ raise NotImplementedError , "must return nodes for this connection after paging"
131
141
end
132
142
133
143
def sliced_nodes
134
- raise NotImplementedError , "must return all items for this connection after chopping off first and last"
144
+ raise NotImplementedError , "must return all nodes for this connection after chopping off first and last"
135
145
end
136
146
end
137
147
end
0 commit comments