Merge pull request #33 from mcg-web/lazy_implementation_to_interfaces_loading

Optimized implementation to interfaces loading
This commit is contained in:
Vladimir Razuvaev 2016-04-20 17:58:09 +06:00
commit 0a0c8181ea
5 changed files with 25 additions and 15 deletions

View File

@ -33,6 +33,8 @@ class Schema
$this->mutationSchema = $mutationSchema; $this->mutationSchema = $mutationSchema;
$this->subscriptionSchema = $subscriptionSchema; $this->subscriptionSchema = $subscriptionSchema;
InterfaceType::loadImplementationToInterfaces();
// Build type map now to detect any errors within this schema. // Build type map now to detect any errors within this schema.
$map = []; $map = [];
foreach ([$this->getQueryType(), $this->getMutationType(), Introspection::_schema()] as $type) { foreach ([$this->getQueryType(), $this->getMutationType(), Introspection::_schema()] as $type) {

View File

@ -18,6 +18,11 @@ class InterfaceType extends Type implements AbstractType, OutputType, CompositeT
*/ */
private $_implementations = []; private $_implementations = [];
/**
* @var \Closure[]
*/
private static $_lazyLoadImplementations = [];
/** /**
* @var {[typeName: string]: boolean} * @var {[typeName: string]: boolean}
*/ */
@ -34,18 +39,30 @@ class InterfaceType extends Type implements AbstractType, OutputType, CompositeT
public $config; public $config;
/** /**
* Update the interfaces to know about this implementation. * Queue the update of the interfaces to know about this implementation.
* This is an rare and unfortunate use of mutation in the type definition * This is an rare and unfortunate use of mutation in the type definition
* implementations, but avoids an expensive "getPossibleTypes" * implementations, but avoids an expensive "getPossibleTypes"
* implementation for Interface types. * implementation for Interface types.
* *
* @param ObjectType $impl * @param ObjectType $impl
* @param InterfaceType[] $interfaces
*/ */
public static function addImplementationToInterfaces(ObjectType $impl) public static function addImplementationToInterfaces(ObjectType $impl)
{ {
foreach ($impl->getInterfaces() as $interface) { self::$_lazyLoadImplementations[] = function() use ($impl) {
$interface->_implementations[] = $impl; foreach ($impl->getInterfaces() as $interface) {
$interface->_implementations[] = $impl;
}
};
}
/**
* Process ImplementationToInterfaces Queue
*/
public static function loadImplementationToInterfaces()
{
foreach (self::$_lazyLoadImplementations as $i => &$lazyLoadImplementation) {
call_user_func($lazyLoadImplementation);
unset(self::$_lazyLoadImplementations[$i]);
} }
} }

View File

@ -98,7 +98,7 @@ GraphQLNonNull;
} }
/** /**
* @return Type * @return Type[]
*/ */
public static function getInternalTypes() public static function getInternalTypes()
{ {

View File

@ -29,7 +29,7 @@ class UnionType extends Type implements AbstractType, OutputType, CompositeType
{ {
Config::validate($config, [ Config::validate($config, [
'name' => Config::STRING | Config::REQUIRED, 'name' => Config::STRING | Config::REQUIRED,
'types' => Config::arrayOf(Config::OBJECT_TYPE, Config::REQUIRED), 'types' => Config::arrayOf(Config::OBJECT_TYPE, Config::MAYBE_THUNK | Config::REQUIRED),
'resolveType' => Config::CALLBACK, // function($value, ResolveInfo $info) => ObjectType 'resolveType' => Config::CALLBACK, // function($value, ResolveInfo $info) => ObjectType
'description' => Config::STRING 'description' => Config::STRING
]); ]);

View File

@ -1,9 +0,0 @@
<?php
namespace GraphQL;
class Types
{
public static function Int()
{
}
}