Skip to content

Commit

Permalink
Merge pull request #33 from mcg-web/lazy_implementation_to_interfaces…
Browse files Browse the repository at this point in the history
…_loading

Optimized implementation to interfaces loading
  • Loading branch information
vladar committed Apr 20, 2016
2 parents 36a8454 + 3c6305c commit 0a0c818
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public function __construct(Type $querySchema = null, Type $mutationSchema = nul
$this->mutationSchema = $mutationSchema;
$this->subscriptionSchema = $subscriptionSchema;

InterfaceType::loadImplementationToInterfaces();

// Build type map now to detect any errors within this schema.
$map = [];
foreach ([$this->getQueryType(), $this->getMutationType(), Introspection::_schema()] as $type) {
Expand Down
25 changes: 21 additions & 4 deletions src/Type/Definition/InterfaceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class InterfaceType extends Type implements AbstractType, OutputType, CompositeT
*/
private $_implementations = [];

/**
* @var \Closure[]
*/
private static $_lazyLoadImplementations = [];

/**
* @var {[typeName: string]: boolean}
*/
Expand All @@ -34,18 +39,30 @@ class InterfaceType extends Type implements AbstractType, OutputType, CompositeT
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
* implementations, but avoids an expensive "getPossibleTypes"
* implementation for Interface types.
*
* @param ObjectType $impl
* @param InterfaceType[] $interfaces
*/
public static function addImplementationToInterfaces(ObjectType $impl)
{
foreach ($impl->getInterfaces() as $interface) {
$interface->_implementations[] = $impl;
self::$_lazyLoadImplementations[] = function() use ($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]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private static function getInternalType($name = null)
}

/**
* @return Type
* @return Type[]
*/
public static function getInternalTypes()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Definition/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct($config)
{
Config::validate($config, [
'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
'description' => Config::STRING
]);
Expand Down
9 changes: 0 additions & 9 deletions src/Types.php

This file was deleted.

0 comments on commit 0a0c818

Please sign in to comment.