Last active
December 5, 2023 05:19
-
-
Save a-am/a54e361118594bdbaae9c0795e127a20 to your computer and use it in GitHub Desktop.
Craft CMS Videos Plugin GQL Setup
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
<?php | |
namespace modules; | |
use Craft; | |
use yii\base\Event; | |
use craft\events\DefineGqlTypeFieldsEvent; | |
use craft\gql\TypeManager; | |
use GraphQL\Type\Definition\Type; | |
use modules\types\VideoGqlType; | |
/** | |
* Custom module class. | |
* | |
* This class will be available throughout the system via: | |
* `Craft::$app->getModule('module')`. | |
* | |
*/ | |
class Module extends \yii\base\Module | |
{ | |
/** | |
* Initializes the module. | |
*/ | |
public function init() | |
{ | |
// Set a @modules alias pointed to the modules/ directory | |
Craft::setAlias('@modules', __DIR__); | |
// Set the controllerNamespace based on whether this is a console or web request | |
if (Craft::$app->getRequest()->getIsConsoleRequest()) { | |
$this->controllerNamespace = 'modules\\console\\controllers'; | |
} else { | |
$this->controllerNamespace = 'modules\\controllers'; | |
} | |
parent::init(); | |
Event::on( | |
TypeManager::class, | |
TypeManager::EVENT_DEFINE_GQL_TYPE_FIELDS, | |
function(DefineGqlTypeFieldsEvent $event) { | |
if ($event->typeName == 'EntryInterface' || $event->typeName == 'NeoBlockInterface') { | |
$event->fields['dukt_video'] = [ | |
'name' => 'dukt_video', | |
'type' => VideoGqlType::getType(), | |
'resolve' => function ($source) { | |
return $source->video; | |
} | |
]; | |
} | |
}); | |
} | |
} |
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
<?php | |
namespace modules\types; | |
use craft\gql\GqlEntityRegistry; | |
use GraphQL\Type\Definition\ObjectType; | |
use GraphQL\Type\Definition\Type; | |
use modules\types\VideoRawGqlType; | |
class VideoGqlType extends ObjectType | |
{ | |
/** | |
* @return string | |
*/ | |
public static function getName(): string | |
{ | |
return 'videoField_Video'; | |
} | |
/** | |
* @return Type | |
*/ | |
public static function getType() | |
{ | |
if ($type = GqlEntityRegistry::getEntity(self::class)) { | |
return $type; | |
} | |
$type = GqlEntityRegistry::createEntity( | |
self::class, | |
new ObjectType( | |
[ | |
'name' => static::getName(), | |
'fields' => self::class . '::getFieldDefinitions', | |
] | |
) | |
); | |
return $type; | |
} | |
/** | |
* @return array | |
*/ | |
public static function getFieldDefinitions(): array | |
{ | |
return [ | |
'id' => [ | |
'name' => 'id', | |
'type' => Type::string() | |
], | |
'raw' => [ | |
'name' => 'raw', | |
'type' => VideoRawGqlType::getType() | |
], | |
'url' => [ | |
'name' => 'url', | |
'type' => Type::string() | |
], | |
'durationSeconds' => [ | |
'name' => 'durationSeconds', | |
'type' => Type::string() | |
], | |
'authorName' => [ | |
'name' => 'authorName', | |
'type' => Type::string() | |
], | |
'authorUrl' => [ | |
'name' => 'authorUrl', | |
'type' => Type::string() | |
], | |
'authorUsername' => [ | |
'name' => 'authorUsername', | |
'type' => Type::string() | |
], | |
'thumbnailSource' => [ | |
'name' => 'thumbnailSource', | |
'type' => Type::string() | |
], | |
'thumbnailLargeSource' => [ | |
'name' => 'thumbnailLargeSource', | |
'type' => Type::string() | |
], | |
'title' => [ | |
'name' => 'title', | |
'type' => Type::string() | |
], | |
'description' => [ | |
'name' => 'description', | |
'type' => Type::string() | |
], | |
'width' => [ | |
'name' => 'width', | |
'type' => Type::string() | |
], | |
'height' => [ | |
'name' => 'height', | |
'type' => Type::string() | |
], | |
]; | |
} | |
} |
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
<?php | |
namespace modules\types; | |
use craft\gql\GqlEntityRegistry; | |
use GraphQL\Type\Definition\ObjectType; | |
use GraphQL\Type\Definition\Type; | |
class VideoPictureGqlType extends ObjectType | |
{ | |
/** | |
* @return string | |
*/ | |
public static function getName(): string | |
{ | |
return 'videoPicture_Video'; | |
} | |
/** | |
* @return Type | |
*/ | |
public static function getType() | |
{ | |
if ($type = GqlEntityRegistry::getEntity(self::class)) { | |
return $type; | |
} | |
$type = GqlEntityRegistry::createEntity( | |
self::class, | |
new ObjectType( | |
[ | |
'name' => static::getName(), | |
'fields' => self::class . '::getFieldDefinitions', | |
] | |
) | |
); | |
return $type; | |
} | |
/** | |
* @return array | |
*/ | |
public static function getFieldDefinitions(): array | |
{ | |
return [ | |
'link' => [ | |
'name' => 'link', | |
'type' => Type::string() | |
], | |
'width' => [ | |
'name' => 'width', | |
'type' => Type::int() | |
], | |
'height' => [ | |
'name' => 'height', | |
'type' => Type::int() | |
], | |
]; | |
} | |
} |
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
<?php | |
namespace modules\types; | |
use craft\gql\GqlEntityRegistry; | |
use GraphQL\Type\Definition\ObjectType; | |
use GraphQL\Type\Definition\Type; | |
use modules\types\VideoPictureGqlType; | |
class VideoRawGqlType extends ObjectType | |
{ | |
/** | |
* @return string | |
*/ | |
public static function getName(): string | |
{ | |
return 'videoRaw_Video'; | |
} | |
/** | |
* @return Type | |
*/ | |
public static function getType() | |
{ | |
if ($type = GqlEntityRegistry::getEntity(self::class)) { | |
return $type; | |
} | |
$type = GqlEntityRegistry::createEntity( | |
self::class, | |
new ObjectType( | |
[ | |
'name' => static::getName(), | |
'fields' => self::class . '::getFieldDefinitions', | |
] | |
) | |
); | |
return $type; | |
} | |
/** | |
* @return array | |
*/ | |
public static function getFieldDefinitions(): array | |
{ | |
return [ | |
'pictures' => [ | |
'name' => 'pictures', | |
'type' => Type::listOf(VideoPictureGqlType::getType()) | |
], | |
'link' => [ | |
'name' => 'link', | |
'type' => Type::string() | |
], | |
'duration' => [ | |
'name' => 'duration', | |
'type' => Type::int() | |
], | |
'created_time' => [ | |
'name' => 'created_time', | |
'type' => Type::string() | |
] | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment