Skip to content

Commit

Permalink
Reorganise GraphQL and Extensions classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Rainville committed Jun 19, 2020
1 parent f42485d commit 16f48ee
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 45 deletions.
8 changes: 4 additions & 4 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ Name: linkfield

SilverStripe\Admin\LeftAndMain:
extensions:
- SilverStripe\Link\LeftAndMainExtension
- SilverStripe\Link\Extensions\LeftAndMain

SilverStripe\Admin\ModalController:
extensions:
- SilverStripe\Link\ModalControllerExtension
- SilverStripe\Link\Extensions\ModalController

SilverStripe\Forms\TreeDropdownField:
extensions:
- SilverStripe\Link\TreeDropdownFieldExtension
- SilverStripe\Link\Extensions\AjaxField

SilverStripe\CMS\Forms\AnchorSelectorField:
extensions:
- SilverStripe\Link\TreeDropdownFieldExtension
- SilverStripe\Link\Extensions\AjaxField
11 changes: 4 additions & 7 deletions _config/graphql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ SilverStripe\GraphQL\Manager:
schemas:
admin:
types:
LinkType: 'SilverStripe\Link\Type\TypeCreator'
LinkDescription: 'SilverStripe\Link\Type\LinkDescriptionTypeCreator'
LinkType: SilverStripe\Link\GraphQL\LinkTypeType
LinkDescription: SilverStripe\Link\GraphQL\LinkDescriptionType

queries:
readLinkTypes: 'SilverStripe\Link\Type\QueryCreator'
readLinkDescription: 'SilverStripe\Link\Type\LinkDescriptionQueryCreator'

# mutations:
# createFile: 'SilverStripe\AssetAdmin\GraphQL\CreateFileMutationCreator'
readLinkTypes: SilverStripe\Link\GraphQL\LinkTypeQuery
readLinkDescription: SilverStripe\Link\GraphQL\LinkDescriptionQuery
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
<?php

namespace SilverStripe\Link;
namespace SilverStripe\Link\Extensions;

use SilverStripe\Core\Extension;
use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\Forms\FormField;
use SilverStripe\Link;

