Skip to content

Commit

Permalink
Revert "validate field arguments (rebing#608)"
Browse files Browse the repository at this point in the history
This reverts commit 81ed042
  • Loading branch information
mfn committed Apr 23, 2020
1 parent 7cb2b3e commit fb9dace
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 417 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ before_script:
- phpenv config-rm xdebug.ini || true

install:
- composer self-update --snapshot
- |
# Due to version incompatiblity with older Laravels we test, we remove it
composer remove --dev matt-allan/laravel-code-style --no-interaction --no-update
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CHANGELOG
=========

[Next release](https://github.com/rebing/graphql-laravel/compare/5.1.0...master)
[Next release](https://github.com/rebing/graphql-laravel/compare/5.0.0...master)
--------------

2019-04-22, 5.1.0
Expand Down
15 changes: 15 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ parameters:
count: 1
path: src/Support/Field.php

-
message: "#^Method Rebing\\\\GraphQL\\\\Support\\\\Field\\:\\:instanciateSelectFields\\(\\) has parameter \\$arguments with no value type specified in iterable type array\\.$#"
count: 1
path: src/Support/Field.php

-
message: "#^Method Rebing\\\\GraphQL\\\\Support\\\\Field\\:\\:aliasArgs\\(\\) has parameter \\$arguments with no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -301,6 +306,16 @@ parameters:
count: 1
path: src/Support/SelectFields.php

-
message: "#^Method Rebing\\\\GraphQL\\\\Support\\\\SelectFields\\:\\:getFieldSelection\\(\\) has parameter \\$args with no value type specified in iterable type array\\.$#"
count: 1
path: src/Support/SelectFields.php

-
message: "#^Method Rebing\\\\GraphQL\\\\Support\\\\SelectFields\\:\\:getFieldSelection\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Support/SelectFields.php

-
message: "#^Method Rebing\\\\GraphQL\\\\Support\\\\SelectFields\\:\\:getSelectableFieldsAndRelations\\(\\) has parameter \\$queryArgs with no value type specified in iterable type array\\.$#"
count: 1
Expand Down
50 changes: 6 additions & 44 deletions src/Support/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@
*/
abstract class Field
{
/**
* The depth the SelectField and ResolveInfoFieldsAndArguments classes traverse.
*
* @var int
*/
protected $depth = 5;

protected $attributes = [];

/**
Expand Down Expand Up @@ -93,21 +86,6 @@ public function getRules(array $arguments = []): array
return array_merge($argsRules, $rules);
}

/**
* @param array<string,mixed> $fieldsAndArgumentsSelection
* @return void
*/
public function validateFieldArguments(array $fieldsAndArgumentsSelection): void
{
$argsRules = (new RulesInFields($this->type(), $fieldsAndArgumentsSelection))->get();
if (count($argsRules)) {
$validator = $this->getValidator($fieldsAndArgumentsSelection, $argsRules);
if ($validator->fails()) {
throw new ValidationError('validation', $validator);
}
}
}

public function getValidator(array $args, array $rules): ValidatorContract
{
// allow our error messages to be customised
Expand Down Expand Up @@ -145,11 +123,6 @@ protected function getResolver(): ?Closure
}
}

$fieldsAndArguments = (new ResolveInfoFieldsAndArguments($arguments[3]))->getFieldsAndArgumentsSelection($this->depth);

// Validate arguments in fields
$this->validateFieldArguments($fieldsAndArguments);

$arguments[1] = $this->getArgs($arguments);

// Authorize
Expand All @@ -161,21 +134,21 @@ protected function getResolver(): ?Closure

$additionalParams = array_slice($method->getParameters(), 3);

$additionalArguments = array_map(function ($param) use ($arguments, $fieldsAndArguments) {
$additionalArguments = array_map(function ($param) use ($arguments) {
$className = null !== $param->getClass() ? $param->getClass()->getName() : null;

if (null === $className) {
throw new InvalidArgumentException("'{$param->name}' could not be injected");
}

if (Closure::class === $param->getType()->getName()) {
return function (int $depth = null) use ($arguments, $fieldsAndArguments): SelectFields {
return $this->instanciateSelectFields($arguments, $fieldsAndArguments, $depth);
return function (int $depth = null) use ($arguments): SelectFields {
return $this->instanciateSelectFields($arguments, $depth);
};
}

if (SelectFields::class === $className) {
return $this->instanciateSelectFields($arguments, $fieldsAndArguments, null);
return $this->instanciateSelectFields($arguments);
}

if (ResolveInfo::class === $className) {
Expand All @@ -192,22 +165,11 @@ protected function getResolver(): ?Closure
};
}

/**
* @param array<int,mixed> $arguments
* @param int $depth
* @param array<string,mixed> $fieldsAndArguments
* @return SelectFields
*/
private function instanciateSelectFields(array $arguments, array $fieldsAndArguments, int $depth = null): SelectFields
private function instanciateSelectFields(array $arguments, int $depth = null): SelectFields
{
$ctx = $arguments[2] ?? null;

if ($depth !== null && $depth !== $this->depth) {
$fieldsAndArguments = (new ResolveInfoFieldsAndArguments($arguments[3]))
->getFieldsAndArgumentsSelection($depth);
}

return new SelectFields($this->type(), $arguments[1], $ctx, $fieldsAndArguments);
return new SelectFields($arguments[3], $this->type(), $arguments[1], $depth ?? 5, $ctx);
}

protected function aliasArgs(array $arguments): array
Expand Down
93 changes: 0 additions & 93 deletions src/Support/RulesInFields.php

This file was deleted.

22 changes: 15 additions & 7 deletions src/Support/SelectFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use GraphQL\Error\InvariantViolation;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type as GraphqlType;
use GraphQL\Type\Definition\UnionType;
use GraphQL\Type\Definition\WrappingType;
Expand All @@ -31,28 +32,35 @@ class SelectFields
const ALWAYS_RELATION_KEY = 'ALWAYS_RELATION_KEY';

/**
* @param ResolveInfo $info
* @param GraphqlType $parentType
* @param array $queryArgs Arguments given with the query/mutation
* @param int $depth The depth to walk the AST and introspect for nested relations
* @param mixed $ctx The GraphQL context; can be anything and is only passed through
* Can be created/overridden by \Rebing\GraphQL\GraphQLController::queryContext
* @param array<string,mixed> $fieldsAndArguments Field and argument tree
*/
public function __construct(GraphqlType $parentType, array $queryArgs, $ctx, array $fieldsAndArguments)
public function __construct(ResolveInfo $info, GraphqlType $parentType, array $queryArgs, int $depth, $ctx)
{
if ($parentType instanceof WrappingType) {
$parentType = $parentType->getWrappedType(true);
}

$requestedFields = [
'args' => $queryArgs,
'fields' => $fieldsAndArguments,
];

$requestedFields = $this->getFieldSelection($info, $queryArgs, $depth);
$fields = self::getSelectableFieldsAndRelations($queryArgs, $requestedFields, $parentType, null, true, $ctx);
$this->select = $fields[0];
$this->relations = $fields[1];
}

private function getFieldSelection(ResolveInfo $resolveInfo, array $args, int $depth): array
{
$resolveInfoFieldsAndArguments = new ResolveInfoFieldsAndArguments($resolveInfo);

return [
'args' => $args,
'fields' => $resolveInfoFieldsAndArguments->getFieldsAndArgumentsSelection($depth),
];
}

/**
* Retrieve the fields (top level) and relations that
* will be selected with the query.
Expand Down
11 changes: 1 addition & 10 deletions tests/Unit/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace Rebing\GraphQL\Tests\Unit;

use Closure;
use GraphQL\Type\Definition\ResolveInfo;
use PHPUnit\Framework\MockObject\MockObject;
use Rebing\GraphQL\Tests\Support\Objects\ExampleField;
use Rebing\GraphQL\Tests\TestCase;

Expand All @@ -17,13 +15,6 @@ protected function getFieldClass()
return ExampleField::class;
}

protected function resolveInfoMock(): MockObject
{
return $this->getMockBuilder(ResolveInfo::class)
->disableOriginalConstructor()
->getMock();
}

/**
* Test get attributes.
*/
Expand Down Expand Up @@ -56,7 +47,7 @@ public function testResolve(): void
->method('resolve');

$attributes = $field->getAttributes();
$attributes['resolve'](null, [], [], $this->resolveInfoMock());
$attributes['resolve'](null, [], [], null);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/GraphQLQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function testQueryWithValidationError(): void
$this->assertArrayHasKey('errors', $result);
$this->assertArrayHasKey('extensions', $result['errors'][0]);
$this->assertArrayHasKey('validation', $result['errors'][0]['extensions']);
$this->assertTrue($result['errors'][0]['extensions']['validation']->has('test_validation.args.index'));
$this->assertTrue($result['errors'][0]['extensions']['validation']->has('index'));
}

/**
Expand Down
Loading

0 comments on commit fb9dace

Please sign in to comment.