View source
<?php
namespace Drupal\Tests\graphql\Kernel;
use Drupal\graphql\GraphQL\ResolverRegistry;
class ResolverRegistryTest extends GraphQLTestBase {
public function setUp() : void {
parent::setUp();
$schema = <<<GQL
type Query {
transportation: Vehicle
}
interface Vehicle {
model: String
}
interface Car implements Vehicle {
model: String
}
type Cabrio implements Car {
model: String
}
GQL;
$this
->setUpSchema($schema);
self::assertInstanceOf(ResolverRegistry::class, $this->registry);
}
public function testGetAllFieldResolvers() : void {
$transportation_resolver = $this->builder
->fromValue('Ford Model T');
$this
->mockResolver('Query', 'transportation', $transportation_resolver);
$car_resolver = $this->builder
->fromParent();
$this
->mockResolver('Car', 'model', $car_resolver);
$cabrio_resolver = $this->builder
->fromValue('Cabrio');
$this
->mockResolver('Cabrio', 'model', $cabrio_resolver);
self::assertEquals([
'Query' => [
'transportation' => $transportation_resolver,
],
'Car' => [
'model' => $car_resolver,
],
'Cabrio' => [
'model' => $cabrio_resolver,
],
], $this->registry
->getAllFieldResolvers());
}
public function testGetFieldResolverWithInheritanceTraversesSingleInheritance() : void {
$expected_resolver = $this->builder
->fromValue('Car');
$this
->mockResolver('Car', 'model', $expected_resolver);
$returned_resolver = $this->registry
->getFieldResolverWithInheritance($this->schema
->getSchema($this->registry)
->getType('Cabrio'), 'model');
self::assertEquals($expected_resolver, $returned_resolver);
}
public function testGetFieldResolverWithInheritanceTraversesMultipleInheritance() : void {
$expected_resolver = $this->builder
->fromValue('Vehicle');
$this
->mockResolver('Vehicle', 'model', $expected_resolver);
$returned_resolver = $this->registry
->getFieldResolverWithInheritance($this->schema
->getSchema($this->registry)
->getType('Cabrio'), 'model');
self::assertEquals($expected_resolver, $returned_resolver);
}
public function testGetFieldResolverWithInheritanceGivesPrecedenceToType() : void {
$this
->mockResolver('Vehicle', 'model', $this->builder
->fromValue('Vehicle'));
$expected_resolver = $this->builder
->fromValue('Cabrio');
$this
->mockResolver('Cabrio', 'model', $expected_resolver);
$returned_resolver = $this->registry
->getFieldResolverWithInheritance($this->schema
->getSchema($this->registry)
->getType('Cabrio'), 'model');
self::assertEquals($expected_resolver, $returned_resolver);
}
}