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