class TreeDropdownFieldExtension extends Extension
/**
* Tweak fields that need to be serve through the DynamicLink form schema and need to be able to receive AJAX calls.
*
* For example the TreeDropdownField need to be able to receive AJAX request to fetch the list of available SiteTrees.
*/
class AjaxField extends Extension
{

public function updateLink(&$link, $action)
{
/** @var TreeDropdownField $owner */
/** @var FormField $owner */
$owner = $this->getOwner();
$formName = $owner->getForm()->getName();

Expand Down
15 changes: 6 additions & 9 deletions src/LeftAndMainExtension.php → src/Extensions/LeftAndMain.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?php

namespace SilverStripe\Link;
namespace SilverStripe\Link\Extensions;

use SilverStripe\Core\Extension;
use SilverStripe\Link\Type\Registry;
use SilverStripe\View\Requirements;

class LeftAndMainExtension extends Extension
/**
* Register a new Form Schema in LeftAndMain.
*/
class LeftAndMain extends Extension
{
public function init()
{
// Get the Link Registry to load all the JS requirements for managing Links.
Registry::singleton()->init();
}

Expand All @@ -19,10 +22,4 @@ public function updateClientConfig(&$clientConfig)
'schemaUrl' => $this->getOwner()->Link('methodSchema/Modals/DynamicLink'),
];
}

public function DynamicLink()
{

}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
<?php

namespace SilverStripe\Link;
namespace SilverStripe\Link\Extensions;

use InvalidArgumentException;
use SilverStripe\Admin\ModalController;
use SilverStripe\Admin\ModalController as OwnerController;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\Form;
use SilverStripe\Link\FormFactory;
use SilverStripe\Link\Type\Registry;
use SilverStripe\View\Requirements;

class ModalControllerExtension extends Extension
/**
* Extensions to apply to ModalController so it knows how to handle the DynamicLink action.
*
* This action receive a link type key and some link data as a JSON string and retrieve a Form Schema for a
* specific Link Type.
*/
class ModalController extends Extension
{
private static $url_handlers = [
'editorAnchorLink/$ItemID' => 'editorAnchorLink', // Matches LeftAndMain::methodSchema args
Expand All @@ -30,7 +37,7 @@ public function DynamicLink()
// Show link text field if requested
$linkDataJsonStr = $this->getOwner()->controller->getRequest()->getVar('data');

/** @var ModalController $owner */
/** @var OwnerController $owner */
$owner = $this->getOwner();

$factory = FormFactory::singleton();
Expand All @@ -44,7 +51,12 @@ public function DynamicLink()
)->loadDataFrom($data);
}

private function getContext()
/**
* Build the context to pass to the Form Link Factory
* @return array
* @throws HTTPResponse_Exception
*/
private function getContext(): array
{
$linkTypeKey = $this->getOwner()->controller->getRequest()->getVar('key');
if (empty($linkTypeKey)) {
Expand All @@ -65,7 +77,11 @@ private function getContext()
];
}

private function getData()
/**
* Extract the Link Data out of the Request.
* @return array
*/
private function getData(): array
{
$data = [];
$dataString = $this->getOwner()->controller->getRequest()->getVar('data');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php
namespace SilverStripe\Link\Type;
namespace SilverStripe\Link\GraphQL;

use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type as GraphqlType;
use SilverStripe\GraphQL\Pagination\Connection;
use SilverStripe\GraphQL\QueryCreator as GraphqlQueryCreator;
use SilverStripe\GraphQL\QueryCreator;
use SilverStripe\Link\Type\Registry;

/**
* GraphQL Query to retrieve usage count for files and folders on GraphQL request.
* GraphQL Query to generate a Link Description for a provided JSON string representing the Link Data.
*/
class LinkDescriptionQueryCreator extends GraphqlQueryCreator
class LinkDescriptionQuery extends QueryCreator
{

public function attributes()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

namespace SilverStripe\Link\Type;
namespace SilverStripe\Link\GraphQL;

use GraphQL\Type\Definition\Type as GraphqlType;
use SilverStripe\GraphQL\TypeCreator as GraphqlTypeCreator;
use GraphQL\Type\Definition\ResolveInfo;

/**
*
* GraphQL type for serving a Link Description.
*/
class LinkDescriptionTypeCreator extends GraphqlTypeCreator
class LinkDescriptionType extends GraphqlTypeCreator
{
/**
* @return array
Expand Down
10 changes: 6 additions & 4 deletions src/Type/QueryCreator.php → src/GraphQL/LinkTypeQuery.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?php
namespace SilverStripe\Link\Type;
namespace SilverStripe\Link\GraphQL;

use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type as GraphqlType;
use SilverStripe\GraphQL\Pagination\Connection;
use SilverStripe\GraphQL\QueryCreator as GraphqlQueryCreator;
use SilverStripe\GraphQL\QueryCreator;
use SilverStripe\Link\Type\Registry;
use SilverStripe\Link\Type\Type;

/**
* GraphQL Query to retrieve usage count for files and folders on GraphQL request.
* GraphQL Query to retrieve the list of possible LinkTypes.
*/
class QueryCreator extends GraphqlQueryCreator
class LinkTypeQuery extends QueryCreator
{

public function attributes()
Expand Down
7 changes: 3 additions & 4 deletions src/Type/TypeCreator.php → src/GraphQL/LinkTypeType.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?php

namespace SilverStripe\Link\Type;
namespace SilverStripe\Link\GraphQL;

use GraphQL\Type\Definition\Type as GraphqlType;
use SilverStripe\GraphQL\TypeCreator as GraphqlTypeCreator;
use GraphQL\Type\Definition\ResolveInfo;

/**
*
* Provide a GraphQL Type for LinkTypes
*/
class TypeCreator extends GraphqlTypeCreator
class LinkTypeType extends GraphqlTypeCreator
{
/**
* @return array
Expand Down

0 comments on commit 16f48ee

Please sign in to comment.