Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL support #55

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes for Store Hours

## Unreleased

- Added GraphQL support. ([#55](https://github.com/craftcms/store-hours/pull/55))
- Added `craft\storehours\gql\types\Day`.
- Added `craft\storehours\gql\types\generators\DayType`.

## 3.0.0 - 2022-05-03

### Added
Expand Down
34 changes: 31 additions & 3 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
use Craft;
use craft\base\ElementInterface;
use craft\elements\User;
use craft\gql\GqlEntityRegistry;
use craft\helpers\Cp;
use craft\helpers\DateTimeHelper;
use craft\helpers\Json;
use craft\i18n\Locale;
use craft\storehours\data\DayData;
use craft\storehours\data\FieldData;
use craft\storehours\gql\types\Day;
use craft\storehours\gql\types\generators\DayType;
use craft\web\assets\timepicker\TimepickerAsset;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\Type;
use yii\db\Schema;

/**
Expand Down Expand Up @@ -124,7 +130,7 @@ public function getSettingsHtml(): ?string
/**
* @inheritdoc
*/
public function getInputHtml(mixed $value, ?\craft\base\ElementInterface $element = null): string
public function inputHtml(mixed $value, ?ElementInterface $element = null): string
{
Craft::$app->getView()->registerAssetBundle(TimepickerAsset::class);

Expand All @@ -135,7 +141,7 @@ public function getInputHtml(mixed $value, ?\craft\base\ElementInterface $elemen
/**
* @inheritdoc
*/
public function normalizeValue(mixed $value, ?\craft\base\ElementInterface $element = null): mixed
public function normalizeValue(mixed $value, ?ElementInterface $element = null): mixed
{
if (is_string($value) && !empty($value)) {
$value = Json::decodeIfJson($value);
Expand Down Expand Up @@ -164,7 +170,7 @@ public function normalizeValue(mixed $value, ?\craft\base\ElementInterface $elem
/**
* @inheritdoc
*/
public function serializeValue(mixed $value, ?\craft\base\ElementInterface $element = null): mixed
public function serializeValue(mixed $value, ?ElementInterface $element = null): mixed
{
/** @var FieldData $value */
$serialized = [];
Expand Down Expand Up @@ -261,4 +267,26 @@ private function _getInputHtml(FieldData $value, bool $static): string
'staticRows' => true,
]);
}

/**
* @inheritdoc
* @since 3.1.0
*/
public function getContentGqlType(): ListOfType
{
return Type::listOf(DayType::generateType($this));
}

/**
* @inheritdoc
* @since 3.1.0
*/
public function getContentGqlMutationArgumentType(): Type|array
{
$typeName = "{$this->handle}_DayInput";
return Type::listOf(GqlEntityRegistry::getOrCreate($typeName, fn() => new InputObjectType([
'name' => $typeName,
'fields' => fn() => Day::prepareFieldDefinition($this->slots),
])));
}
}
45 changes: 45 additions & 0 deletions src/gql/types/Day.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\storehours\gql\types;

use craft\gql\base\ObjectType;
use craft\gql\types\DateTime;
use GraphQL\Type\Definition\ResolveInfo;

/**
* Class Day
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.1.0
*/
class Day extends ObjectType
{
/**
* @inheritdoc
*/
protected function resolve(mixed $source, array $arguments, mixed $context, ResolveInfo $resolveInfo): mixed
{
return $source[$resolveInfo->fieldName];
}

/**
* Returns day fields prepared for GraphQL object definition.
*
* @param array $slots
* @return array
*/
public static function prepareFieldDefinition(array $slots): array
{
$contentFields = [];
foreach ($slots as $slotId => $slot) {
$handle = ($slot['handle'] ?? false) ?: $slotId;
$contentFields[$handle] = DateTime::getType();
}
return $contentFields;
}
}
61 changes: 61 additions & 0 deletions src/gql/types/generators/DayType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\storehours\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\storehours\Field;
use craft\storehours\gql\types\Day;

/**
* Class DayType
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.1.0
*/
class DayType implements GeneratorInterface, SingleGeneratorInterface
{
/**
* @inheritdoc
*/
public static function generateTypes(mixed $context = null): array
{
return [static::generateType($context)];
}

/**
* Returns the generator name.
*
* @return string
*/
public static function getName($context = null): string
{
/** @var Field $context */
return "{$context->handle}_Day";
}

/**
* @inheritdoc
*/
public static function generateType(mixed $context): ObjectType
{
$typeName = self::getName($context);

return GqlEntityRegistry::getOrCreate($typeName, fn() => new Day([
'name' => $typeName,
'fields' => function() use ($context, $typeName) {
/** @var Field $context */
$contentFields = Day::prepareFieldDefinition($context->slots);
return Craft::$app->getGql()->prepareFieldDefinitions($contentFields, $typeName);
},
]));
}
}