33from .connectiontypes import Connection , PageInfo , Edge
44
55
6- def connection_from_list (data , args = {}, ** kwargs ):
6+ def connection_from_list (data , args = {}, connection_type = None ,
7+ edge_type = None , pageinfo_type = None , ** kwargs ):
78 '''
89 A simple function that accepts an array and connection arguments, and returns
910 a connection object for use in GraphQL. It uses array offsets as pagination,
1011 so pagination will only work if the array is static.
1112 '''
13+ connection_type = connection_type or Connection
14+ edge_type = edge_type or Edge
15+ pageinfo_type = pageinfo_type or PageInfo
16+
1217 full_args = dict (args , ** kwargs )
1318
1419 before = full_args .get ('before' )
@@ -21,7 +26,7 @@ def connection_from_list(data, args={}, **kwargs):
2126 begin = max (get_offset (after , - 1 ), - 1 ) + 1
2227 end = min (get_offset (before , count + 1 ), count )
2328 if begin >= count or begin >= end :
24- return empty_connection ()
29+ return empty_connection (connection_type , pageinfo_type )
2530
2631 # Save the pre-slice cursors
2732 first_preslice_cursor = offset_to_cursor (begin )
@@ -34,24 +39,24 @@ def connection_from_list(data, args={}, **kwargs):
3439 begin = max (end - last , begin )
3540
3641 if begin >= count or begin >= end :
37- return empty_connection ()
42+ return empty_connection (connection_type , pageinfo_type )
3843
3944 sliced_data = data [begin :end ]
4045 edges = [
41- Edge ( node , cursor = offset_to_cursor (i + begin ))
46+ edge_type ( node = node , cursor = offset_to_cursor (i + begin ))
4247 for i , node in enumerate (sliced_data )
4348 ]
4449
4550 # Construct the connection
4651 first_edge = edges [0 ]
4752 last_edge = edges [len (edges ) - 1 ]
48- return Connection (
49- edges ,
50- PageInfo (
51- startCursor = first_edge .cursor ,
52- endCursor = last_edge .cursor ,
53- hasPreviousPage = (first_edge .cursor != first_preslice_cursor ),
54- hasNextPage = (last_edge .cursor != last_preslice_cursor )
53+ return connection_type (
54+ edges = edges ,
55+ page_info = pageinfo_type (
56+ start_cursor = first_edge .cursor ,
57+ end_cursor = last_edge .cursor ,
58+ has_previous_page = (first_edge .cursor != first_preslice_cursor ),
59+ has_next_page = (last_edge .cursor != last_preslice_cursor )
5560 )
5661 )
5762
@@ -66,17 +71,20 @@ def connection_from_promised_list(data_promise, args={}, **kwargs):
6671 # return dataPromise.then(lambda data:connection_from_list(data, args))
6772
6873
69- def empty_connection ():
74+ def empty_connection (connection_type = None , pageinfo_type = None ):
7075 '''
7176 Helper to get an empty connection.
7277 '''
73- return Connection (
74- [],
75- PageInfo (
76- startCursor = None ,
77- endCursor = None ,
78- hasPreviousPage = False ,
79- hasNextPage = False ,
78+ connection_type = connection_type or Connection
79+ pageinfo_type = pageinfo_type or PageInfo
80+
81+ return connection_type (
82+ edges = [],
83+ page_info = pageinfo_type (
84+ start_cursor = None ,
85+ end_cursor = None ,
86+ has_previous_page = False ,
87+ has_next_page = False ,
8088 )
8189 )
8290
0 commit comments