1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Type; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\GraphQL; |
6
|
|
|
use Digia\GraphQL\Type\Definition\EnumType; |
7
|
|
|
use Digia\GraphQL\Type\Definition\Field; |
8
|
|
|
use Digia\GraphQL\Type\Definition\NamedTypeInterface; |
9
|
|
|
use Digia\GraphQL\Type\Definition\ObjectType; |
10
|
|
|
use Digia\GraphQL\Type\Definition\TypeInterface; |
11
|
|
|
use function Digia\GraphQL\Util\arraySome; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @return ObjectType |
15
|
|
|
*/ |
16
|
|
|
function __Schema(): ObjectType |
17
|
|
|
{ |
18
|
|
|
return GraphQL::get(GraphQL::INTROSPECTION_SCHEMA); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return ObjectType |
23
|
|
|
*/ |
24
|
|
|
function __Directive(): ObjectType |
25
|
|
|
{ |
26
|
|
|
return GraphQL::get(GraphQL::INTROSPECTION_DIRECTIVE); |
27
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return EnumType |
32
|
|
|
*/ |
33
|
|
|
function __DirectiveLocation(): EnumType |
34
|
|
|
{ |
35
|
|
|
return GraphQL::get(GraphQL::INTROSPECTION_DIRECTIVE_LOCATION); |
36
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return ObjectType |
41
|
|
|
*/ |
42
|
|
|
function __Type(): ObjectType |
43
|
|
|
{ |
44
|
|
|
return GraphQL::get(GraphQL::INTROSPECTION_TYPE); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return ObjectType |
49
|
|
|
*/ |
50
|
|
|
function __Field(): ObjectType |
51
|
|
|
{ |
52
|
|
|
return GraphQL::get(GraphQL::INTROSPECTION_FIELD); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return ObjectType |
57
|
|
|
*/ |
58
|
|
|
function __InputValue(): ObjectType |
59
|
|
|
{ |
60
|
|
|
return GraphQL::get(GraphQL::INTROSPECTION_INPUT_VALUE); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return ObjectType |
65
|
|
|
*/ |
66
|
|
|
function __EnumValue(): ObjectType |
67
|
|
|
{ |
68
|
|
|
return GraphQL::get(GraphQL::INTROSPECTION_ENUM_VALUE); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
function __TypeKind(): EnumType |
72
|
|
|
{ |
73
|
|
|
return GraphQL::get(GraphQL::INTROSPECTION_TYPE_KIND); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return Field |
78
|
|
|
* @throws \TypeError |
79
|
|
|
*/ |
80
|
|
|
function SchemaMetaFieldDefinition(): Field |
81
|
|
|
{ |
82
|
|
|
return new Field([ |
83
|
|
|
'name' => '__schema', |
84
|
|
|
'type' => GraphQLNonNull(__Schema()), |
85
|
|
|
'description' => 'Access the current type schema of this server.', |
86
|
|
|
'resolve' => function ($source, $args, $context, $info): SchemaInterface { |
87
|
|
|
[$schema] = $info; |
88
|
|
|
return $schema; |
89
|
|
|
} |
90
|
|
|
]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return Field |
95
|
|
|
* @throws \TypeError |
96
|
|
|
*/ |
97
|
|
|
function TypeMetaFieldDefinition(): Field |
98
|
|
|
{ |
99
|
|
|
return new Field([ |
100
|
|
|
'name' => '__type', |
101
|
|
|
'type' => __Type(), |
102
|
|
|
'description' => 'Request the type information of a single type.', |
103
|
|
|
'args' => [ |
104
|
|
|
'name' => ['type' => GraphQLNonNull(GraphQLString())], |
105
|
|
|
], |
106
|
|
|
'resolve' => function ($source, $args, $context, $info): TypeInterface { |
107
|
|
|
/** @var SchemaInterface $schema */ |
108
|
|
|
[$name] = $args; |
109
|
|
|
[$schema] = $info; |
110
|
|
|
return $schema->getType($name); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return Field |
117
|
|
|
* @throws \TypeError |
118
|
|
|
*/ |
119
|
|
|
function TypeNameMetaFieldDefinition(): Field |
120
|
|
|
{ |
121
|
|
|
return new Field([ |
122
|
|
|
'name' => '__typename', |
123
|
|
|
'type' => GraphQLNonNull(GraphQLString()), |
124
|
|
|
'description' => 'The name of the current Object type at runtime.', |
125
|
|
|
'resolve' => function ($source, $args, $context, $info): string { |
126
|
|
|
/** @var NamedTypeInterface $parentType */ |
127
|
|
|
[$parentType] = $info; |
128
|
|
|
return $parentType->getName(); |
129
|
|
|
} |
130
|
|
|
]); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
function introspectionTypes(): array |
137
|
|
|
{ |
138
|
|
|
return [ |
139
|
|
|
__Schema(), |
140
|
|
|
__Directive(), |
141
|
|
|
__DirectiveLocation(), |
142
|
|
|
__Type(), |
143
|
|
|
__Field(), |
144
|
|
|
__InputValue(), |
145
|
|
|
__EnumValue(), |
146
|
|
|
__TypeKind(), |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param TypeInterface $type |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
function isIntrospectionType(TypeInterface $type): bool |
155
|
|
|
{ |
156
|
|
|
return arraySome( |
157
|
|
|
introspectionTypes(), |
158
|
|
|
function (TypeInterface $introspectionType) use ($type) { |
159
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
160
|
|
|
return $type->getName() === $introspectionType->getName(); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|