Created
May 9, 2019 18:13
-
-
Save codyjames/898158e1d04da80a783708c2a2529368 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
// yourmodule/src/listeners/GetLinkitSchema.php | |
namespace modules\hshelpers\listeners; | |
use fruitstudios\linkit\base\Link; | |
use fruitstudios\linkit\models\Entry; | |
use markhuot\CraftQL\Types\EntryInterface; | |
use fruitstudios\linkit\fields\LinkitField; | |
use markhuot\CraftQL\Events\GetFieldSchema; | |
use modules\hshelpers\graphql\builders\Union; | |
use fruitstudios\linkit\models\Email; | |
use fruitstudios\linkit\models\Phone; | |
use markhuot\CraftQL\Types\CategoryInterface; | |
use fruitstudios\linkit\models\Category; | |
use fruitstudios\linkit\models\Asset; | |
use markhuot\CraftQL\Types\VolumeInterface; | |
use fruitstudios\linkit\models\User; | |
class GetLinkitSchema | |
{ | |
const TYPE_NAME = 'LinkitUrl'; | |
protected static $union = null; | |
/** | |
* Handle the request for the schema | |
* | |
* @param \markhuot\CraftQL\Events\GetFieldSchema $event | |
* @return void | |
*/ | |
public function handle($event) | |
{ | |
$event->handled = true; | |
/** @var LinkitField $field */ | |
$field = $event->sender; | |
$union = $this->_getUnion($event); | |
$urlField = $union->addType($this->_buildName($field, 'Url')); | |
$urlField->addStringField('url')->resolve($this->urlResolver()); | |
$urlField->addStringField('text')->resolve($this->textResolver()); | |
$emailField = $union->addType($this->_buildName($field, 'Email')); | |
$emailField->addStringField('url')->resolve($this->urlResolver()); | |
$emailField->addStringField('text')->resolve($this->textResolver()); | |
$emailField->addStringField('emailAddress')->resolve($this->valueResolver()); | |
$phoneField = $union->addType($this->_buildName($field, 'Phone')); | |
$phoneField->addStringField('url')->resolve($this->urlResolver()); | |
$phoneField->addStringField('text')->resolve($this->textResolver()); | |
$phoneField->addStringField('phoneNumber')->resolve($this->valueResolver()); | |
$entryField = $union->addType($this->_buildName($field, 'Entry')); | |
$entryField->addStringField('text')->resolve($this->textResolver()); | |
$entryField->addField('entry') | |
->type(EntryInterface::class) | |
->nonNull(false) | |
->resolve(function (Entry $root) { | |
return $root->getEntry(); | |
}); | |
$categoryField = $union->addType($this->_buildName($field, 'Category')); | |
$categoryField->addStringField('text')->resolve($this->textResolver()); | |
$categoryField->addField('category') | |
->type(CategoryInterface::class) | |
->resolve(function (Category $category) { | |
return $category->getCategory(); | |
}); | |
$assetField = $union->addType($this->_buildName($field, 'Asset')); | |
$assetField->addStringField('text')->resolve($this->textResolver()); | |
$assetField->addField('asset') | |
->type(VolumeInterface::class) | |
->resolve(function (Asset $root) { | |
return $root->getAsset(); | |
}); | |
} | |
/** | |
* Get union field. | |
* | |
* @param GetFieldSchema $event | |
* | |
* @return Union | |
*/ | |
private function _getUnion(GetFieldSchema $event) | |
{ | |
$field = $event->sender; | |
$schema = $event->schema; | |
static::$union = $schema->addUnionField($field) | |
->resolveType(function ($root) use ($field) { | |
$className = is_object($root) ? get_class($root) : 'Unknown'; | |
switch ($className) { | |
case Entry::class: | |
return $this->_buildName($field, 'Entry'); | |
case Email::class: | |
return $this->_buildName($field, 'Email'); | |
case Phone::class: | |
return $this->_buildName($field, 'Phone'); | |
case Category::class: | |
return $this->_buildName($field, 'Category'); | |
case Asset::class: | |
return $this->_buildName($field, 'Asset'); | |
default: | |
return $this->_buildName($field, 'Url'); | |
} | |
}) | |
->resolve(function ($root) use ($field) { | |
return $root->{$field->handle}; | |
}); | |
return static::$union; | |
} | |
/** | |
* Build type name for field. | |
* | |
* @param LinkitField $field | |
* @param string $name | |
* | |
* @return string | |
*/ | |
private function _buildName(LinkitField $field, string $name) | |
{ | |
return ucfirst($field->handle).$name; | |
} | |
/** | |
* @return \Closure | |
*/ | |
private function textResolver() | |
{ | |
return function (Link $root) { | |
return $root->getText(); | |
}; | |
} | |
/** | |
* @return \Closure | |
*/ | |
private function urlResolver() | |
{ | |
return function (Link $root) { | |
return $root->getUrl(); | |
}; | |
} | |
/** | |
* @return \Closure | |
*/ | |
private function valueResolver() | |
{ | |
return function (Link $root) { | |
return $root->value; | |
}; | |
} | |
} |
This file contains hidden or 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 | |
// yourmodule/src/GraphQLSchema.php | |
namespace modules\hshelpers; | |
use yii\base\Event; | |
class GraphQLSchema | |
{ | |
/** | |
* Initialize the GraphQL schema. | |
* | |
* @return void | |
*/ | |
public static function init() | |
{ | |
Event::on( | |
\fruitstudios\linkit\fields\LinkitField::class, | |
'craftQlGetFieldSchema', | |
[new \modules\hshelpers\listeners\GetLinkitSchema, 'handle'] | |
); | |
} | |
} |
This file contains hidden or 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 | |
... | |
GraphQLSchema::init(); | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment