Skip to content

Uses pipeline #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 3 additions & 25 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language : php
php : [5.6, 7.0, 7.1]
php : [7.1, 7.2]
cache : { directories : [$COMPOSER_CACHE_DIR, $HOME/.composer/cache, vendor] }
install : composer update --no-interaction --prefer-dist
notifications :
Expand All @@ -9,44 +9,22 @@ stages :
- test
- lint

env :
- TESTBENCH_VERSION=3.2.* LARAVEL_VERSION=5.2.*
- TESTBENCH_VERSION=3.3.* LARAVEL_VERSION=5.3.*
- TESTBENCH_VERSION=3.4.* LARAVEL_VERSION=5.4.*
- TESTBENCH_VERSION=3.5.* LARAVEL_VERSION=5.5.*
- TESTBENCH_VERSION=3.6.*@dev LARAVEL_VERSION=5.6.*

matrix :
exclude :
- php : 5.6
env : TESTBENCH_VERSION=3.5.* LARAVEL_VERSION=5.5.*
- php : 5.6
env : TESTBENCH_VERSION=3.6.*@dev LARAVEL_VERSION=5.6.*
- php : 7.0
env : TESTBENCH_VERSION=3.6.*@dev LARAVEL_VERSION=5.6.*

script :
- vendor/bin/phpunit

before_install :
- composer global require hirak/prestissimo --update-no-dev
- composer require "laravel/framework:${LARAVEL_VERSION}" --no-update --prefer-dist
- composer require "orchestra/testbench-browser-kit:${TESTBENCH_VERSION}" --no-update --prefer-dist

jobs :
include :
- stage : lint
php : 7.1
env : TESTBENCH_VERSION=3.6.*@dev LARAVEL_VERSION=5.6.*
php : 7.2
env : TESTBENCH_VERSION=3.6.* LARAVEL_VERSION=5.6.*
before_install :
- composer global require hirak/prestissimo --update-no-dev
- composer require "laravel/framework:${LARAVEL_VERSION}" --no-update --prefer-dist
- composer require "orchestra/testbench-browser-kit:${TESTBENCH_VERSION}" --no-update --prefer-dist
- composer require phpmd/phpmd --no-update --prefer-dist
- composer require phpstan/phpstan --no-update --prefer-dist
- composer require friendsofphp/php-cs-fixer --no-update --prefer-dist
script :
- vendor/bin/phpmd src text phpmd.xml
- vendor/bin/phpmd tests text phpmd.xml
- vendor/bin/phpstan analyse --autoload-file=_ide_helper.php --level 1 src
- vendor/bin/php-cs-fixer fix -v --dry-run --stop-on-violation --using-cache=no
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
}
],
"require": {
"laravel/framework": "5.5.*|5.6.*",
"laravel/framework": "5.6.*",
"webonyx/graphql-php": "^0.11.0",
"doctrine/dbal": "^2.5",
"cache/array-adapter": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "4.0|~5.0|~6.0",
"orchestra/testbench-browser-kit": "3.2.*|3.3.*|3.4.*|3.5.*",
"codeception/specify": "^0.4.6",
"phpunit/phpunit": "^7.0",
"orchestra/testbench-browser-kit": "3.6.*",
"codeception/specify": "^1.1",
"phpmd/phpmd" : "@stable",
"phpstan/phpstan": "^0.9.2",
"friendsofphp/php-cs-fixer": "^2.10"
Expand Down
6 changes: 6 additions & 0 deletions resources/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
]
],

// Define list of availabled transformers
'transformers' => [
'query' => ['list', 'view'],
'mutation' => ['store', 'drop', 'batch', 'restore']
],

