-
Notifications
You must be signed in to change notification settings - Fork 1
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 #5 from dutchheight/feature-craftql
added caftql support
- Loading branch information
Showing
9 changed files
with
298 additions
and
19 deletions.
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,44 @@ | ||
<?php | ||
|
||
namespace dutchheight\navie\craftql\arguments; | ||
|
||
use dutchheight\navie\craftql\factories\ListFactory; | ||
use dutchheight\navie\craftql\repositories\ListRepository; | ||
use markhuot\CraftQL\Behaviors\FieldBehavior; | ||
|
||
class ListItemQueryArguments extends FieldBehavior | ||
{ | ||
public function initListItemQueryArguments() | ||
{ | ||
$repository = new ListRepository(); | ||
$repository->load(); | ||
|
||
$this->owner->addIntArgument('ancestorOf'); | ||
$this->owner->addIntArgument('ancestorDist'); | ||
$this->owner->addIntArgument('level'); | ||
$this->owner->addIntArgument('descendantOf'); | ||
$this->owner->addIntArgument('descendantDist'); | ||
$this->owner->addBooleanArgument('fixedOrder'); | ||
$this->owner->addArgument('list')->type((new ListFactory($repository, $this->owner->request))->enum())->lists(); | ||
$this->owner->addIntArgument('listId'); | ||
$this->owner->addIntArgument('id')->lists(); | ||
$this->owner->addStringArgument('indexBy'); | ||
$this->owner->addIntArgument('limit'); | ||
$this->owner->addStringArgument('site'); | ||
$this->owner->addIntArgument('siteId'); | ||
$this->owner->addIntArgument('nextSiblingOf'); | ||
$this->owner->addIntArgument('offset'); | ||
$this->owner->addStringArgument('order'); | ||
$this->owner->addStringArgument('orderBy'); | ||
$this->owner->addIntArgument('positionedAfter'); | ||
$this->owner->addIntArgument('positionedBefore'); | ||
$this->owner->addIntArgument('prevSiblingOf'); | ||
$this->owner->addIntArgument('siblingOf'); | ||
$this->owner->addStringArgument('title'); | ||
$this->owner->addStringArgument('url'); | ||
|
||
$fieldService = \Yii::$container->get('craftQLFieldService'); | ||
$arguments = $fieldService->getQueryArguments($this->owner->getRequest()); | ||
$this->owner->addArguments($arguments, false); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace dutchheight\navie\craftql\factories; | ||
|
||
use dutchheight\navie\craftql\types\ListItemType; | ||
use markhuot\CraftQL\Factories\BaseFactory; | ||
|
||
class ListFactory extends BaseFactory | ||
{ | ||
function make($raw, $request) | ||
{ | ||
return new ListItemType($request, $raw); | ||
} | ||
|
||
function can($id, $mode = 'query') | ||
{ | ||
return true; | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace dutchheight\navie\craftql\repositories; | ||
|
||
use dutchheight\navie\Navie; | ||
use yii\base\Component; | ||
|
||
class ListRepository extends Component | ||
{ | ||
private $lists = []; | ||
|
||
public function load() | ||
{ | ||
foreach (Navie::$plugin->getLists()->getAllLists() as $list) { | ||
if (!isset($this->lists[$list->id])) { | ||
$this->lists[$list->id] = $list; | ||
|
||
if (!empty($list->uid)) { | ||
$this->lists[$list->uid] = $list; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public function get($id) | ||
{ | ||
if (!isset($this->lists[$id])) { | ||
return false; | ||
} | ||
|
||
return $this->lists[$id]; | ||
} | ||
|
||
public function all() | ||
{ | ||
return $this->lists; | ||
} | ||
} |
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,59 @@ | ||
<?php | ||
|
||
namespace dutchheight\navie\craftql\types; | ||
|
||
use dutchheight\navie\craftql\arguments\ListItemQueryArguments; | ||
use dutchheight\navie\elements\ListItem; | ||
use markhuot\CraftQL\Builders\InterfaceBuilder; | ||
|
||
class ItemInterface extends InterfaceBuilder | ||
{ | ||
function boot() | ||
{ | ||
$this->addIntField('id')->nonNull(); | ||
$this->addIntField('listId'); | ||
$this->addIntField('elementId'); | ||
$this->addIntField('level'); | ||
|
||
$this->addStringField('title')->nonNull(); | ||
$this->addStringField('type'); | ||
$this->addStringField('url'); | ||
$this->addStringField('target'); | ||
|
||
$this->addField('children') | ||
->type(ItemInterface::class) | ||
->lists() | ||
->use(new ListItemQueryArguments) | ||
->resolve(function ($root, $args, $context, $info) { | ||
return ItemInterface::criteriaResolver($root, $args, $context, $info, $root->getChildren()); | ||
}); | ||
|
||
$this->addField('parent')->type(ItemInterface::class); | ||
$this->addField('next')->type(ItemInterface::class); | ||
$this->addField('nextSibling')->type(ItemInterface::class); | ||
$this->addField('prev')->type(ItemInterface::class); | ||
$this->addField('prevSibling')->type(ItemInterface::class); | ||
} | ||
|
||
function getResolveType() | ||
{ | ||
return function ($item) { | ||
return ucfirst($item->list->handle) . 'List'; | ||
}; | ||
} | ||
|
||
static function criteriaResolver($root, $args, $context, $info, $criteria = null, $asArray = true) { | ||
$criteria = $criteria ?: ListItem::find(); | ||
|
||
if (isset($args['list'])) { | ||
$args['listId'] = $args['list'][0]; | ||
unset($args['list']); | ||
} | ||
|
||
foreach ($args as $key => $value) { | ||
$criteria = $criteria->{$key}($value); | ||
} | ||
|
||
return $asArray ? $criteria->all() : $criteria; | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
namespace dutchheight\navie\craftql\types; | ||
|
||
use dutchheight\navie\craftql\types\ItemInterface; | ||
use markhuot\CraftQL\Builders\Schema; | ||
|
||
class ListItemType extends Schema { | ||
|
||
protected $interfaces = [ | ||
ItemInterface::class, | ||
]; | ||
|
||
function boot() | ||
{ | ||
$this->addFieldsByLayoutId($this->context->fieldLayoutId); | ||
} | ||
|
||
function getName(): string | ||
{ | ||
return ucfirst($this->context->handle) . 'List'; | ||
} | ||
|
||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace dutchheight\navie\craftql\types; | ||
|
||
use dutchheight\navie\records\ListRecord; | ||
use markhuot\CraftQL\Builders\Schema; | ||
|
||
class ListType extends Schema | ||
{ | ||
function boot() | ||
{ | ||
$this->addIntField('id'); | ||
$this->addIntField('structureId'); | ||
$this->addIntField('fieldLayoutId'); | ||
$this->addIntField('maxLevels'); | ||
|
||
$this->addStringField('name'); | ||
$this->addStringField('handle'); | ||
$this->addStringField('uid'); | ||
|
||
$this->addBooleanField('propagate'); | ||
|
||
$this->addDateField('dateCreated'); | ||
$this->addDateField('dateUpdated'); | ||
} | ||
|
||
static function criteriaResolver($root, $args, $context, $info, $criteria = null, $asArray = true) | ||
{ | ||
$criteria = $criteria ?: ListRecord::find(); | ||
|
||
foreach ($args as $key => $value) { | ||
$criteria = $criteria->where([$key => $value]); | ||
} | ||
|
||
return $asArray ? $criteria->all() : $criteria; | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
namespace dutchheight\navie\listeners; | ||
|
||
use dutchheight\navie\craftql\arguments\ListItemQueryArguments; | ||
use dutchheight\navie\craftql\factories\ListFactory; | ||
use dutchheight\navie\craftql\repositories\ListRepository; | ||
use dutchheight\navie\craftql\types\ItemInterface; | ||
use dutchheight\navie\craftql\types\ListType; | ||
|
||
use markhuot\CraftQL\Events\AlterQuerySchema; | ||
|
||
class GetNavieFieldSchema | ||
{ | ||
public static function handle(AlterQuerySchema $event) | ||
{ | ||
$repository = new ListRepository(); | ||
$repository->load(); | ||
|
||
$factory = new ListFactory($repository, $event->query->getRequest()); | ||
|
||
foreach ($factory->all() as $list) { | ||
$event->query->addConcreteType($list->getRawGraphQLObject()); | ||
} | ||
|
||
$navieType = $event->query->createObjectType('Navie'); | ||
|
||
$navieType | ||
->addField('items') | ||
->lists() | ||
->type(ItemInterface::class) | ||
->use(new ListItemQueryArguments) | ||
->resolve(function ($root, $args, $context, $info) { | ||
return ItemInterface::criteriaResolver($root, $args, $context, $info); | ||
}); | ||
|
||
$navieType | ||
->addField('lists') | ||
->lists() | ||
->type(ListType::class) | ||
->resolve(function ($root, $args, $context, $info) { | ||
return ListType::criteriaResolver($root, $args, $context, $info); | ||
}); | ||
|
||
$event->query | ||
->addField('navie') | ||
->type($navieType) | ||
->resolve(function ($root) { | ||
return []; | ||
}); | ||
} | ||
} |
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