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