Skip to content

Commit

Permalink
updates to server factory configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
iampersistent committed Aug 15, 2018
1 parent a4ac09d commit 3b8d1cd
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 12 deletions.
25 changes: 22 additions & 3 deletions src/Factory/StandardServerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,35 @@
namespace Xaddax\GraphQL\Factory;

use GraphQL\Server\StandardServer;
use GraphQL\Type\Schema;
use Psr\Container\ContainerInterface;

final class StandardServerFactory
{
public function __invoke(ContainerInterface $container): StandardServer
{
$config = $container->get('config')['graphQL']['server'];
$containerConfig = $container->get('config');

$schemaClass = $config['schema'];
$config['schema'] = new $schemaClass();
$config = $containerConfig['graphQL']['server'] ?? [];

$schemaClass = $config['schema'] ?? Schema::class;
$config['schema'] = $container->get($schemaClass);

$callables = [
'errorFormatter',
'errorsHandler',
'fieldResolver',
'persistentQueryLoader',
];
foreach ($callables as $callableProperty) {
if (isset($config[$callableProperty])) {
$callable = $config[$callableProperty];
if (!is_callable($config[$callableProperty])) {
$callable = $container->get($callable);
}
$config[$callableProperty] = $callable;
}
}

return new StandardServer($config);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/_support/Fixture/TestContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

namespace Tests\Fixture;

use Psr\Container\ContainerInterface;

final class TestContainer implements ContainerInterface
{
private $values = [];

public function get($id)
{
return $this->values[$id];
}

public function has($id)
{
return isset($this->values[$id]);
}

public function set($id, $value): TestContainer
{
$this->values[$id] = $value;

return $this;
}
}
13 changes: 13 additions & 0 deletions tests/_support/TestResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);

namespace Tests\Fixture;

use GraphQL\Type\Definition\ResolveInfo;

final class TestResolver
{
public function __invoke($val, $args, $context, ResolveInfo $info)
{
}
}
73 changes: 64 additions & 9 deletions tests/unit/Factory/StandardServerFactoryCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

namespace Tests\Unit\Factory;

use Closure;
use GraphQL\Server\ServerConfig;
use GraphQL\Server\StandardServer;
use Prophecy\Prophet;
use Psr\Container\ContainerInterface;
use GraphQL\Type\Schema;
use Tests\Fixture\TestContainer;
use Tests\Fixture\TestResolver;
use Tests\Fixture\TestSchema;
use UnitTester;
use Xaddax\GraphQL\Factory\StandardServerFactory;
Expand All @@ -14,21 +17,73 @@ class StandardServerFactoryCest
{
public function testInvoke(UnitTester $I)
{
$prophet = new Prophet();
$prophecy = $prophet->prophesize();
$prophecy->willImplement(ContainerInterface::class);
$config = [
'graphQL' => [
'server' => [
'fieldResolver' => TestResolver::class,
'schema' => TestSchema::class,
],
],
];
$prophecy->get('config')->willReturn($config);
$container = $prophecy->reveal();
$container = (new TestContainer())
->set('config', $config)
->set(TestSchema::class, new TestSchema())
->set(TestResolver::class, new TestResolver());

$middleware = (new StandardServerFactory())->__invoke($container);
$server = (new StandardServerFactory())->__invoke($container);

$I->assertInstanceOf(StandardServer::class, $middleware);
$I->assertInstanceOf(StandardServer::class, $server);
$config = $this->getServerConfig($server);
$I->assertInstanceOf(TestSchema::class, $config->getSchema());
$I->assertInstanceOf(TestResolver::class, $config->getFieldResolver());
}

public function testInvokeNonConfiguredSchema(UnitTester $I)
{
$container = (new TestContainer())
->set('config', [])
->set(Schema::class, new TestSchema());

$server = (new StandardServerFactory())->__invoke($container);

$config = $this->getServerConfig($server);
$I->assertInstanceOf(TestSchema::class, $config->getSchema());
}

public function testInvokeCallable(UnitTester $I)
{
$errorFormatter = function() {
return;
};

$config = [
'graphQL' => [
'server' => [
'errorFormatter' => $errorFormatter,
'fieldResolver' => TestResolver::class,
'schema' => TestSchema::class,
],
],
];
$container = (new TestContainer())
->set('config', $config)
->set(TestSchema::class, new TestSchema())
->set(TestResolver::class, new TestResolver());

$server = (new StandardServerFactory())->__invoke($container);

$config = $this->getServerConfig($server);
$I->assertInstanceOf(TestSchema::class, $config->getSchema());
$I->assertInstanceOf(TestResolver::class, $config->getFieldResolver());
$I->assertSame($errorFormatter, $config->getErrorFormatter());
}

private function getServerConfig(StandardServer $server): ServerConfig
{
$getConfig = function(StandardServer $server) {
return $server->config;
};

return Closure::bind($getConfig, $server, $server)->__invoke($server);
}
}

0 comments on commit 3b8d1cd

Please sign in to comment.