Skip to content

Commit

Permalink
added caftql support
Browse files Browse the repository at this point in the history
  • Loading branch information
freimutt committed Nov 1, 2019
1 parent f8f1004 commit f1c1e12
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 19 deletions.
38 changes: 24 additions & 14 deletions src/Navie.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,33 @@

namespace dutchheight\navie;

use dutchheight\navie\base\PluginTrait;
use dutchheight\navie\variables\NavieVariable;
use dutchheight\navie\models\Settings;
use dutchheight\navie\elements\ListItem as ListItemElement;
use dutchheight\navie\graphql\queries\ListItem as ListItemQuery;
use dutchheight\navie\graphql\interfaces\ListItem as ListItemInterface;

use Craft;
use craft\base\Plugin;
use craft\helpers\UrlHelper;
use craft\services\UserPermissions;
use craft\services\Gql;
use craft\web\UrlManager;
use craft\web\twig\variables\CraftVariable;
use craft\events\RegisterUrlRulesEvent;
use craft\events\RegisterUserPermissionsEvent;
use craft\events\RegisterGqlQueriesEvent;
use craft\events\RegisterGqlTypesEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\events\RegisterUserPermissionsEvent;
use craft\events\SiteEvent;
use craft\helpers\UrlHelper;
use craft\queue\jobs\ResaveElements;
use craft\services\Gql;
use craft\services\Sites;
use craft\services\UserPermissions;
use craft\web\twig\variables\CraftVariable;
use craft\web\UrlManager;

use dutchheight\navie\base\PluginTrait;
use dutchheight\navie\elements\ListItem as ListItemElement;
use dutchheight\navie\graphql\interfaces\ListItem as ListItemInterface;
use dutchheight\navie\graphql\queries\ListItem as ListItemQuery;
use dutchheight\navie\listeners\GetNavieFieldSchema;
use dutchheight\navie\models\Settings;
use dutchheight\navie\variables\NavieVariable;

use markhuot\CraftQL\Builders\Schema;
use markhuot\CraftQL\CraftQL;
use markhuot\CraftQL\Events\AlterQuerySchema;

use yii\base\Event;

/**
Expand Down Expand Up @@ -142,6 +148,10 @@ private function _registerEventListeners()
$this->_registerGraphql();
}

if (class_exists(CraftQL::class)) {
Event::on(Schema::class, AlterQuerySchema::EVENT, [GetNavieFieldSchema::class, 'handle']);
}

// Install only for non-console Control Panel requests
if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) {
$this->_registerCpEventListeners();
Expand Down
44 changes: 44 additions & 0 deletions src/craftql/arguments/ListItemQueryArguments.php
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);
}
}
19 changes: 19 additions & 0 deletions src/craftql/factories/ListFactory.php
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;
}
}
38 changes: 38 additions & 0 deletions src/craftql/repositories/ListRepository.php
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;
}
}
59 changes: 59 additions & 0 deletions src/craftql/types/ItemInterface.php
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;
}
}
24 changes: 24 additions & 0 deletions src/craftql/types/ListItemType.php
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';
}

}
37 changes: 37 additions & 0 deletions src/craftql/types/ListType.php
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;
}
}
52 changes: 52 additions & 0 deletions src/listeners/GetNavieFieldSchema.php
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 [];
});
}
}
6 changes: 1 addition & 5 deletions src/records/ListRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@

namespace dutchheight\navie\records;

use dutchheight\navie\Navie;

use Craft;
use craft\db\ActiveRecord;
use craft\records\Structure;
use craft\records\FieldLayout;
use craft\records\Structure;

use yii\db\ActiveQueryInterface;

Expand All @@ -38,7 +35,6 @@ class ListRecord extends ActiveRecord

/**
* @inheritdoc
* @return string
*/
public static function tableName(): string
{
Expand Down

0 comments on commit f1c1e12

Please sign in to comment.