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

Commit 1b1ce91

Browse files
authored
Merge pull request #284 from digiaonline/improve-default-type-resolver
Small improvement for defaultTypeResolver
2 parents 97cd9ff + 3659e65 commit 1b1ce91

File tree

3 files changed

+163
-1
lines changed

3 files changed

+163
-1
lines changed

src/Execution/Executor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,10 @@ protected function defaultTypeResolver(
665665
ResolveInfo $info,
666666
AbstractTypeInterface $abstractType
667667
) {
668+
if (\is_array($value) && isset($value['__typename'])) {
669+
return $value['__typename'];
670+
}
671+
668672
/** @var ObjectType[] $possibleTypes */
669673
$possibleTypes = $info->getSchema()->getPossibleTypes($abstractType);
670674
$promisedIsTypeOfResults = [];

tests/Functional/Schema/BuildingTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Digia\GraphQL\Test\Functional\Schema;
44

5-
use Digia\GraphQL\Test\TestCase;
65
use Digia\GraphQL\Schema\Schema;
6+
use Digia\GraphQL\Test\TestCase;
77
use function Digia\GraphQL\buildSchema;
88
use function Digia\GraphQL\Test\readFileContents;
99

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: hunguyen
5+
* Date: 20/09/2018
6+
* Time: 10.55
7+
*/
8+
9+
namespace Digia\GraphQL\Test\Functional\Schema;
10+
11+
12+
use Digia\GraphQL\Test\TestCase;
13+
use function Digia\GraphQL\buildSchema;
14+
use function Digia\GraphQL\graphql;
15+
16+
class SchemaBuilderTest extends TestCase
17+
{
18+
/**
19+
* @throws \Digia\GraphQL\Error\InvariantException
20+
*/
21+
public function testSpecifyingInterfaceTypeUsingTypeNameMetaFieldDefinition()
22+
{
23+
$source = '
24+
type Query {
25+
characters: [Character]
26+
}
27+
28+
interface Character {
29+
name: String!
30+
}
31+
32+
type Human implements Character {
33+
name: String!
34+
totalCredits: Int
35+
}
36+
37+
type Droid implements Character {
38+
name: String!
39+
primaryFunction: String
40+
}
41+
';
42+
43+
/** @noinspection PhpUnhandledExceptionInspection */
44+
$schema = buildSchema($source);
45+
46+
$query = '
47+
{
48+
characters {
49+
name
50+
... on Human {
51+
totalCredits
52+
}
53+
... on Droid {
54+
primaryFunction
55+
}
56+
}
57+
}
58+
';
59+
60+
$rootValue = [
61+
'characters' => [
62+
[
63+
'name' => 'Han Solo',
64+
'totalCredits' => 10,
65+
'__typename' => 'Human',
66+
],
67+
[
68+
'name' => 'R2-D2',
69+
'primaryFunction' => 'Astromech',
70+
'__typename' => 'Droid',
71+
],
72+
],
73+
];
74+
75+
$result = graphql($schema, $query, $rootValue);
76+
77+
$this->assertEquals([
78+
'data' => [
79+
'characters' => [
80+
[
81+
'name' => 'Han Solo',
82+
'totalCredits' => 10,
83+
],
84+
[
85+
'name' => 'R2-D2',
86+
'primaryFunction' => 'Astromech',
87+
],
88+
],
89+
],
90+
], $result);
91+
}
92+
93+
/**
94+
* @throws \Digia\GraphQL\Error\InvariantException
95+
*/
96+
public function testSpecifyingUnionTypeUsingTypeNameMetaFieldDefinition()
97+
{
98+
$source = '
99+
type Query {
100+
fruits: [Fruit]
101+
}
102+
103+
union Fruit = Apple | Banana
104+
105+
type Apple {
106+
color: String
107+
}
108+
109+
type Banana {
110+
length: Int
111+
}
112+
';
113+
114+
/** @noinspection PhpUnhandledExceptionInspection */
115+
$schema = buildSchema($source);
116+
117+
$query = '
118+
{
119+
fruits {
120+
... on Apple {
121+
color
122+
}
123+
... on Banana {
124+
length
125+
}
126+
}
127+
}
128+
';
129+
130+
$rootValue = [
131+
'fruits' => [
132+
[
133+
'color' => 'green',
134+
'__typename' => 'Apple',
135+
],
136+
[
137+
'length' => 5,
138+
'__typename' => 'Banana',
139+
],
140+
],
141+
];
142+
143+
$result = graphql($schema, $query, $rootValue);
144+
145+
$this->assertEquals([
146+
'data' => [
147+
'fruits' => [
148+
[
149+
'color' => 'green',
150+
],
151+
[
152+
'length' => 5,
153+
],
154+
],
155+
],
156+
], $result);
157+
}
158+
}

0 commit comments

Comments
 (0)