|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQL\Tests\Validator; |
|
6
|
|
|
|
|
7
|
|
|
use GraphQL\GraphQL; |
|
8
|
|
|
use GraphQL\Language\DirectiveLocation; |
|
9
|
|
|
use GraphQL\Type\Definition\Directive; |
|
10
|
|
|
use GraphQL\Type\Definition\FieldArgument; |
|
11
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
12
|
|
|
use GraphQL\Type\Definition\Type; |
|
13
|
|
|
use GraphQL\Type\Schema; |
|
14
|
|
|
use function array_merge; |
|
15
|
|
|
|
|
16
|
|
|
class QuerySecuritySchema |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var Schema */ |
|
19
|
|
|
private static $schema; |
|
20
|
|
|
|
|
21
|
|
|
/** @var Directive */ |
|
22
|
|
|
private static $fooDirective; |
|
23
|
|
|
|
|
24
|
|
|
/** @var ObjectType */ |
|
25
|
|
|
private static $dogType; |
|
26
|
|
|
|
|
27
|
|
|
/** @var ObjectType */ |
|
28
|
|
|
private static $humanType; |
|
29
|
|
|
|
|
30
|
|
|
/** @var ObjectType */ |
|
31
|
|
|
private static $queryRootType; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return Schema |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function buildSchema() |
|
37
|
|
|
{ |
|
38
|
|
|
if (self::$schema !== null) { |
|
39
|
|
|
return self::$schema; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
self::$schema = new Schema([ |
|
43
|
|
|
'query' => static::buildQueryRootType(), |
|
44
|
|
|
'directives' => array_merge(GraphQL::getStandardDirectives(), [static::buildFooDirective()]), |
|
45
|
|
|
]); |
|
46
|
|
|
|
|
47
|
|
|
return self::$schema; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public static function buildQueryRootType() |
|
51
|
|
|
{ |
|
52
|
|
|
if (self::$queryRootType !== null) { |
|
53
|
|
|
return self::$queryRootType; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
self::$queryRootType = new ObjectType([ |
|
57
|
|
|
'name' => 'QueryRoot', |
|
58
|
|
|
'fields' => [ |
|
59
|
|
|
'human' => [ |
|
60
|
|
|
'type' => self::buildHumanType(), |
|
61
|
|
|
'args' => ['name' => ['type' => Type::string()]], |
|
62
|
|
|
], |
|
63
|
|
|
], |
|
64
|
|
|
]); |
|
65
|
|
|
|
|
66
|
|
|
return self::$queryRootType; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public static function buildHumanType() |
|
70
|
|
|
{ |
|
71
|
|
|
if (self::$humanType !== null) { |
|
72
|
|
|
return self::$humanType; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
self::$humanType = new ObjectType( |
|
76
|
|
|
[ |
|
77
|
|
|
'name' => 'Human', |
|
78
|
|
|
'fields' => static function () { |
|
79
|
|
|
return [ |
|
80
|
|
|
'firstName' => ['type' => Type::nonNull(Type::string())], |
|
81
|
|
|
'dogs' => [ |
|
82
|
|
|
'type' => Type::nonNull( |
|
83
|
|
|
Type::listOf( |
|
84
|
|
|
Type::nonNull(self::buildDogType()) |
|
85
|
|
|
) |
|
86
|
|
|
), |
|
87
|
|
|
'complexity' => static function ($childrenComplexity, $args) { |
|
88
|
|
|
$complexity = isset($args['name']) ? 1 : 10; |
|
89
|
|
|
|
|
90
|
|
|
return $childrenComplexity + $complexity; |
|
91
|
|
|
}, |
|
92
|
|
|
'args' => ['name' => ['type' => Type::string()]], |
|
93
|
|
|
], |
|
94
|
|
|
]; |
|
95
|
|
|
}, |
|
96
|
|
|
] |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
return self::$humanType; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public static function buildDogType() |
|
103
|
|
|
{ |
|
104
|
|
|
if (self::$dogType !== null) { |
|
105
|
|
|
return self::$dogType; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
self::$dogType = new ObjectType( |
|
109
|
|
|
[ |
|
110
|
|
|
'name' => 'Dog', |
|
111
|
|
|
'fields' => [ |
|
112
|
|
|
'name' => ['type' => Type::nonNull(Type::string())], |
|
113
|
|
|
'master' => [ |
|
114
|
|
|
'type' => self::buildHumanType(), |
|
115
|
|
|
], |
|
116
|
|
|
], |
|
117
|
|
|
] |
|
118
|
|
|
); |
|
119
|
|
|
|
|
120
|
|
|
return self::$dogType; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public static function buildFooDirective() : Directive |
|
124
|
|
|
{ |
|
125
|
|
|
if (self::$fooDirective !== null) { |
|
126
|
|
|
return self::$fooDirective; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
self::$fooDirective = new Directive([ |
|
130
|
|
|
'name' => 'foo', |
|
131
|
|
|
'locations' => [DirectiveLocation::FIELD], |
|
132
|
|
|
'args' => [new FieldArgument([ |
|
133
|
|
|
'name' => 'bar', |
|
134
|
|
|
'type' => Type::nonNull(Type::boolean()), |
|
135
|
|
|
'defaultValue' => ' ', |
|
136
|
|
|
]), |
|
137
|
|
|
], |
|
138
|
|
|
]); |
|
139
|
|
|
|
|
140
|
|
|
return self::$fooDirective; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|