1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Tests\Request\Validator\Rule; |
13
|
|
|
|
14
|
|
|
use GraphQL\Schema as GraphQLSchema; |
15
|
|
|
use GraphQL\Type\Definition\ObjectType; |
16
|
|
|
use GraphQL\Type\Definition\Type; |
17
|
|
|
|
18
|
|
|
class Schema |
19
|
|
|
{ |
20
|
|
|
private static $schema; |
21
|
|
|
|
22
|
|
|
private static $dogType; |
23
|
|
|
|
24
|
|
|
private static $humanType; |
25
|
|
|
|
26
|
|
|
private static $queryRootType; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return GraphQLSchema |
30
|
|
|
*/ |
31
|
|
|
public static function buildSchema() |
32
|
|
|
{ |
33
|
|
|
if (null !== self::$schema) { |
34
|
|
|
return self::$schema; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
static::buildHumanType(); |
38
|
|
|
static::buildDogType(); |
39
|
|
|
|
40
|
|
|
self::$schema = new GraphQLSchema(static::buildQueryRootType()); |
41
|
|
|
|
42
|
|
|
return self::$schema; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public static function buildQueryRootType() |
46
|
|
|
{ |
47
|
|
|
if (null !== self::$queryRootType) { |
48
|
|
|
return self::$queryRootType; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
self::$queryRootType = new ObjectType([ |
52
|
|
|
'name' => 'QueryRoot', |
53
|
|
|
'fields' => [ |
54
|
|
|
'human' => [ |
55
|
|
|
'type' => self::buildHumanType(), |
56
|
|
|
], |
57
|
|
|
], |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
return self::$queryRootType; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function buildHumanType() |
64
|
|
|
{ |
65
|
|
|
if (null !== self::$humanType) { |
66
|
|
|
return self::$humanType; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
self::$humanType = new ObjectType( |
70
|
|
|
[ |
71
|
|
|
'name' => 'Human', |
72
|
|
|
'fields' => [ |
73
|
|
|
'firstName' => ['type' => Type::nonNull(Type::string())], |
74
|
|
|
'Dog' => [ |
75
|
|
|
'type' => function () { |
76
|
|
|
return Type::nonNull( |
77
|
|
|
Type::listOf( |
78
|
|
|
Type::nonNull(self::buildDogType()) |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
}, |
82
|
|
|
], |
83
|
|
|
], |
84
|
|
|
] |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return self::$humanType; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public static function buildDogType() |
91
|
|
|
{ |
92
|
|
|
if (null !== self::$dogType) { |
93
|
|
|
return self::$dogType; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
self::$dogType = new ObjectType( |
97
|
|
|
[ |
98
|
|
|
'name' => 'Dog', |
99
|
|
|
'fields' => [ |
100
|
|
|
'name' => ['type' => Type::nonNull(Type::string())], |
101
|
|
|
'master' => [ |
102
|
|
|
'type' => self::buildHumanType(), |
103
|
|
|
], |
104
|
|
|
], |
105
|
|
|
] |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
return self::$dogType; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|