1 | <?php |
||
19 | class Connection |
||
20 | { |
||
21 | |||
22 | 1 | public static function connectionArgs() |
|
26 | |||
27 | 1 | public static function forwardArgs() |
|
28 | { |
||
29 | return [ |
||
30 | 1 | 'after' => ['type' => TypeMap::TYPE_STRING], |
|
31 | 'first' => ['type' => TypeMap::TYPE_INT] |
||
32 | ]; |
||
33 | } |
||
34 | |||
35 | 1 | public static function backwardArgs() |
|
36 | { |
||
37 | return [ |
||
38 | 1 | 'before' => ['type' => TypeMap::TYPE_STRING], |
|
39 | 'last' => ['type' => TypeMap::TYPE_INT] |
||
40 | ]; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param AbstractType $type |
||
45 | * @param null|string $name |
||
46 | * @param array $config |
||
47 | * @option string edgeFields |
||
48 | * |
||
49 | * @return ObjectType |
||
50 | */ |
||
51 | 2 | public static function edgeDefinition(AbstractType $type, $name = null, $config = []) |
|
52 | { |
||
53 | 2 | $name = $name ?: $type->getName(); |
|
54 | 2 | $edgeFields = !empty($config['edgeFields']) ? $config['edgeFields'] : []; |
|
55 | |||
56 | 2 | $edgeType = new ObjectType([ |
|
57 | 2 | 'name' => $name . 'Edge', |
|
58 | 2 | 'description' => 'An edge in a connection.', |
|
59 | 2 | 'fields' => array_merge([ |
|
60 | 2 | 'totalCount' => [ |
|
61 | 2 | 'type' => new NonNullType(new IntType()), |
|
62 | 2 | 'description' => 'How many nodes.', |
|
63 | 'resolve' => [__CLASS__, 'getTotalCount'], |
||
64 | ], |
||
65 | 'node' => [ |
||
66 | 2 | 'type' => $type, |
|
67 | 2 | 'description' => 'The item at the end of the edge', |
|
68 | 'resolve' => [__CLASS__, 'getNode'], |
||
69 | ], |
||
70 | 'cursor' => [ |
||
71 | 'type' => TypeMap::TYPE_STRING, |
||
72 | 'description' => 'A cursor for use in pagination' |
||
73 | ] |
||
74 | 2 | ], $edgeFields) |
|
75 | ]); |
||
76 | |||
77 | 2 | return $edgeType; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param AbstractType $type |
||
82 | * @param null|string $name |
||
83 | * @param array $config |
||
84 | * @option string connectionFields |
||
85 | * |
||
86 | * @return ObjectType |
||
87 | */ |
||
88 | 1 | public static function connectionDefinition(AbstractType $type, $name = null, $config = []) |
|
89 | { |
||
90 | 1 | $name = $name ?: $type->getName(); |
|
91 | 1 | $connectionFields = !empty($config['connectionFields']) ? $config['connectionFields'] : []; |
|
92 | |||
93 | 1 | $connectionType = new ObjectType([ |
|
94 | 1 | 'name' => $name . 'Connection', |
|
95 | 1 | 'description' => 'A connection to a list of items.', |
|
96 | 1 | 'fields' => array_merge([ |
|
97 | 1 | 'pageInfo' => [ |
|
98 | 1 | 'type' => new NonNullType(new PageInfoType()), |
|
99 | 1 | 'description' => 'Information to aid in pagination.', |
|
100 | 'resolve' => [__CLASS__, 'getPageInfo'], |
||
101 | ], |
||
102 | 'edges' => [ |
||
103 | 1 | 'type' => new ListType(self::edgeDefinition($type, $name, $config)), |
|
104 | 1 | 'description' => 'A list of edges.', |
|
105 | 'resolve' => [__CLASS__, 'getEdges'], |
||
106 | ] |
||
107 | 1 | ], $connectionFields) |
|
108 | ]); |
||
109 | |||
110 | 1 | return $connectionType; |
|
111 | } |
||
112 | |||
113 | public static function getTotalCount($value) |
||
117 | |||
118 | public static function getEdges($value) |
||
122 | |||
123 | public static function getPageInfo($value) |
||
127 | |||
128 | public static function getNode($value) |
||
132 | } |
||
133 |