Skip to content

Commit

Permalink
Merge pull request #13 from roippi/nested_variable_fix
Browse files Browse the repository at this point in the history
validate query arguments while processing AST
  • Loading branch information
viniychuk authored Jul 13, 2016
2 parents a948211 + 28bef5e commit c79eb70
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
31 changes: 30 additions & 1 deletion Tests/DataProvider/TestObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Youshido\GraphQL\Type\Object\ObjectType;
use Youshido\GraphQL\Type\Scalar\IntType;
use Youshido\GraphQL\Type\Scalar\StringType;
use Youshido\GraphQL\Type\NonNullType;

class TestObjectType extends AbstractObjectType
{
Expand All @@ -27,7 +28,35 @@ public function build($config)
'country' => new StringType(),
'city' => new StringType()
],
]));
]))
->addField('location', [
'type' => new ObjectType(
[
'name' => 'Location',
'fields' => [
'address' => new StringType()
]
]
),
'args' => [
'noop' => new IntType()
],
'resolve' => function ($value, $args, $info) {
return ['address' => '1234 Street'];
}
]
)
->addField(
'echo', [
'type' => new StringType(),
'args' => [
'value' => new NonNullType(new StringType())
],
'resolve' => function ($value, $args, $info) {
return $args['value'];
}
]
);
}

public function getInterfaces()
Expand Down
3 changes: 3 additions & 0 deletions Tests/DataProvider/TestSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Youshido\GraphQL\Config\Schema\SchemaConfig;
use Youshido\GraphQL\Execution\ResolveInfo;
use Youshido\GraphQL\Schema\AbstractSchema;
use Youshido\GraphQL\Type\NonNullType;
use Youshido\GraphQL\Type\Scalar\IntType;
use Youshido\GraphQL\Type\Scalar\StringType;

class TestSchema extends AbstractSchema
{
Expand Down
28 changes: 28 additions & 0 deletions Tests/Schema/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,34 @@ public function testEmptyQueries()

}

public function testNestedVariables() {
$processor = new Processor(new TestSchema());
$noArgsQuery = '{ me { echo(value:"foo") } }';
$expectedData = ['data' => ['me' => ['echo' => 'foo']]];
$processor->processPayload($noArgsQuery, ['value' => 'foo']);
$this->assertEquals($expectedData, $processor->getResponseData());

$parameterizedFieldQuery =
'query nestedFieldQuery($value:String!){
me {
echo(value:$value)
}
}';
$processor->processPayload($parameterizedFieldQuery, ['value' => 'foo']);
$this->assertEquals($expectedData, $processor->getResponseData());

$parameterizedQueryQuery =
'query nestedQueryQuery($value:Int){
me {
location(noop:$value) {
address
}
}
}';
$processor->processPayload($parameterizedQueryQuery, ['value' => 1]);
$this->assertArrayNotHasKey('errors', $processor->getResponseData());
}

public function testListNullResponse()
{
$processor = new Processor(new Schema([
Expand Down
4 changes: 4 additions & 0 deletions src/Execution/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ protected function executeOperation(Query $query, $currentLevelSchema)
*/
protected function processQueryAST(Query $query, AbstractField $field, $contextValue = null)
{
if (!$this->resolveValidator->validateArguments($field, $query, $this->executionContext->getRequest())) {
return null;
}

$resolvedValue = $this->resolveFieldValue($field, $contextValue, $query);

if (!$this->resolveValidator->isValidValueForField($field, $resolvedValue)) {
Expand Down

0 comments on commit c79eb70

Please sign in to comment.