Skip to content

Commit 7a33c3d

Browse files
authored
Move __typename type resolution tests into appropriate file (#1108)
1 parent e6fda8d commit 7a33c3d

File tree

2 files changed

+125
-146
lines changed

2 files changed

+125
-146
lines changed

tests/Executor/AbstractTest.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use GraphQL\Type\Definition\Type;
1818
use GraphQL\Type\Definition\UnionType;
1919
use GraphQL\Type\Schema;
20+
use GraphQL\Utils\BuildSchema;
2021
use PHPUnit\Framework\TestCase;
2122

2223
/**
@@ -373,6 +374,130 @@ public function testResolveTypeOnUnionYieldsUsefulError(): void
373374
self::assertArraySubset($expected, $result);
374375
}
375376

377+
/** @see it('resolve Union type using __typename on source object') */
378+
public function testResolveUnionTypeUsingTypenameOnSourceObject(): void
379+
{
380+
$schema = BuildSchema::build('
381+
type Query {
382+
pets: [Pet]
383+
}
384+
385+
union Pet = Cat | Dog
386+
387+
type Cat {
388+
name: String
389+
meows: Boolean
390+
}
391+
392+
type Dog {
393+
name: String
394+
woofs: Boolean
395+
}
396+
');
397+
398+
$query = '
399+
{
400+
pets {
401+
name
402+
... on Dog {
403+
woofs
404+
}
405+
... on Cat {
406+
meows
407+
}
408+
}
409+
}
410+
';
411+
412+
$rootValue = [
413+
'pets' => [
414+
[
415+
'__typename' => 'Dog',
416+
'name' => 'Odie',
417+
'woofs' => true,
418+
],
419+
[
420+
'__typename' => 'Cat',
421+
'name' => 'Garfield',
422+
'meows' => false,
423+
],
424+
],
425+
];
426+
427+
$expected = new ExecutionResult([
428+
'pets' => [
429+
['name' => 'Odie', 'woofs' => true],
430+
['name' => 'Garfield', 'meows' => false],
431+
],
432+
]);
433+
434+
$result = Executor::execute($schema, Parser::parse($query), $rootValue);
435+
self::assertEquals($expected, $result);
436+
}
437+
438+
/** @see it('resolve Interface type using __typename on source object') */
439+
public function testResolveInterfaceTypeUsingTypenameOnSourceObject(): void
440+
{
441+
$schema = BuildSchema::build('
442+
type Query {
443+
pets: [Pet]
444+
}
445+
446+
interface Pet {
447+
name: String
448+
}
449+
450+
type Cat implements Pet {
451+
name: String
452+
meows: Boolean
453+
}
454+
455+
type Dog implements Pet {
456+
name: String
457+
woofs: Boolean
458+
}
459+
');
460+
461+
$query = '
462+
{
463+
pets {
464+
name
465+
... on Dog {
466+
woofs
467+
}
468+
... on Cat {
469+
meows
470+
}
471+
}
472+
}
473+
';
474+
475+
$rootValue = [
476+
'pets' => [
477+
[
478+
'__typename' => 'Dog',
479+
'name' => 'Odie',
480+
'woofs' => true,
481+
],
482+
[
483+
'__typename' => 'Cat',
484+
'name' => 'Garfield',
485+
'meows' => false,
486+
],
487+
],
488+
];
489+
490+
$expected = new ExecutionResult([
491+
'pets' => [
492+
['name' => 'Odie', 'woofs' => true],
493+
['name' => 'Garfield', 'meows' => false],
494+
],
495+
]);
496+
497+
$result = Executor::execute($schema, Parser::parse($query), $rootValue);
498+
self::assertEquals($expected, $result);
499+
}
500+
376501
/**
377502
* @see it('returning invalid value from resolveType yields useful error')
378503
*/

tests/Utils/BuildSchemaLegacyTest.php

Lines changed: 0 additions & 146 deletions
This file was deleted.

0 commit comments

Comments
 (0)