/
Entry.php
258 lines (246 loc) · 10.4 KB
/
Entry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\gql\interfaces\elements;
use Craft;
use craft\gql\arguments\elements\Entry as EntryArguments;
use craft\gql\GqlEntityRegistry;
use craft\gql\interfaces\elements\Entry as EntryInterface;
use craft\gql\interfaces\Structure;
use craft\gql\types\DateTime;
use craft\gql\types\generators\EntryType;
use craft\helpers\Gql;
use craft\services\Gql as GqlService;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\Type;
/**
* Class Entry
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.3.0
*/
class Entry extends Structure
{
/**
* @inheritdoc
*/
public static function getTypeGenerator(): string
{
return EntryType::class;
}
/**
* @inheritdoc
*/
public static function getType(): Type
{
if ($type = GqlEntityRegistry::getEntity(self::getName())) {
return $type;
}
$type = GqlEntityRegistry::createEntity(self::getName(), new InterfaceType([
'name' => static::getName(),
'fields' => self::class . '::getFieldDefinitions',
'description' => 'This is the interface implemented by all entries.',
'resolveType' => self::class . '::resolveElementTypeName',
]));
EntryType::generateTypes();
return $type;
}
/**
* @inheritdoc
*/
public static function getName(): string
{
return 'EntryInterface';
}
/**
* @inheritdoc
*/
public static function getFieldDefinitions(): array
{
return Craft::$app->getGql()->prepareFieldDefinitions(array_merge(parent::getFieldDefinitions(), static::getDraftFieldDefinitions(), self::getConditionalFields(), [
'canonicalId' => [
'name' => 'canonicalId',
'type' => Type::int(),
'description' => 'Returns the entry’s canonical ID.',
],
'canonicalUid' => [
'name' => 'canonicalUid',
'type' => Type::string(),
'description' => 'Returns the entry’s canonical UUID.',
],
'sourceId' => [
'name' => 'sourceId',
'type' => Type::int(),
'description' => 'Returns the entry’s canonical ID.',
'deprecationReason' => 'this field has been deprecated since Craft 3.7.7. Use `canonicalId` instead.',
],
'sourceUid' => [
'name' => 'sourceUid',
'type' => Type::string(),
'description' => 'Returns the entry’s canonical UUID.',
'deprecationReason' => 'this field has been deprecated since Craft 3.7.7. Use `canonicalUid` instead.',
],
'sectionId' => [
'name' => 'sectionId',
'type' => Type::nonNull(Type::int()),
'description' => 'The ID of the section that contains the entry.',
],
'sectionHandle' => [
'name' => 'sectionHandle',
'type' => Type::nonNull(Type::string()),
'description' => 'The handle of the section that contains the entry.',
'complexity' => Gql::singleQueryComplexity(),
],
'typeId' => [
'name' => 'typeId',
'type' => Type::nonNull(Type::int()),
'description' => 'The ID of the entry type that contains the entry.',
],
'typeHandle' => [
'name' => 'typeHandle',
'type' => Type::nonNull(Type::string()),
'description' => 'The handle of the entry type that contains the entry.',
'complexity' => Gql::singleQueryComplexity(),
],
'postDate' => [
'name' => 'postDate',
'type' => DateTime::getType(),
'description' => 'The entry’s post date.',
],
'expiryDate' => [
'name' => 'expiryDate',
'type' => DateTime::getType(),
'description' => 'The expiry date of the entry.',
],
'children' => [
'name' => 'children',
'args' => EntryArguments::getArguments(),
'type' => Type::nonNull(Type::listOf(Type::nonNull(static::getType()))),
'description' => 'The entry’s children, if the section is a structure. Accepts the same arguments as the `entries` query.',
'complexity' => Gql::relatedArgumentComplexity(GqlService::GRAPHQL_COMPLEXITY_EAGER_LOAD),
],
'descendants' => [
'name' => 'descendants',
'args' => EntryArguments::getArguments(),
'type' => Type::nonNull(Type::listOf(Type::nonNull(static::getType()))),
'description' => 'The entry’s descendants, if the section is a structure. Accepts the same arguments as the `entries` query.',
'complexity' => Gql::relatedArgumentComplexity(GqlService::GRAPHQL_COMPLEXITY_EAGER_LOAD),
],
'parent' => [
'name' => 'parent',
'args' => EntryArguments::getArguments(),
'type' => EntryInterface::getType(),
'description' => 'The entry’s parent, if the section is a structure.',
'complexity' => Gql::relatedArgumentComplexity(GqlService::GRAPHQL_COMPLEXITY_EAGER_LOAD),
],
'ancestors' => [
'name' => 'ancestors',
'args' => EntryArguments::getArguments(),
'type' => Type::nonNull(Type::listOf(Type::nonNull(static::getType()))),
'description' => 'The entry’s ancestors, if the section is a structure. Accepts the same arguments as the `entries` query.',
'complexity' => Gql::relatedArgumentComplexity(GqlService::GRAPHQL_COMPLEXITY_EAGER_LOAD),
],
'url' => [
'name' => 'url',
'type' => Type::string(),
'description' => 'The element’s full URL',
],
'localized' => [
'name' => 'localized',
'args' => EntryArguments::getArguments(),
'type' => Type::nonNull(Type::listOf(Type::nonNull(static::getType()))),
'description' => 'The same element in other locales.',
'complexity' => Gql::eagerLoadComplexity(),
],
'prev' => [
'name' => 'prev',
'type' => self::getType(),
'args' => EntryArguments::getArguments(),
'description' => 'Returns the previous element relative to this one, from a given set of criteria.',
'complexity' => function($childrenComplexity, $args) {
return $childrenComplexity + GqlService::GRAPHQL_COMPLEXITY_NPLUS1 * (int)!empty($args);
},
],
'next' => [
'name' => 'next',
'type' => self::getType(),
'args' => EntryArguments::getArguments(),
'description' => 'Returns the next element relative to this one, from a given set of criteria.',
'complexity' => function($childrenComplexity, $args) {
return $childrenComplexity + GqlService::GRAPHQL_COMPLEXITY_NPLUS1 * (int)!empty($args);
},
],
'enabledForSite' => [
'name' => 'enabledForSite',
'type' => Type::boolean(),
'description' => 'Whether the element is enabled for the site.',
],
]), self::getName());
}
/**
* @inheritdoc
*/
protected static function getConditionalFields(): array
{
$fields = [];
if (Gql::canQueryUsers()) {
$fields = array_merge($fields, [
'authorId' => [
'name' => 'authorId',
'type' => Type::int(),
'description' => 'The ID of the author of this entry.',
],
'author' => [
'name' => 'author',
'type' => User::getType(),
'description' => 'The entry’s author.',
'complexity' => Gql::eagerLoadComplexity(),
],
]);
}
if (Gql::canQueryDrafts()) {
$fields = array_merge($fields, [
'draftCreator' => [
'name' => 'draftCreator',
'type' => User::getType(),
'description' => 'The creator of a given draft.',
'complexity' => Gql::relatedArgumentComplexity(GqlService::GRAPHQL_COMPLEXITY_EAGER_LOAD),
],
'drafts' => [
'name' => 'drafts',
'args' => EntryArguments::getArguments(),
'type' => Type::listOf(EntryInterface::getType()),
'description' => 'The drafts for the entry.',
'complexity' => Gql::relatedArgumentComplexity(GqlService::GRAPHQL_COMPLEXITY_EAGER_LOAD),
],
]);
}
if (Gql::canQueryRevisions()) {
$fields = array_merge($fields, [
'revisionCreator' => [
'name' => 'revisionCreator',
'type' => User::getType(),
'description' => 'The creator of a given revision.',
'complexity' => Gql::relatedArgumentComplexity(GqlService::GRAPHQL_COMPLEXITY_EAGER_LOAD),
],
'currentRevision' => [
'name' => 'currentRevision',
'type' => EntryInterface::getType(),
'description' => 'The current revision for the entry.',
'complexity' => Gql::relatedArgumentComplexity(GqlService::GRAPHQL_COMPLEXITY_EAGER_LOAD),
],
'revisions' => [
'name' => 'revisions',
'args' => EntryArguments::getArguments(),
'type' => Type::listOf(EntryInterface::getType()),
'description' => 'The revisions for the entry.',
'complexity' => Gql::relatedArgumentComplexity(GqlService::GRAPHQL_COMPLEXITY_EAGER_LOAD),
],
]);
}
return $fields;
}
}