Skip to content

Commit

Permalink
sync entry query parameters and add site enum
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Feb 5, 2019
1 parent 12053b0 commit 4663af3
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 19 deletions.
18 changes: 18 additions & 0 deletions src/Factories/Site.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace markhuot\CraftQL\Factories;

use markhuot\CraftQL\Factories\BaseFactory;
use markhuot\CraftQL\Types\Site as SiteObjectType;

class Site extends BaseFactory {

function make($raw, $request) {
return new SiteObjectType($request, $raw);
}

function can($id, $mode='query') {
return true;
}

}
2 changes: 1 addition & 1 deletion src/FieldBehaviors/CategoryQueryArguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function initCategoryQueryArguments() {
$this->owner->addIntArgument('id')->lists();
$this->owner->addStringArgument('indexBy');
$this->owner->addIntArgument('limit');
$this->owner->addStringArgument('site');
$this->owner->addArgument('site')->type($this->owner->request->sites()->enum());
$this->owner->addIntArgument('siteId');
$this->owner->addIntArgument('nextSiblingOf');
$this->owner->addIntArgument('offset');
Expand Down
38 changes: 23 additions & 15 deletions src/FieldBehaviors/EntryQueryArguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,50 @@ function initEntryQueryArguments() {
$tmp = new Field($this->owner->request, 'TmpHoldingForAllQueryArgs');

$tmp->addStringArgument('after');
$tmp->addIntArgument('ancestorOf');
$tmp->addIntArgument('ancestorDist');
$tmp->addIntArgument('ancestorOf');
$tmp->addBooleanArgument('anyStatus');
$tmp->addBooleanArgument('archived');
$tmp->addStringArgument('authorGroup');
$tmp->addIntArgument('authorGroupId');
$tmp->addStringArgument('authorGroup')->lists();
$tmp->addIntArgument('authorGroupId')->lists();
$tmp->addIntArgument('authorId')->lists();
$tmp->addStringArgument('before');
$tmp->addIntArgument('level');
$tmp->addBooleanArgument('localeEnabled');
$tmp->addIntArgument('descendantOf');
$tmp->addStringArgument('dateCreated');
$tmp->addStringArgument('dateUpdated');
$tmp->addIntArgument('descendantDist');
$tmp->addIntArgument('descendantOf');
$tmp->addBooleanArgument('enabledForSite');
$tmp->addStringArgument('expiryDate');
$tmp->addBooleanArgument('fixedOrder');
$tmp->addBooleanArgument('hasDescendants');
// $tmp->addBooleanArgument('localeEnabled');
$tmp->addIntArgument('id')->lists();
$tmp->addIntArgument('idNot')->lists();
$tmp->addBooleanArgument('inReverse');
$tmp->addBooleanArgument('leaves');
$tmp->addIntArgument('level');
$tmp->addIntArgument('limit');
$tmp->addStringArgument('site');
$tmp->addIntArgument('siteId');
$tmp->addIntArgument('nextSiblingOf');
$tmp->addIntArgument('offset');
$tmp->addStringArgument('order');
$tmp->addStringArgument('orderBy');
$tmp->addIntArgument('positionedAfter');
$tmp->addIntArgument('positionedBefore');
$tmp->addStringArgument('postDate');
$tmp->addStringArgument('dateCreated');
$tmp->addStringArgument('dateUpdated');
$tmp->addIntArgument('prevSiblingOf');
$tmp->addStringArgument('relatedTo')->lists()->type($this->relatedToInputObject());
$tmp->addStringArgument('orRelatedTo')->lists()->type($this->relatedToInputObject());
$tmp->addStringArgument('search');
// $tmp->addStringArgument('section')->lists()->type($tmp->getRequest()->sections()->enum());
$tmp->addStringArgument('section')->lists()->type($this->owner->getRequest()->sections()->enum());
$tmp->addIntArgument('siblingOf');
$tmp->addStringArgument('slug');
$tmp->addStringArgument('status');
$tmp->addStringArgument('title');
// $tmp->addStringArgument('type')->lists()->type($tmp->getRequest()->entryTypes()->enum());
$tmp->addArgument('site')->type($this->owner->getRequest()->sites()->enum());
$tmp->addIntArgument('siteId');
$tmp->addStringArgument('slug')->lists();
$tmp->addStringArgument('status')->lists();
$tmp->addStringArgument('title')->lists();
$tmp->addBooleanArgument('trashed');
$tmp->addStringArgument('type')->lists()->type($this->owner->getRequest()->entryTypes()->enum());
$tmp->addStringArgument('uid');
$tmp->addStringArgument('uri');

$fieldService = \Yii::$container->get('craftQLFieldService');
Expand Down
2 changes: 1 addition & 1 deletion src/FieldBehaviors/TagQueryArguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function initTagQueryArguments() {
$this->owner->addIntArgument('id');
$this->owner->addStringArgument('indexBy');
$this->owner->addIntArgument('limit');
$this->owner->addStringArgument('site');
$this->owner->addArgument('site')->type($this->owner->request->sites()->enum());
$this->owner->addIntArgument('siteId');
$this->owner->addIntArgument('offset');
$this->owner->addStringArgument('order');
Expand Down
31 changes: 31 additions & 0 deletions src/Repositories/Site.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace markhuot\CraftQL\Repositories;

use Craft;
use yii\base\Component;

class Site extends Component {

private $sites = [];

function load() {
foreach (Craft::$app->sites->getAllSites() as $site) {
if (!isset($this->sites[$site->id])) {
$this->sites[$site->id] = $site;
if (!empty($site->uid)) {
$this->sites[$site->uid] = $site;
}
}
}
}

function get($id) {
return $this->sites[$id];
}

function all() {
return $this->sites;
}

}
9 changes: 9 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Request {
private $tagGroups;
private $sections;
private $globals;
private $sites;

function __construct($token) {
$this->token = $token;
Expand Down Expand Up @@ -44,6 +45,10 @@ function addGlobals(\markhuot\CraftQL\Factories\Globals $globals) {
$this->globals = $globals;
}

function addSites(\markhuot\CraftQL\Factories\Site $sites) {
$this->sites = $sites;
}

function token() {
return $this->token;
}
Expand Down Expand Up @@ -80,6 +85,10 @@ function globals(): \markhuot\CraftQL\Factories\Globals {
return $this->globals;
}

function sites(): \markhuot\CraftQL\Factories\Site {
return $this->sites;
}

private function parseRelatedTo($relations, $id) {
foreach ($relations as $index => &$relatedTo) {
foreach (['element', 'sourceElement', 'targetElement'] as $key) {
Expand Down
7 changes: 6 additions & 1 deletion src/Services/GraphQLService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,24 @@ class GraphQLService extends Component {
private $entryTypes;
private $sections;
private $globals;
private $sites;

function __construct(
\markhuot\CraftQL\Repositories\Volumes $volumes,
\markhuot\CraftQL\Repositories\CategoryGroup $categoryGroups,
\markhuot\CraftQL\Repositories\TagGroup $tagGroups,
\markhuot\CraftQL\Repositories\EntryType $entryTypes,
\markhuot\CraftQL\Repositories\Section $sections,
\markhuot\CraftQL\Repositories\Globals $globals
\markhuot\CraftQL\Repositories\Globals $globals,
\markhuot\CraftQL\Repositories\Site $sites
) {
$this->volumes = $volumes;
$this->categoryGroups = $categoryGroups;
$this->tagGroups = $tagGroups;
$this->entryTypes = $entryTypes;
$this->sections = $sections;
$this->globals = $globals;
$this->sites = $sites;
}

/**
Expand All @@ -68,6 +71,7 @@ function bootstrap() {
$this->entryTypes->load();
$this->sections->load();
$this->globals->load();
$this->sites->load();

// @TODO don't load _everything_. Instead only load what's needed on demand
\Yii::$container->get('craftQLFieldService')->load();
Expand All @@ -93,6 +97,7 @@ function getSchema($token) {
$request->addSections(new \markhuot\CraftQL\Factories\Section($this->sections, $request));
$request->addTagGroups(new \markhuot\CraftQL\Factories\TagGroup($this->tagGroups, $request));
$request->addGlobals(new \markhuot\CraftQL\Factories\Globals($this->globals, $request));
$request->addSites(new \markhuot\CraftQL\Factories\Site($this->sites, $request));

$schemaConfig = [];

Expand Down
2 changes: 1 addition & 1 deletion src/Types/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function addGlobalsSchema() {
$this->addField('globals')
->type(\markhuot\CraftQL\Types\GlobalsSet::class)
->arguments(function ($field) {
$field->addStringArgument('site');
$field->addArgument('site')->type($this->request->sites()->enum());
$field->addIntArgument('siteId');
})
->resolve(function ($root, $args) {
Expand Down

0 comments on commit 4663af3

Please sign in to comment.