Skip to content

Commit adb18a5

Browse files
committed
* Optimized implementation to interfaces using lazy loader
* Union types now accepting callback
1 parent 36a8454 commit adb18a5

File tree

5 files changed

+25
-15
lines changed

5 files changed

+25
-15
lines changed

src/Schema.php

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function __construct(Type $querySchema = null, Type $mutationSchema = nul
3333
$this->mutationSchema = $mutationSchema;
3434
$this->subscriptionSchema = $subscriptionSchema;
3535

36+
InterfaceType::loadImplementationToInterfaces();
37+
3638
// Build type map now to detect any errors within this schema.
3739
$map = [];
3840
foreach ([$this->getQueryType(), $this->getMutationType(), Introspection::_schema()] as $type) {

src/Type/Definition/InterfaceType.php

+21-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class InterfaceType extends Type implements AbstractType, OutputType, CompositeT
1818
*/
1919
private $_implementations = [];
2020

21+
/**
22+
* @var \Closure[]
23+
*/
24+
private static $_lazyLoadImplementations = [];
25+
2126
/**
2227
* @var {[typeName: string]: boolean}
2328
*/
@@ -34,18 +39,30 @@ class InterfaceType extends Type implements AbstractType, OutputType, CompositeT
3439
public $config;
3540

3641
/**
37-
* Update the interfaces to know about this implementation.
42+
* Queue the update of the interfaces to know about this implementation.
3843
* This is an rare and unfortunate use of mutation in the type definition
3944
* implementations, but avoids an expensive "getPossibleTypes"
4045
* implementation for Interface types.
4146
*
4247
* @param ObjectType $impl
43-
* @param InterfaceType[] $interfaces
4448
*/
4549
public static function addImplementationToInterfaces(ObjectType $impl)
4650
{
47-
foreach ($impl->getInterfaces() as $interface) {
48-
$interface->_implementations[] = $impl;
51+
static::$_lazyLoadImplementations[] = function() use ($impl) {
52+
foreach ($impl->getInterfaces() as $interface) {
53+
$interface->_implementations[] = $impl;
54+
}
55+
};
56+
}
57+
58+
/**
59+
* Process ImplementationToInterfaces Queue
60+
*/
61+
public static function loadImplementationToInterfaces()
62+
{
63+
foreach (static::$_lazyLoadImplementations as $i => &$lazyLoadImplementation) {
64+
call_user_func($lazyLoadImplementation);
65+
unset(static::$_lazyLoadImplementations[$i]);
4966
}
5067
}
5168

src/Type/Definition/Type.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private static function getInternalType($name = null)
9898
}
9999

100100
/**
101-
* @return Type
101+
* @return Type[]
102102
*/
103103
public static function getInternalTypes()
104104
{

src/Type/Definition/UnionType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct($config)
2929
{
3030
Config::validate($config, [
3131
'name' => Config::STRING | Config::REQUIRED,
32-
'types' => Config::arrayOf(Config::OBJECT_TYPE, Config::REQUIRED),
32+
'types' => Config::arrayOf(Config::OBJECT_TYPE, Config::MAYBE_THUNK | Config::REQUIRED),
3333
'resolveType' => Config::CALLBACK, // function($value, ResolveInfo $info) => ObjectType
3434
'description' => Config::STRING
3535
]);

src/Types.php

-9
This file was deleted.

0 commit comments

Comments
 (0)