Skip to content
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
31 changes: 22 additions & 9 deletions Config/EnumTypeDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,31 @@ public function getDefinition()
->append($this->nameSection())
->arrayNode('values')
->useAttributeAsKey('name')
// if value not define we use name as value
->beforeNormalization()
->ifTrue(function ($v) {
return is_array($v);
})
->then(function ($v) {
foreach ($v as $name => &$options) {
// short syntax NAME: VALUE
if (!is_null($options) && !is_array($options)) {
$options = ['value' => $options];
}

// use name as value if no value given
if (!array_key_exists('value', $options)) {
$options['value'] = $name;
}
}

return $v;
})
->end()
->prototype('array')
->beforeNormalization()
->ifTrue(function ($v) {
return !is_null($v) && !is_array($v);
})
->then(function ($v) {
return ['value' => $v];
})
->end()
->isRequired()
->children()
->scalarNode('value')->end()
->scalarNode('value')->isRequired()->end()
->append($this->descriptionSection())
->append($this->deprecationReasonSelection())
->end()
Expand Down
5 changes: 5 additions & 0 deletions Resources/doc/definitions/system-types/enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ Episode:
NEWHOPE:
value: 4
description: "Released in 1977."
# to deprecate a value, only set the deprecation reason
#deprecationReason: "Just because"
EMPIRE:
value: 5
description: "Released in 1980."
JEDI: 6 # using the short syntax (JEDI value equal to 6)
# in this case FORCEAWAKENS value = FORCEAWAKENS
# FORCEAWAKENS:
# description: "Released in 2015."
```
2 changes: 2 additions & 0 deletions Resources/doc/definitions/system-types/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Human:
id:
type: "String!"
description: "The id of the character."
# to deprecate a field, only set the deprecation reason
#deprecationReason: "A terrible reason"
name:
type: "String"
description: "The name of the character."
Expand Down
11 changes: 8 additions & 3 deletions Tests/Functional/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ protected static function createAndBootKernel(array $options = [])
{
static::bootKernel($options);

static::$kernel->getContainer()->get('overblog_graphql.cache_compiler')->loadClasses();
static::getContainer()->get('overblog_graphql.cache_compiler')->loadClasses();
}

protected static function executeGraphQLRequest($query, $rootValue = [], $throwException = false)
{
$request = new Request();
$request->query->set('query', $query);

$req = static::$kernel->getContainer()->get('overblog_graphql.request_parser')->parse($request);
$executor = static::$kernel->getContainer()->get('overblog_graphql.request_executor');
$req = static::getContainer()->get('overblog_graphql.request_parser')->parse($request);
$executor = static::getContainer()->get('overblog_graphql.request_executor');
$executor->setThrowException($throwException);
$res = $executor->execute($req, $rootValue);

Expand All @@ -110,4 +110,9 @@ protected static function assertGraphQL($query, array $expectedData = null, arra

static::assertEquals($expected, $result, json_encode($result));
}

protected static function getContainer()
{
return static::$kernel->getContainer();
}
}
68 changes: 68 additions & 0 deletions Tests/Functional/Type/DefinitionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the OverblogGraphQLBundle package.
*
* (c) Overblog <http://github.com/overblog/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Overblog\GraphQLBundle\Tests\Functional\Security;

use GraphQL\Type\Definition\EnumType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use Overblog\GraphQLBundle\Tests\Functional\TestCase;

class DefinitionTest extends TestCase
{
protected function setUp()
{
parent::setUp();

static::createAndBootKernel(['test_case' => 'definition']);
}

public function testDefinesEnumTypeWithDeprecatedValue()
{
/** @var EnumType $enumTypeWithDeprecatedValue */
$enumTypeWithDeprecatedValue = $this->getType('EnumWithDeprecatedValue');
$value = $enumTypeWithDeprecatedValue->getValues()[0];
$this->assertEquals([
'name' => 'foo',
'description' => null,
'deprecationReason' => 'Just because',
'value' => 'foo',
], (array) $value);
$this->assertEquals(true, $this->isDeprecated($value));
}

public function testDefinesAnObjectTypeWithDeprecatedField()
{
/** @var ObjectType $TypeWithDeprecatedField */
$TypeWithDeprecatedField = $this->getType('ObjectWithDeprecatedField');
$field = $TypeWithDeprecatedField->getField('bar');
$this->assertEquals(Type::string(), $field->getType());
$this->assertEquals(true, $this->isDeprecated($field));
$this->assertEquals('A terrible reason', $field->deprecationReason);
$this->assertEquals('bar', $field->name);
$this->assertEquals([], $field->args);
}

private function isDeprecated($node)
{
// TODO(mcg-web) refactor after moving to latest lib version
if (is_callable([$node, 'isDeprecated'])) {
return $node->isDeprecated();
}

return (bool) $node->deprecationReason;
}

private function getType($type)
{
return $this->getContainer()->get('overblog_graphql.type_resolver')->resolve($type);
}
}
15 changes: 15 additions & 0 deletions Tests/Functional/app/config/definition/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
imports:
- { resource: ../config.yml }

parameters:
overblog_graphql.type_class_namespace: "Overblog\\GraphQLBundle\\Definition\\__DEFINITIONS__"

overblog_graphql:
definitions:
schema:
query: RootQuery
mappings:
types:
-
type: yml
dir: "%kernel.root_dir%/config/definition/mapping"
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
RootQuery:
type: object
config:
fields:
fake:
type: String

EnumWithDeprecatedValue:
type: enum
config:
values:
foo:
deprecationReason: "Just because"

ObjectWithDeprecatedField:
type: object
config:
fields:
bar:
type: String
deprecationReason: "A terrible reason"