public static interfacePossibleTypesMustImplementTheInterfaceRule ( ) |
public static function interfacePossibleTypesMustImplementTheInterfaceRule()
{
return function ($context) {
/** @var Schema $schema */
$schema = $context['schema'];
$typeMap = $schema->getTypeMap();
$errors = [];
foreach ($typeMap as $typeName => $type) {
if ($type instanceof InterfaceType) {
$possibleTypes = $type->getPossibleTypes();
foreach ($possibleTypes as $possibleType) {
if (!in_array($type, $possibleType->getInterfaces())) {
$errors[] = new Error("{$possibleType} is a possible type of interface {$type} but does " . "not implement it!");
}
}
}
}
return !empty($errors) ? $errors : null;
};
}
public function testRejectsWhenAPossibleTypeDoesNotImplementTheInterface() { // rejects when a possible type does not implement the interface $InterfaceType = new InterfaceType(['name' => 'InterfaceType', 'fields' => []]); $SubType = new ObjectType(['name' => 'SubType', 'fields' => [], 'interfaces' => []]); InterfaceType::addImplementationToInterfaces($SubType, [$InterfaceType]); // Sanity check. $this->assertEquals(1, count($InterfaceType->getPossibleTypes())); $this->assertEquals($SubType, $InterfaceType->getPossibleTypes()[0]); $schema = new Schema($InterfaceType); $validationResult = SchemaValidator::validate($schema, [SchemaValidator::interfacePossibleTypesMustImplementTheInterfaceRule()]); $this->assertSame(false, $validationResult->isValid); $this->assertSame(1, count($validationResult->errors)); $this->assertSame('SubType is a possible type of interface InterfaceType but does not ' . 'implement it!', $validationResult->errors[0]->message); }