Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,8 @@ classBoundCache: $classBoundCache,

$aggregateQueryProvider = new AggregateQueryProvider($queryProviders);

$typeRegistry->finalizeTypes();

return new Schema($aggregateQueryProvider, $recursiveTypeMapper, $typeResolver, $topRootTypeMapper, $this->schemaConfig);
}

Expand Down
11 changes: 11 additions & 0 deletions src/TypeRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,15 @@ public function getMutableInterface(string $typeName): MutableInterface

return $type;
}

public function finalizeTypes(): void
{
foreach ($this->types as $type) {
if (! ($type instanceof MutableObjectType) && ! ($type instanceof MutableInterfaceType)) {
continue;
}

$type->freeze();
}
}
}
77 changes: 68 additions & 9 deletions tests/TypeRegistryTest.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
<?php

declare(strict_types=1);

namespace TheCodingMachine\GraphQLite;

use GraphQL\Type\Definition\ObjectType;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use TheCodingMachine\GraphQLite\Types\MutableInterfaceType;
use TheCodingMachine\GraphQLite\Types\MutableObjectType;

class TypeRegistryTest extends TestCase
{

public function testRegisterTypeException(): void
{
$type = new ObjectType([
'name' => 'Foo',
'fields' => function() {return [];}
'fields' => static function () {
return [];
},
]);

$registry = new TypeRegistry();
Expand All @@ -27,7 +32,9 @@ public function testGetType(): void
{
$type = new ObjectType([
'name' => 'Foo',
'fields' => function() {return [];}
'fields' => static function () {
return [];
},
]);

$registry = new TypeRegistry();
Expand All @@ -43,26 +50,31 @@ public function testHasType(): void
{
$type = new ObjectType([
'name' => 'Foo',
'fields' => function() {return [];}
'fields' => static function () {
return [];
},
]);

$registry = new TypeRegistry();
$registry->registerType($type);

$this->assertTrue($registry->hasType('Foo'));
$this->assertFalse($registry->hasType('Bar'));

}

public function testGetMutableObjectType(): void
{
$type = new MutableObjectType([
'name' => 'Foo',
'fields' => function() {return [];}
'fields' => static function () {
return [];
},
]);
$type2 = new ObjectType([
'name' => 'FooBar',
'fields' => function() {return [];}
'fields' => static function () {
return [];
},
]);

$registry = new TypeRegistry();
Expand All @@ -79,11 +91,15 @@ public function testGetMutableInterface(): void
{
$type = new MutableObjectType([
'name' => 'Foo',
'fields' => function() {return [];}
'fields' => static function () {
return [];
},
]);
$type2 = new ObjectType([
'name' => 'FooBar',
'fields' => function() {return [];}
'fields' => static function () {
return [];
},
]);

$registry = new TypeRegistry();
Expand All @@ -95,4 +111,47 @@ public function testGetMutableInterface(): void
$this->expectException(GraphQLRuntimeException::class);
$this->assertSame($type, $registry->getMutableInterface('FooBar'));
}

public function testFinalizeTypesFreezesMutableTypesOnly(): void
{
$mutableObjectType = $this->getMockBuilder(MutableObjectType::class)
->setConstructorArgs([
[
'name' => 'Foo',
'fields' => static function () {
return [];
},
],
])
->onlyMethods(['freeze'])
->getMock();

$mutableObjectType->expects($this->once())
->method('freeze');

$mutableInterfaceType = $this->getMockBuilder(MutableInterfaceType::class)
->setConstructorArgs([
[
'name' => 'Bar',
'fields' => static fn () => [],
],
])
->onlyMethods(['freeze'])
->getMock();

$mutableInterfaceType->expects($this->once())
->method('freeze');

$regularObjectType = new ObjectType([
'name' => 'Baz',
'fields' => static fn () => [],
]);

$registry = new TypeRegistry();
$registry->registerType($mutableObjectType);
$registry->registerType($mutableInterfaceType);
$registry->registerType($regularObjectType);

$registry->finalizeTypes();
}
}
Loading