// Type configuration. You can append any data : a transformer will handle
// them (if exists). Order matter
'definitions' => [
Expand Down
6 changes: 4 additions & 2 deletions src/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ protected function formatError(Error $e) {
*/
private function manageQuery(array $queries) {
$data = [];
$transformers = config('graphql.transformers.query', ['list', 'view']);

foreach ($queries as $query) {
$query = $this->make($query);
Expand All @@ -180,7 +181,7 @@ private function manageQuery(array $queries) {

return new ObjectType([
'name' => 'Query',
'fields' => $this->applyTransformers(['list', 'view'], $data)
'fields' => $this->applyTransformers($transformers, $data)
]);
}

Expand All @@ -192,6 +193,7 @@ private function manageQuery(array $queries) {
*/
private function manageMutation(array $mutations) {
$data = [];
$transformers = config('graphql.transformers.mutation', ['store', 'drop', 'batch', 'restore']);

foreach ($mutations as $mutation) {
$mutation = $this->make($mutation);
Expand All @@ -201,7 +203,7 @@ private function manageMutation(array $mutations) {

return new ObjectType([
'name' => 'Mutation',
'fields' => $this->applyTransformers(['store', 'drop', 'batch', 'restore'], $data)
'fields' => $this->applyTransformers($transformers, $data)
]);
}

Expand Down
9 changes: 9 additions & 0 deletions src/Support/Definition/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ public function getTransformers() {
];
}

/**
* Returns availabled pipes
*
* @return void
*/
public function getPipes(): array {
return [];
}

/**
* Resolve fetchable type
*
Expand Down
17 changes: 17 additions & 0 deletions src/Support/Definition/EloquentDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ public function getTransformers() {
];
}

/**
* @override
* @return array
*/
public function getPipes(): array {
return [
'list' => [
\StudioNet\GraphQL\Support\Pipe\Eloquent\AfterPipe::class,
\StudioNet\GraphQL\Support\Pipe\Eloquent\BeforePipe::class,
\StudioNet\GraphQL\Support\Pipe\Eloquent\SkipPipe::class,
\StudioNet\GraphQL\Support\Pipe\Eloquent\TakePipe::class,
\StudioNet\GraphQL\Support\Pipe\Eloquent\OrderByPipe::class,
\StudioNet\GraphQL\Support\Pipe\Eloquent\FilterPipe::class,
]
];
}

/**
* @override
*
Expand Down
17 changes: 17 additions & 0 deletions src/Support/Pipe/Argumentable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace StudioNet\GraphQL\Support\Pipe;

use StudioNet\GraphQL\Support\Definition\Definition;

/**
* Argumentable
*/
interface Argumentable {
/**
* Defines arguments list
*
* @param Definition $definition
* @return array
*/
public function getArguments(Definition $definition): array;
}
42 changes: 42 additions & 0 deletions src/Support/Pipe/Eloquent/AfterPipe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
namespace StudioNet\GraphQL\Support\Pipe\Eloquent;

use Closure;
use Illuminate\Database\Eloquent\Builder;
use StudioNet\GraphQL\Definition\Type;
use StudioNet\GraphQL\Support\Definition\Definition;
use StudioNet\GraphQL\Support\Pipe\Argumentable;

/**
* AfterPipe
*
* @see Argumentable
*/
class AfterPipe implements Argumentable {
/**
* handle
*
* @param Builder $builder
* @param Closure $next
* @param array $opts
* @return void
*/
public function handle(Builder $builder, Closure $next, array $opts) {
if (array_key_exists('after', $opts['args'])) {
$builder->where($opts['source']->getKeyName(), '>', $opts['args']['after']);
}

return $next($builder);
}

/**
* {@inheritDoc}
*
* @param Definition $definition
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function getArguments(Definition $definition): array {
return ['after' => ['type' => Type::id(), 'description' => 'Cursor-based navigation']];
}
}
42 changes: 42 additions & 0 deletions src/Support/Pipe/Eloquent/BeforePipe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
namespace StudioNet\GraphQL\Support\Pipe\Eloquent;

use Closure;
use Illuminate\Database\Eloquent\Builder;
use StudioNet\GraphQL\Definition\Type;
use StudioNet\GraphQL\Support\Definition\Definition;
use StudioNet\GraphQL\Support\Pipe\Argumentable;

/**
* BeforePipe
*
* @see Argumentable
*/
class BeforePipe implements Argumentable {
/**
* handle
*
* @param Builder $builder
* @param Closure $next
* @param array $opts
* @return void
*/
public function handle(Builder $builder, Closure $next, array $opts) {
if (array_key_exists('before', $opts['args'])) {
$builder->where($opts['source']->getKeyName(), '<', $opts['args']['before']);
}

return $next($builder);
}

/**
* {@inheritDoc}
*
* @param Definition $definition
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function getArguments(Definition $definition): array {
return ['before' => ['type' => Type::id(), 'description' => 'Cursor-based navigation']];
}
}
77 changes: 77 additions & 0 deletions src/Support/Pipe/Eloquent/FilterPipe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
namespace StudioNet\GraphQL\Support\Pipe\Eloquent;

use Closure;
use GraphQL\Type\Definition\InputObjectType;
use Illuminate\Database\Eloquent\Builder;
use StudioNet\GraphQL\Definition\Type;
use StudioNet\GraphQL\Grammar;
use StudioNet\GraphQL\Support\Definition\Definition;
use StudioNet\GraphQL\Support\Pipe\Argumentable;

/**
* FilterPipe
*
* @see Argumentable
*/
class FilterPipe implements Argumentable {
/**
* handle
*
* @param Builder $builder
* @param Closure $next
* @param array $opts
* @return void
*/
public function handle(Builder $builder, Closure $next, array $opts) {
if (array_key_exists('filter', $opts['args'])) {
$driver = \DB::connection()->getDriverName();
$grammar = null;

switch ($driver) {
case 'pgsql' : $grammar = new Grammar\PostgreSQLGrammar ; break;
case 'mysql' : $grammar = new Grammar\MySQLGrammar ; break;
case 'sqlite' : $grammar = new Grammar\SqliteGrammar ; break;
}

// Assert that grammar exists
if (is_null($grammar)) {
throw new \BadMethodCallException("{$driver} driver is not managed");
}

$grammar->getBuilderForFilter($builder, $opts['args']['filter'], $opts['filterables']);
}

return $next($builder);
}

/**
* {@inheritDoc}
*
* @param Definition $definition
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function getArguments(Definition $definition): array {
return ['filter' => ['type' => $this->getType($definition), 'description' => 'Performs filtering']];
}

/**
* Returns `filter` argument type object
*
* @param Definition $definition
* @return InputObjectType
*/
private function getType(Definition $definition) {
$queryable = [];

foreach ($definition->getFilterable() as $field => $filter) {
$queryable[$field] = ['type' => Type::json(), 'filter' => $filter];
}

return new InputObjectType([
'name' => ucfirst($definition->getName()) . 'Filter',
'fields' => $queryable
]);
}
}
52 changes: 52 additions & 0 deletions src/Support/Pipe/Eloquent/OrderByPipe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace StudioNet\GraphQL\Support\Pipe\Eloquent;

use Closure;
use Illuminate\Database\Eloquent\Builder;
use StudioNet\GraphQL\Definition\Type;
use StudioNet\GraphQL\Support\Definition\Definition;
use StudioNet\GraphQL\Support\Pipe\Argumentable;

/**
* OrderByPipe
*
* @see Argumentable
*/
class OrderByPipe implements Argumentable {
/**
* handle
*
* @param Builder $builder
* @param Closure $next
* @param array $opts
* @return void
*/
public function handle(Builder $builder, Closure $next, array $opts) {
if (array_key_exists('order_by', $opts['args'])) {
foreach ($opts['args']['order_by'] as $token) {
$order = $token;
$direction = 'ASC';

if (preg_match('/^(.*)_(asc|desc)$/i', $token, $matches)) {
$order = $matches[1];
$direction = strtoupper($matches[2]);
}

$builder->orderBy($order, $direction);
}
}

return $next($builder);
}

/**
* {@inheritDoc}
*
* @param Definition $definition
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function getArguments(Definition $definition): array {
return ['order_by' => ['type' => Type::listOf(Type::string()), 'description' => 'Ordering results']];
}
}
Loading