-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathWPType.php
42 lines (33 loc) · 972 Bytes
/
WPType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace Mohiohio\GraphQLWP;
use GraphQL\Type\Definition\Type;
use GraphQLRelay\Relay;
class WPType
{
private static $types;
static function get($className)
{
return self::$types[$className] ?? (self::$types[$className] = self::initType($className));
}
static function getEdge($className, $nodeType)
{
return self::$types[$className . 'Edge'] ??
(self::$types[$className . 'Edge'] = Relay::edgeType(['nodeType' => Type::nonNull($nodeType), 'name' => $nodeType->name]));
}
static function getConnection($className, $nodeType)
{
$key = $className . 'Connection';
return self::$types[$key] ??
(self::$types[$key] = Relay::connectionType([
'nodeType' => $nodeType,
'edgeType' => Type::nonNull(static::getEdge($className, $nodeType))
]));
}
protected static function initType($className)
{
if (!class_exists($className)) {
return null;
}
return new $className();
}
}