Skip to content

Reducing the number of calls to buildMap #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 12, 2018
Merged
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
17 changes: 10 additions & 7 deletions src/Mappers/GlobTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ private function getMap(): array
{
if ($this->fullMapComputed === false) {
$key = 'globTypeMapper_'.str_replace('\\', '_', $this->namespace);
$keyNameCache = 'globTypeMapper_names_'.str_replace('\\', '_', $this->namespace);
$this->mapClassToTypeArray = $this->cache->get($key);
if ($this->mapClassToTypeArray === null) {
$this->mapNameToType = $this->cache->get($keyNameCache);
if ($this->mapClassToTypeArray === null || $this->mapNameToType === null) {
$this->buildMap();
// This is a very short lived cache. Useful to avoid overloading a server in case of heavy load.
// Defaults to 2 seconds.
$this->cache->set($key, $this->mapClassToTypeArray, $this->globTtl);
$this->cache->set($keyNameCache, $this->mapNameToType, $this->globTtl);
}
}
return $this->mapClassToTypeArray;
Expand All @@ -118,10 +121,10 @@ private function buildMap(): void
continue;
}
if (isset($this->mapClassToTypeArray[$type->getClass()])) {
if ($this->mapClassToTypeArray[$type->getClass()] === $className) {
/*if ($this->mapClassToTypeArray[$type->getClass()] === $className) {
// Already mapped. Let's continue
continue;
}
}*/
throw DuplicateMappingException::create($type->getClass(), $this->mapClassToTypeArray[$type->getClass()], $className);
}
$this->storeTypeInCache($className, $type, $refClass->getFileName());
Expand Down Expand Up @@ -211,7 +214,7 @@ public function canMapClassToType(string $className): bool
$typeClassName = $this->getTypeFromCacheByObjectClass($className);

if ($typeClassName === null) {
$this->buildMap();
$this->getMap();
}

return isset($this->mapClassToTypeArray[$className]);
Expand All @@ -230,7 +233,7 @@ public function mapClassToType(string $className, RecursiveTypeMapperInterface $
$typeClassName = $this->getTypeFromCacheByObjectClass($className);

if ($typeClassName === null) {
$this->buildMap();
$this->getMap();
}

if (!isset($this->mapClassToTypeArray[$className])) {
Expand Down Expand Up @@ -286,7 +289,7 @@ public function mapNameToType(string $typeName, RecursiveTypeMapperInterface $re
$typeClassName = $this->getTypeFromCacheByGraphQLTypeName($typeName);

if ($typeClassName === null) {
$this->buildMap();
$this->getMap();
}

if (!isset($this->mapNameToType[$typeName])) {
Expand All @@ -309,7 +312,7 @@ public function canMapNameToType(string $typeName): bool
return true;
}

$this->buildMap();
$this->getMap();

return isset($this->mapNameToType[$typeName]);
}
Expand Down
25 changes: 21 additions & 4 deletions tests/Integration/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\Cache\Simple\ArrayCache;
use Symfony\Component\Cache\Simple\NullCache;
use TheCodingMachine\GraphQL\Controllers\AnnotationReader;
use TheCodingMachine\GraphQL\Controllers\ControllerQueryProviderFactory;
use TheCodingMachine\GraphQL\Controllers\Fixtures\Integration\Models\Contact;
Expand All @@ -37,7 +36,6 @@
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService;
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService;
use TheCodingMachine\GraphQL\Controllers\TypeGenerator;
use function var_dump;

class EndToEndTest extends TestCase
{
Expand All @@ -54,7 +52,7 @@ public function setUp()
},
QueryProviderInterface::class => function(ContainerInterface $container) {
return new GlobControllerQueryProvider('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration\\Controllers', $container->get(ControllerQueryProviderFactory::class),
$container->get(RecursiveTypeMapperInterface::class), $container->get(BasicAutoWiringContainer::class), new NullCache());
$container->get(RecursiveTypeMapperInterface::class), $container->get(BasicAutoWiringContainer::class), new ArrayCache());
},
ControllerQueryProviderFactory::class => function(ContainerInterface $container) {
return new ControllerQueryProviderFactory(
Expand Down Expand Up @@ -83,7 +81,7 @@ public function setUp()
$container->get(TypeGenerator::class),
$container->get(BasicAutoWiringContainer::class),
$container->get(AnnotationReader::class),
new NullCache()
new ArrayCache()
);
},
TypeGenerator::class => function(ContainerInterface $container) {
Expand Down Expand Up @@ -144,6 +142,25 @@ public function testEndToEnd()

]
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);

// Let's redo this to test cache.
$result = GraphQL::executeQuery(
$schema,
$queryString
);

$this->assertSame([
'getContacts' => [
[
'name' => 'Joe'
],
[
'name' => 'Bill',
'email' => 'bill@example.com'
]

]
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
}

}