1
1
from graphql_relay .node .node import (
2
2
to_global_id
3
3
)
4
- from graphql_relay .connection .connection import (
5
- connection_definitions
6
- )
7
4
8
5
from graphene .core .types import Interface , ObjectType
9
6
from graphene .core .fields import BooleanField , StringField , ListField , Field
10
7
from graphene .relay .fields import GlobalIDField
11
8
from graphene .utils import memoize
12
9
13
10
14
- class BaseNode (object ):
15
- @classmethod
16
- @memoize
17
- def get_connection (cls , schema ):
18
- _type = cls .internal_type (schema )
19
- type_name = cls ._meta .type_name
20
- connection = connection_definitions (type_name , _type ).connection_type
21
- return connection
22
-
23
- @classmethod
24
- def _prepare_class (cls ):
25
- from graphene .relay .utils import is_node
26
- if is_node (cls ):
27
- assert hasattr (
28
- cls , 'get_node' ), 'get_node classmethod not found in %s Node' % cls
29
-
30
- @classmethod
31
- def to_global_id (cls , instance , args , info ):
32
- type_name = cls ._meta .type_name
33
- return to_global_id (type_name , instance .id )
34
-
35
-
36
- class Node (BaseNode , Interface ):
37
- '''An object with an ID'''
38
- id = GlobalIDField ()
39
-
40
-
41
11
class PageInfo (ObjectType ):
42
12
has_next_page = BooleanField (required = True , description = 'When paginating forwards, are there more items?' )
43
13
has_previous_page = BooleanField (required = True , description = 'When paginating backwards, are there more items?' )
@@ -47,26 +17,70 @@ class PageInfo(ObjectType):
47
17
48
18
class Edge (ObjectType ):
49
19
'''An edge in a connection.'''
20
+ class Meta :
21
+ type_name = 'DefaultEdge'
22
+
50
23
node = Field (lambda field : field .object_type .node_type , description = 'The item at the end of the edge' )
51
- end_cursor = StringField (required = True , description = 'A cursor for use in pagination' )
24
+ cursor = StringField (required = True , description = 'A cursor for use in pagination' )
52
25
53
26
@classmethod
54
27
@memoize
55
28
def for_node (cls , node ):
56
29
from graphene .relay .utils import is_node
57
30
assert is_node (node ), 'ObjectTypes in a edge have to be Nodes'
58
- return type ('%sEdge ' % node ._meta .type_name , (cls , ), {'node_type' : node })
31
+ return type ('%s%s ' % ( node ._meta .type_name , cls . _meta . type_name ) , (cls , ), {'node_type' : node })
59
32
60
33
61
34
class Connection (ObjectType ):
62
35
'''A connection to a list of items.'''
36
+ class Meta :
37
+ type_name = 'DefaultConnection'
38
+
63
39
page_info = Field (PageInfo , required = True , description = 'The Information to aid in pagination' )
64
40
edges = ListField (lambda field : field .object_type .edge_type , description = 'Information to aid in pagination.' )
65
41
42
+ _connection_data = None
43
+
66
44
@classmethod
67
45
@memoize
68
46
def for_node (cls , node , edge_type = None ):
69
47
from graphene .relay .utils import is_node
70
48
edge_type = edge_type or Edge
71
49
assert is_node (node ), 'ObjectTypes in a connection have to be Nodes'
72
- return type ('%sConnection' % node ._meta .type_name , (cls , ), {'edge_type' : edge_type .for_node (node )})
50
+ return type ('%s%s' % (node ._meta .type_name , cls ._meta .type_name ), (cls , ), {'edge_type' : edge_type .for_node (node )})
51
+
52
+ def set_connection_data (self , data ):
53
+ self ._connection_data = data
54
+
55
+ def get_connection_data (self ):
56
+ return self ._connection_data
57
+
58
+
59
+ class BaseNode (object ):
60
+ @classmethod
61
+ def _prepare_class (cls ):
62
+ from graphene .relay .utils import is_node
63
+ if is_node (cls ):
64
+ assert hasattr (
65
+ cls , 'get_node' ), 'get_node classmethod not found in %s Node' % cls
66
+
67
+ @classmethod
68
+ def to_global_id (cls , instance , args , info ):
69
+ type_name = cls ._meta .type_name
70
+ return to_global_id (type_name , instance .id )
71
+
72
+ connection_type = Connection
73
+ edge_type = Edge
74
+
75
+ @classmethod
76
+ def get_connection_type (cls ):
77
+ return cls .connection_type
78
+
79
+ @classmethod
80
+ def get_edge_type (cls ):
81
+ return cls .edge_type
82
+
83
+
84
+ class Node (BaseNode , Interface ):
85
+ '''An object with an ID'''
86
+ id = GlobalIDField ()
0 commit comments