-
-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This can be used to construct a schema without having to link every types together via a central registry or by using callbacks. It makes the types more independent and only on runtime they will have to all resolve to the correct type.
- Loading branch information
Showing
5 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GraphQL\Type\Definition; | ||
|
||
use GraphQL\Error\Error; | ||
|
||
class TypeReference extends Type implements NullableType, NamedType | ||
{ | ||
public string $name; | ||
|
||
public function __construct(string $name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function assertValid() : void | ||
{ | ||
|
||
} | ||
|
||
public function isBuiltInType() : bool | ||
{ | ||
|
||
} | ||
|
||
public function toString() : string | ||
{ | ||
return $this->name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace GraphQL\Tests\Type; | ||
|
||
use GraphQL\Error\InvariantViolation; | ||
use GraphQL\Type\Definition\InputObjectType; | ||
use GraphQL\Type\Definition\InterfaceType; | ||
use GraphQL\Type\Definition\ObjectType; | ||
use GraphQL\Type\Definition\Type; | ||
use GraphQL\Type\Definition\TypeReference; | ||
use GraphQL\Type\Schema; | ||
|
||
final class TypeReferenceTypeLoaderTest extends TypeLoaderTest | ||
{ | ||
private array $types; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->types = [ | ||
'Node' => new InterfaceType([ | ||
'name' => 'Node', | ||
'fields' => [ | ||
'id' => Type::string(), | ||
], | ||
'resolveType' => static fn() : ?ObjectType => null, | ||
]), | ||
'Content' => new InterfaceType([ | ||
'name' => 'Content', | ||
'fields' => [ | ||
'title' => Type::string(), | ||
'body' => Type::string(), | ||
], | ||
'resolveType' => static fn() : ?ObjectType => null, | ||
]), | ||
'BlogStory' => new ObjectType([ | ||
'name' => 'BlogStory', | ||
'interfaces' => [ | ||
new TypeReference('Node'), | ||
new TypeReference('Content'), | ||
], | ||
'fields' => [ | ||
'id' => Type::string(), | ||
'title' => Type::string(), | ||
'body' => Type::string(), | ||
], | ||
]), | ||
'Query' => new ObjectType([ | ||
'name' => 'Query', | ||
'fields' => [ | ||
'latestContent' => new TypeReference('Content'), | ||
'node' => new TypeReference('Node'), | ||
], | ||
]), | ||
'Mutation' => new ObjectType([ | ||
'name' => 'Mutation', | ||
'fields' => [ | ||
'postStory' => [ | ||
'type' => new TypeReference('PostStoryMutation'), | ||
'args' => [ | ||
'input' => Type::nonNull(new TypeReference('PostStoryMutationInput')), | ||
'clientRequestId' => Type::string(), | ||
], | ||
], | ||
], | ||
]), | ||
'PostStoryMutation' => new ObjectType([ | ||
'name' => 'PostStoryMutation', | ||
'fields' => [ | ||
'story' => new TypeReference('BlogStory'), | ||
], | ||
]), | ||
'PostStoryMutationInput' => new InputObjectType([ | ||
'name' => 'PostStoryMutationInput', | ||
'fields' => [ | ||
'title' => Type::string(), | ||
'body' => Type::string(), | ||
'author' => Type::id(), | ||
'category' => Type::id(), | ||
], | ||
]), | ||
]; | ||
} | ||
|
||
public function testWorksWithTypeLoader(): void | ||
{ | ||
$schema = new Schema([ | ||
'query' => $this->types['Query'], | ||
'mutation' => $this->types['Mutation'], | ||
'typeLoader' => fn (string $name) => $this->types[$name] ?? null, | ||
]); | ||
|
||
$node = $schema->getType('Node'); | ||
self::assertSame($this->types['Node'], $node); | ||
|
||
$content = $schema->getType('Content'); | ||
self::assertSame($this->types['Content'], $content); | ||
|
||
$blogStory = $schema->getType('BlogStory'); | ||
self::assertSame($this->types['BlogStory'], $blogStory); | ||
self::assertInstanceOf(ObjectType::class, $blogStory); | ||
self::assertCount(2, $blogStory->getInterfaces()); | ||
|
||
$postStoryMutation = $schema->getType('PostStoryMutation'); | ||
self::assertSame($this->types['PostStoryMutation'], $postStoryMutation); | ||
self::assertInstanceOf(ObjectType::class, $postStoryMutation); | ||
$field = $postStoryMutation->getField('story'); | ||
self::assertSame($blogStory, $field->getType()); | ||
|
||
$input = $schema->getType('PostStoryMutationInput'); | ||
self::assertSame($this->types['PostStoryMutationInput'], $input); | ||
|
||
$result = $schema->isSubType($node, $schema->getType('BlogStory')); | ||
self::assertTrue($result); | ||
} | ||
} |