Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
viniychuk committed Nov 29, 2016
2 parents d3aaf03 + 9de7091 commit 75a8b3e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
71 changes: 71 additions & 0 deletions Tests/Schema/InputObjectDefaultValuesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Youshido\Tests\Schema;

use Youshido\GraphQL\Execution\Processor;
use Youshido\GraphQL\Schema\Schema;
use Youshido\GraphQL\Type\Enum\EnumType;
use Youshido\GraphQL\Type\InputObject\InputObjectType;
use Youshido\GraphQL\Type\NonNullType;
use Youshido\GraphQL\Type\Object\ObjectType;
use Youshido\GraphQL\Type\Scalar\DateTimeType;
use Youshido\GraphQL\Type\Scalar\DateTimeTzType;
use Youshido\GraphQL\Type\Scalar\IntType;
use Youshido\GraphQL\Type\Scalar\StringType;

class InputObjectDefaultValuesTest extends \PHPUnit_Framework_TestCase
{

public function testDefaultEnum()
{
$enumType = new EnumType([
'name' => 'InternalStatus',
'values' => [
[
'name' => 1,
'value' => 'ACTIVE'
],
[
'name' => 0,
'value' => 'DISABLED'
],
]
]);
$schema = new Schema([
'query' => new ObjectType([
'name' => 'RootQuery',
'fields' => [
'stringQuery' => [
'type' => new StringType(),
'args' => [
'statObject' => new InputObjectType([
'name' => 'StatObjectType',
'fields' => [
'status' => [
'type' => $enumType,
'default' => 1
],
'level' => new NonNullType(new IntType())
]
])
],
'resolve' => function ($source, $args) {
return sprintf('Result with level %s and status %s',
$args['statObject']['level'], $args['statObject']['status']
);
},
],
]
])
]);

$processor = new Processor($schema);
$processor->processPayload('{ stringQuery(statObject: { level: 1 }) }');
$result = $processor->getResponseData();

$this->assertEquals(['data' => [
'stringQuery' => 'Result with level 1 and status ACTIVE'
]], $result);
}

}
6 changes: 6 additions & 0 deletions src/Execution/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ private function prepareArgumentValue($argumentValue, AbstractType $argumentType
/** @var $argumentType AbstractInputObjectType */
$result = [];
if ($argumentValue instanceof AstInputObject) {
foreach($argumentType->getFields() as $field) {
/** @var $field Field */
if ($field->getConfig()->has('default')) {
$result[$field->getName()] = $field->getConfig()->get('default');
}
}
foreach ($argumentValue->getValue() as $key => $item) {
if ($argumentType->hasField($key)) {
$result[$key] = $this->prepareArgumentValue($item, $argumentType->getField($key)->getType()->getNullableType(), $request);
Expand Down

0 comments on commit 75a8b3e

Please sign in to comment.