|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Date: 03.12.15 |
|
4
|
|
|
* |
|
5
|
|
|
* @author Portey Vasil <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Youshido\GraphQL\Introspection; |
|
9
|
|
|
|
|
10
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
|
11
|
|
|
use Youshido\GraphQL\Field\Field; |
|
12
|
|
|
use Youshido\GraphQL\Introspection\Traits\TypeCollectorTrait; |
|
13
|
|
|
use Youshido\GraphQL\Type\AbstractType; |
|
14
|
|
|
use Youshido\GraphQL\Type\CompositeTypeInterface; |
|
15
|
|
|
use Youshido\GraphQL\Type\Enum\AbstractEnumType; |
|
16
|
|
|
use Youshido\GraphQL\Type\InputObject\AbstractInputObjectType; |
|
17
|
|
|
use Youshido\GraphQL\Type\ListType\ListType; |
|
18
|
|
|
use Youshido\GraphQL\Type\Object\AbstractObjectType; |
|
19
|
|
|
use Youshido\GraphQL\Type\Scalar\BooleanType; |
|
20
|
|
|
use Youshido\GraphQL\Type\TypeMap; |
|
21
|
|
|
use Youshido\GraphQL\Type\Union\AbstractUnionType; |
|
22
|
|
|
|
|
23
|
|
|
class QueryType extends AbstractObjectType |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
use TypeCollectorTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return String type name |
|
30
|
|
|
*/ |
|
31
|
27 |
|
public function getName() |
|
32
|
|
|
{ |
|
33
|
27 |
|
return '__Type'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
3 |
|
public function resolveOfType(AbstractType $value) |
|
37
|
|
|
{ |
|
38
|
3 |
|
if ($value instanceof CompositeTypeInterface) { |
|
39
|
2 |
|
return $value->getTypeOf(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
3 |
|
return null; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
3 |
|
public function resolveInputFields($value) |
|
46
|
|
|
{ |
|
47
|
3 |
|
if ($value instanceof AbstractInputObjectType) { |
|
48
|
|
|
/** @var AbstractObjectType $value */ |
|
49
|
|
|
return $value->getConfig()->getFields(); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
return null; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
public function resolveEnumValues($value, $args) |
|
56
|
|
|
{ |
|
57
|
|
|
/** @var $value AbstractType|AbstractEnumType */ |
|
58
|
2 |
|
if ($value && $value->getKind() == TypeMap::KIND_ENUM) { |
|
59
|
2 |
|
$data = []; |
|
60
|
2 |
|
foreach ($value->getValues() as $enumValue) { |
|
|
|
|
|
|
61
|
2 |
|
if(!$args['includeDeprecated'] && (isset($enumValue['isDeprecated']) && $enumValue['isDeprecated'])) { |
|
62
|
|
|
continue; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
if (!array_key_exists('description', $enumValue)) { |
|
66
|
2 |
|
$enumValue['description'] = ''; |
|
67
|
2 |
|
} |
|
68
|
2 |
|
if (!array_key_exists('isDeprecated', $enumValue)) { |
|
69
|
2 |
|
$enumValue['isDeprecated'] = false; |
|
70
|
2 |
|
} |
|
71
|
2 |
|
if (!array_key_exists('deprecationReason', $enumValue)) { |
|
72
|
2 |
|
$enumValue['deprecationReason'] = ''; |
|
73
|
2 |
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
$data[] = $enumValue; |
|
76
|
2 |
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
return $data; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
return null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
4 |
|
public function resolveFields($value, $args) |
|
85
|
|
|
{ |
|
86
|
|
|
/** @var AbstractType $value */ |
|
87
|
4 |
|
if (!$value || |
|
88
|
4 |
|
in_array($value->getKind(), [TypeMap::KIND_SCALAR, TypeMap::KIND_UNION, TypeMap::KIND_INPUT_OBJECT, TypeMap::KIND_ENUM]) |
|
89
|
4 |
|
) { |
|
90
|
3 |
|
return null; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** @var AbstractObjectType $value */ |
|
94
|
4 |
|
return array_filter($value->getConfig()->getFields(), function ($field) use ($args) { |
|
|
|
|
|
|
95
|
|
|
/** @var $field Field */ |
|
96
|
4 |
|
if (in_array($field->getName(), ['__type', '__schema']) || (!$args['includeDeprecated'] && $field->isDeprecated())) { |
|
97
|
4 |
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
4 |
|
return true; |
|
101
|
4 |
|
}); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
3 |
|
public function resolveInterfaces($value) |
|
105
|
|
|
{ |
|
106
|
|
|
/** @var $value AbstractType */ |
|
107
|
3 |
|
if ($value->getKind() == TypeMap::KIND_OBJECT) { |
|
108
|
|
|
/** @var $value AbstractObjectType */ |
|
109
|
3 |
|
return $value->getConfig()->getInterfaces() ?: []; |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
2 |
|
return null; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
3 |
|
public function resolvePossibleTypes($value, $args, ResolveInfo $info) |
|
116
|
|
|
{ |
|
117
|
|
|
/** @var $value AbstractObjectType */ |
|
118
|
3 |
|
if ($value->getKind() == TypeMap::KIND_INTERFACE) { |
|
119
|
2 |
|
$this->collectTypes($info->getExecutionContext()->getSchema()->getQueryType()); |
|
120
|
|
|
|
|
121
|
2 |
|
$possibleTypes = []; |
|
122
|
2 |
|
foreach ($this->types as $type) { |
|
123
|
|
|
/** @var $type AbstractObjectType */ |
|
124
|
2 |
|
if ($type->getKind() == TypeMap::KIND_OBJECT) { |
|
125
|
2 |
|
$interfaces = $type->getConfig()->getInterfaces(); |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
2 |
|
if ($interfaces) { |
|
128
|
2 |
|
foreach ($interfaces as $interface) { |
|
129
|
2 |
|
if ($interface->getName() == $value->getName()) { |
|
130
|
2 |
|
$possibleTypes[] = $type; |
|
131
|
2 |
|
} |
|
132
|
2 |
|
} |
|
133
|
2 |
|
} |
|
134
|
2 |
|
} |
|
135
|
2 |
|
} |
|
136
|
|
|
|
|
137
|
2 |
|
return $possibleTypes; |
|
138
|
3 |
|
} elseif ($value->getKind() == TypeMap::KIND_UNION) { |
|
139
|
|
|
/** @var $value AbstractUnionType */ |
|
140
|
1 |
|
return $value->getTypes(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
3 |
|
return null; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
7 |
|
public function build($config) |
|
147
|
|
|
{ |
|
148
|
|
|
$config |
|
149
|
7 |
|
->addField('name', TypeMap::TYPE_STRING) |
|
150
|
7 |
|
->addField('kind', TypeMap::TYPE_STRING) |
|
151
|
7 |
|
->addField('description', TypeMap::TYPE_STRING) |
|
152
|
7 |
|
->addField('ofType', [ |
|
153
|
7 |
|
'type' => new QueryType(), |
|
154
|
7 |
|
'resolve' => [$this, 'resolveOfType'] |
|
155
|
7 |
|
]) |
|
156
|
7 |
|
->addField(new Field([ |
|
157
|
7 |
|
'name' => 'inputFields', |
|
158
|
7 |
|
'type' => new ListType(new InputValueType()), |
|
159
|
7 |
|
'resolve' => [$this, 'resolveInputFields'] |
|
160
|
7 |
|
])) |
|
161
|
7 |
|
->addField(new Field([ |
|
162
|
7 |
|
'name' => 'enumValues', |
|
163
|
|
|
'args' => [ |
|
164
|
|
|
'includeDeprecated' => [ |
|
165
|
7 |
|
'type' => new BooleanType(), |
|
166
|
|
|
'default' => false |
|
167
|
7 |
|
] |
|
168
|
7 |
|
], |
|
169
|
7 |
|
'type' => new ListType(new EnumValueType()), |
|
170
|
7 |
|
'resolve' => [$this, 'resolveEnumValues'] |
|
171
|
7 |
|
])) |
|
172
|
7 |
|
->addField(new Field([ |
|
173
|
7 |
|
'name' => 'fields', |
|
174
|
|
|
'args' => [ |
|
175
|
|
|
'includeDeprecated' => [ |
|
176
|
7 |
|
'type' => new BooleanType(), |
|
177
|
|
|
'default' => false |
|
178
|
7 |
|
] |
|
179
|
7 |
|
], |
|
180
|
7 |
|
'type' => new ListType(new FieldType()), |
|
181
|
7 |
|
'resolve' => [$this, 'resolveFields'] |
|
182
|
7 |
|
])) |
|
183
|
7 |
|
->addField(new Field([ |
|
184
|
7 |
|
'name' => 'interfaces', |
|
185
|
7 |
|
'type' => new ListType(new QueryType()), |
|
186
|
7 |
|
'resolve' => [$this, 'resolveInterfaces'] |
|
187
|
7 |
|
])) |
|
188
|
7 |
|
->addField('possibleTypes', [ |
|
189
|
7 |
|
'type' => new ListType(new QueryType()), |
|
190
|
7 |
|
'resolve' => [$this, 'resolvePossibleTypes'] |
|
191
|
7 |
|
]); |
|
192
|
7 |
|
} |
|
193
|
|
|
|
|
194
|
6 |
|
public function isValidValue($value) |
|
195
|
|
|
{ |
|
196
|
6 |
|
return $value instanceof AbstractType; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: