-
Notifications
You must be signed in to change notification settings - Fork 611
/
GlobalSetType.php
77 lines (67 loc) · 2.22 KB
/
GlobalSetType.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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\gql\types\generators;
use Craft;
use craft\elements\GlobalSet as GlobalSetElement;
use craft\gql\base\Generator;
use craft\gql\base\GeneratorInterface;
use craft\gql\base\ObjectType;
use craft\gql\base\SingleGeneratorInterface;
use craft\gql\GqlEntityRegistry;
use craft\gql\interfaces\elements\GlobalSet as GlobalSetInterface;
use craft\gql\types\elements\GlobalSet;
use craft\helpers\Gql as GqlHelper;
/**
* Class GlobalSetType
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.3.0
*/
class GlobalSetType extends Generator implements GeneratorInterface, SingleGeneratorInterface
{
/**
* @inheritdoc
*/
public static function generateTypes(mixed $context = null): array
{
$globalSets = Craft::$app->getGlobals()->getAllSets();
$gqlTypes = [];
foreach ($globalSets as $globalSet) {
$requiredContexts = GlobalSetElement::gqlScopesByContext($globalSet);
if (!GqlHelper::isSchemaAwareOf($requiredContexts)) {
continue;
}
// Generate a type for each global set
$type = static::generateType($globalSet);
$gqlTypes[$type->name] = $type;
}
return $gqlTypes;
}
/**
* Returns the generator name.
*/
public static function getName($context = null): string
{
/** @var GlobalSetElement $context */
return $context->handle . '_GlobalSet';
}
/**
* @inheritdoc
*/
public static function generateType(mixed $context): ObjectType
{
$typeName = self::getName($context);
return GqlEntityRegistry::getOrCreate($typeName, fn() => new GlobalSet([
'name' => $typeName,
'fields' => function() use ($context, $typeName) {
$contentFieldGqlTypes = self::getContentFields($context);
$globalSetFields = array_merge(GlobalSetInterface::getFieldDefinitions(), $contentFieldGqlTypes);
return Craft::$app->getGql()->prepareFieldDefinitions($globalSetFields, $typeName);
},
]));
}
}