Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #284 from digiaonline/improve-default-type-resolver
Browse files Browse the repository at this point in the history
Small improvement for defaultTypeResolver
  • Loading branch information
hungneox authored Sep 20, 2018
2 parents 97cd9ff + 3659e65 commit 1b1ce91
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Execution/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,10 @@ protected function defaultTypeResolver(
ResolveInfo $info,
AbstractTypeInterface $abstractType
) {
if (\is_array($value) && isset($value['__typename'])) {
return $value['__typename'];
}

/** @var ObjectType[] $possibleTypes */
$possibleTypes = $info->getSchema()->getPossibleTypes($abstractType);
$promisedIsTypeOfResults = [];
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Schema/BuildingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Digia\GraphQL\Test\Functional\Schema;

use Digia\GraphQL\Test\TestCase;
use Digia\GraphQL\Schema\Schema;
use Digia\GraphQL\Test\TestCase;
use function Digia\GraphQL\buildSchema;
use function Digia\GraphQL\Test\readFileContents;

Expand Down
158 changes: 158 additions & 0 deletions tests/Functional/Schema/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php
/**
* Created by PhpStorm.
* User: hunguyen
* Date: 20/09/2018
* Time: 10.55
*/

namespace Digia\GraphQL\Test\Functional\Schema;


use Digia\GraphQL\Test\TestCase;
use function Digia\GraphQL\buildSchema;
use function Digia\GraphQL\graphql;

class SchemaBuilderTest extends TestCase
{
/**
* @throws \Digia\GraphQL\Error\InvariantException
*/
public function testSpecifyingInterfaceTypeUsingTypeNameMetaFieldDefinition()
{
$source = '
type Query {
characters: [Character]
}
interface Character {
name: String!
}
type Human implements Character {
name: String!
totalCredits: Int
}
type Droid implements Character {
name: String!
primaryFunction: String
}
';

/** @noinspection PhpUnhandledExceptionInspection */
$schema = buildSchema($source);

$query = '
{
characters {
name
... on Human {
totalCredits
}
... on Droid {
primaryFunction
}
}
}
';

$rootValue = [
'characters' => [
[
'name' => 'Han Solo',
'totalCredits' => 10,
'__typename' => 'Human',
],
[
'name' => 'R2-D2',
'primaryFunction' => 'Astromech',
'__typename' => 'Droid',
],
],
];

$result = graphql($schema, $query, $rootValue);

$this->assertEquals([
'data' => [
'characters' => [
[
'name' => 'Han Solo',
'totalCredits' => 10,
],
[
'name' => 'R2-D2',
'primaryFunction' => 'Astromech',
],
],
],
], $result);
}

/**
* @throws \Digia\GraphQL\Error\InvariantException
*/
public function testSpecifyingUnionTypeUsingTypeNameMetaFieldDefinition()
{
$source = '
type Query {
fruits: [Fruit]
}
union Fruit = Apple | Banana
type Apple {
color: String
}
type Banana {
length: Int
}
';

/** @noinspection PhpUnhandledExceptionInspection */
$schema = buildSchema($source);

$query = '
{
fruits {
... on Apple {
color
}
... on Banana {
length
}
}
}
';

$rootValue = [
'fruits' => [
[
'color' => 'green',
'__typename' => 'Apple',
],
[
'length' => 5,
'__typename' => 'Banana',
],
],
];

$result = graphql($schema, $query, $rootValue);

$this->assertEquals([
'data' => [
'fruits' => [
[
'color' => 'green',
],
[
'length' => 5,
],
],
],
], $result);
}
}

0 comments on commit 1b1ce91

Please sign in to comment.