-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16237 from craftcms/feature/link-gql
"Full data" GraphQL mode for Link fields
- Loading branch information
Showing
4 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\gql\types; | ||
|
||
use craft\fields\data\LinkData as FieldLinkData; | ||
use craft\gql\base\ObjectType; | ||
use GraphQL\Type\Definition\ResolveInfo; | ||
|
||
/** | ||
* Class LinkData | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 5.6.0 | ||
*/ | ||
class LinkData extends ObjectType | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function resolve(mixed $source, array $arguments, mixed $context, ResolveInfo $resolveInfo): mixed | ||
{ | ||
/** @var FieldLinkData $source */ | ||
$fieldName = $resolveInfo->fieldName; | ||
return match ($fieldName) { | ||
'type' => $source->getType(), | ||
'value' => $source->getValue(), | ||
'label' => $source->getLabel(true), | ||
'url' => $source->getUrl(), | ||
'elementType' => $source->getElement() ? $source->getElement()::class : null, | ||
'elementId' => $source->getElement()?->id, | ||
'elementSiteId' => $source->getElement()?->siteId, | ||
'elementTitle' => $source->getElement() ? (string)$source->getElement() : null, | ||
default => $source->$fieldName, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?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\gql\base\GeneratorInterface; | ||
use craft\gql\base\ObjectType; | ||
use craft\gql\base\SingleGeneratorInterface; | ||
use craft\gql\GqlEntityRegistry; | ||
use craft\gql\types\LinkData; | ||
use GraphQL\Type\Definition\Type; | ||
|
||
/** | ||
* Class LinkDataType | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 5.6.0 | ||
*/ | ||
class LinkDataType implements GeneratorInterface, SingleGeneratorInterface | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public static function generateTypes(mixed $context = null): array | ||
{ | ||
return [static::generateType($context)]; | ||
} | ||
|
||
/** | ||
* Returns the generator name. | ||
*/ | ||
public static function getName(): string | ||
{ | ||
return 'LinkData'; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public static function generateType(mixed $context): ObjectType | ||
{ | ||
$typeName = self::getName(); | ||
return GqlEntityRegistry::getOrCreate($typeName, fn() => new LinkData([ | ||
'name' => $typeName, | ||
'fields' => fn() => Craft::$app->getGql()->prepareFieldDefinitions([ | ||
'type' => Type::string(), | ||
'value' => Type::string(), | ||
'label' => Type::string(), | ||
'urlSuffix' => Type::string(), | ||
'url' => Type::string(), | ||
'elementType' => Type::string(), | ||
'elementId' => Type::int(), | ||
'elementSiteId' => Type::int(), | ||
'elementTitle' => Type::string(), | ||
], $typeName), | ||
])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters