-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide pagination in scaffolded queries
- Loading branch information
Uncle Cheese
committed
Jan 20, 2017
1 parent
f634fdf
commit d92d3ad
Showing
9 changed files
with
313 additions
and
132 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
61 changes: 61 additions & 0 deletions
61
src/Scaffolding/Creators/PaginatedQueryOperationCreator.php
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,61 @@ | ||
<?php | ||
|
||
namespace SilverStripe\GraphQL\Scaffolding\Creators; | ||
|
||
use SilverStripe\GraphQL\Pagination\PaginatedQueryCreator; | ||
use SilverStripe\GraphQL\Pagination\Connection; | ||
use SilverStripe\GraphQL\Manager; | ||
|
||
class PaginatedQueryOperationCreator extends PaginatedQueryCreator | ||
{ | ||
|
||
use PolymorphicResolverTrait; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $operationName; | ||
|
||
/** | ||
* @param Manager $manager | ||
* @param Connection $connection | ||
* @param string $operationName | ||
* @param string $typeName | ||
* @param Callable|ResolverInterface $resolver | ||
*/ | ||
public function __construct(Manager $manager, Connection $connection, $operationName, $typeName, $resolver) | ||
{ | ||
$this->connection = $connection; | ||
$this->resolver = $resolver; | ||
$this->operationName = $operationName; | ||
|
||
parent::__construct($manager); | ||
|
||
$this->connection | ||
->setConnectionType(function() use ($typeName) { | ||
return $this->manager->getType($typeName); | ||
}) | ||
->setConnectionResolver($this->createResolverFunction()); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function attributes() | ||
{ | ||
return [ | ||
'name' => $this->operationName | ||
]; | ||
} | ||
|
||
/** | ||
* This method usually builds a new Connection interface, but since it is | ||
* assigned in the constructor of this class, it's overloaded as a simple accessor | ||
* | ||
* @return SilverStripe\GraphQL\Pagination\Connection; | ||
*/ | ||
public function connection() | ||
{ | ||
return $this->connection; | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace SilverStripe\GraphQL\Scaffolding\Creators; | ||
|
||
use SilverStripe\GraphQL\Scaffolding\ResolverInterface; | ||
|
||
trait PolymorphicResolverTrait | ||
{ | ||
/** | ||
* @var \Closure|SilverStripe\GraphQL\ResolverInterface | ||
*/ | ||
protected $resolver; | ||
|
||
protected function createResolverFunction() | ||
{ | ||
$resolver = $this->resolver; | ||
|
||
return function () use ($resolver) { | ||
$args = func_get_args(); | ||
if (is_callable($resolver)) { | ||
return call_user_func_array($resolver, $args); | ||
} else { | ||
if ($resolver instanceof ResolverInterface) { | ||
return call_user_func_array([$resolver, 'resolve'], $args); | ||
} else { | ||
throw new \Exception(sprintf( | ||
'%s resolver must be a closure or implement %s', | ||
__CLASS__, | ||
ResolverInterface::class | ||
)); | ||
} | ||
} | ||
}; | ||
} | ||
} |
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
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
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
Oops, something went wrong.