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