1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Date: 10.05.16 |
4
|
|
|
* |
5
|
|
|
* @author Portey Vasil <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Youshido\GraphQL\Relay\Connection; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use Youshido\GraphQL\Relay\Type\PageInfoType; |
12
|
|
|
use Youshido\GraphQL\Type\AbstractType; |
13
|
|
|
use Youshido\GraphQL\Type\ListType\ListType; |
14
|
|
|
use Youshido\GraphQL\Type\NonNullType; |
15
|
|
|
use Youshido\GraphQL\Type\Object\ObjectType; |
16
|
|
|
use Youshido\GraphQL\Type\Scalar\IntType; |
17
|
|
|
use Youshido\GraphQL\Type\TypeMap; |
18
|
|
|
|
19
|
|
|
class Connection |
20
|
|
|
{ |
21
|
|
|
|
22
|
1 |
|
public static function connectionArgs() |
23
|
|
|
{ |
24
|
1 |
|
return array_merge(self::forwardArgs(), self::backwardArgs()); |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
public static function forwardArgs() |
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
1 |
|
'after' => ['type' => TypeMap::TYPE_STRING], |
31
|
1 |
|
'first' => ['type' => TypeMap::TYPE_INT] |
32
|
1 |
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
public static function backwardArgs() |
36
|
|
|
{ |
37
|
|
|
return [ |
38
|
1 |
|
'before' => ['type' => TypeMap::TYPE_STRING], |
39
|
1 |
|
'last' => ['type' => TypeMap::TYPE_INT] |
40
|
1 |
|
]; |
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
|
|
|
'totalCount' => [ |
61
|
2 |
|
'type' => new NonNullType(new IntType()), |
62
|
2 |
|
'description' => 'How many nodes.', |
63
|
2 |
|
'resolve' => [__CLASS__, 'getTotalCount'], |
64
|
2 |
|
], |
65
|
|
|
'node' => [ |
66
|
2 |
|
'type' => $type, |
67
|
2 |
|
'description' => 'The item at the end of the edge', |
68
|
2 |
|
'resolve' => [__CLASS__, 'getNode'], |
69
|
2 |
|
], |
70
|
|
|
'cursor' => [ |
71
|
2 |
|
'type' => TypeMap::TYPE_STRING, |
72
|
|
|
'description' => 'A cursor for use in pagination' |
73
|
2 |
|
] |
74
|
2 |
|
], $edgeFields) |
75
|
2 |
|
]); |
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
|
|
|
'pageInfo' => [ |
98
|
1 |
|
'type' => new NonNullType(new PageInfoType()), |
99
|
1 |
|
'description' => 'Information to aid in pagination.', |
100
|
1 |
|
'resolve' => [__CLASS__, 'getPageInfo'], |
101
|
1 |
|
], |
102
|
|
|
'edges' => [ |
103
|
1 |
|
'type' => new ListType(self::edgeDefinition($type, $name, $config)), |
104
|
1 |
|
'description' => 'A list of edges.', |
105
|
1 |
|
'resolve' => [__CLASS__, 'getEdges'], |
106
|
|
|
] |
107
|
1 |
|
], $connectionFields) |
108
|
1 |
|
]); |
109
|
|
|
|
110
|
1 |
|
return $connectionType; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function getTotalCount($value) |
114
|
|
|
{ |
115
|
|
|
return isset($value['totalCount']) ? $value['totalCount'] : -1; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public static function getEdges($value) |
119
|
|
|
{ |
120
|
|
|
return isset($value['edges']) ? $value['edges'] : null; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public static function getPageInfo($value) |
124
|
|
|
{ |
125
|
|
|
return isset($value['pageInfo']) ? $value['pageInfo'] : null; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public static function getNode($value) |
129
|
|
|
{ |
130
|
|
|
return isset($value['node']) ? $value['node'] : null; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|