Skip to content

Adding support for static factories #27

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 1 commit into from
Apr 2, 2019
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
13 changes: 10 additions & 3 deletions src/InputTypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use GraphQL\Type\Definition\ObjectType;
use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\Types\Object_;
use Psr\Container\ContainerInterface;
use ReflectionClass;
use ReflectionMethod;
use ReflectionType;
Expand Down Expand Up @@ -48,20 +49,26 @@ public function __construct(InputTypeUtils $inputTypeUtils,
}

/**
* @param object $factory
* @param string $factory
* @param string $methodName
* @param RecursiveTypeMapperInterface $recursiveTypeMapper
* @return InputObjectType
*/
public function mapFactoryMethod($factory, string $methodName, RecursiveTypeMapperInterface $recursiveTypeMapper): InputObjectType
public function mapFactoryMethod(string $factory, string $methodName, RecursiveTypeMapperInterface $recursiveTypeMapper, ContainerInterface $container): InputObjectType
{
$method = new ReflectionMethod($factory, $methodName);

if ($method->isStatic()) {
$object = $factory;
} else {
$object = $container->get($factory);
}

[$inputName, $className] = $this->inputTypeUtils->getInputTypeNameAndClassName($method);

if (!isset($this->cache[$inputName])) {
// TODO: add comment argument.
$this->cache[$inputName] = new ResolvableInputObjectType($inputName, $this->fieldsBuilderFactory, $recursiveTypeMapper, $factory, $methodName, $this->hydrator, null);
$this->cache[$inputName] = new ResolvableInputObjectType($inputName, $this->fieldsBuilderFactory, $recursiveTypeMapper, $object, $methodName, $this->hydrator, null);
}

return $this->cache[$inputName];
Expand Down
18 changes: 18 additions & 0 deletions src/InputTypeUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
namespace TheCodingMachine\GraphQLite;

use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\Types\Object_;
use phpDocumentor\Reflection\Types\Self_;
use ReflectionClass;
use ReflectionMethod;

class InputTypeUtils
Expand Down Expand Up @@ -57,10 +60,25 @@ private function validateReturnType(ReflectionMethod $refMethod): Fqsen
$typeResolver = new \phpDocumentor\Reflection\TypeResolver();

$phpdocType = $typeResolver->resolve($type);
$phpdocType = $this->resolveSelf($phpdocType, $refMethod->getDeclaringClass());
if (!$phpdocType instanceof Object_) {
throw MissingTypeHintException::invalidReturnType($refMethod);
}

return $phpdocType->getFqsen();
}

/**
* Resolves "self" types into the class type.
*
* @param Type $type
* @return Type
*/
private function resolveSelf(Type $type, ReflectionClass $reflectionClass): Type
{
if ($type instanceof Self_) {
return new Object_(new Fqsen('\\'.$reflectionClass->getName()));
}
return $type;
}
}
4 changes: 2 additions & 2 deletions src/Mappers/GlobTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ public function mapClassToInputType(string $className, RecursiveTypeMapperInterf
$factory = $map[$className];
}

return $this->inputTypeGenerator->mapFactoryMethod($this->container->get($factory[0]), $factory[1], $recursiveTypeMapper);
return $this->inputTypeGenerator->mapFactoryMethod($factory[0], $factory[1], $recursiveTypeMapper, $this->container);
}

/**
Expand Down Expand Up @@ -687,7 +687,7 @@ public function mapNameToType(string $typeName, RecursiveTypeMapperInterface $re
return $this->typeGenerator->mapAnnotatedObject($typeClassName, $recursiveTypeMapper);
}
if (isset($factory)) {
return $this->inputTypeGenerator->mapFactoryMethod($this->container->get($factory[0]), $factory[1], $recursiveTypeMapper);
return $this->inputTypeGenerator->mapFactoryMethod($factory[0], $factory[1], $recursiveTypeMapper, $this->container);
}

throw CannotMapTypeException::createForName($typeName);
Expand Down
2 changes: 1 addition & 1 deletion src/Types/ResolvableInputObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ResolvableInputObjectType extends InputObjectType implements ResolvableInp
* @param string $name
* @param FieldsBuilderFactory $controllerQueryProviderFactory
* @param RecursiveTypeMapperInterface $recursiveTypeMapper
* @param object $factory
* @param object|string $factory
* @param string $methodName
* @param HydratorInterface $hydrator
* @param null|string $comment
Expand Down
21 changes: 21 additions & 0 deletions tests/Fixtures/Integration/Controllers/FilterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php


namespace TheCodingMachine\GraphQLite\Fixtures\Integration\Controllers;


use TheCodingMachine\GraphQLite\Annotations\Query;
use TheCodingMachine\GraphQLite\Fixtures\Integration\Models\Filter;
use function var_export;

class FilterController
{
/**
* @Query()
* @return string[]
*/
public function echoFilters(Filter $filter): array
{
return $filter->getValues();
}
}
41 changes: 41 additions & 0 deletions tests/Fixtures/Integration/Models/Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php


namespace TheCodingMachine\GraphQLite\Fixtures\Integration\Models;


use TheCodingMachine\GraphQLite\Annotations\Factory;

class Filter
{
/**
* @var string[]|int[]
*/
private $values;

/**
* Filter constructor.
* @param string[]|int[] $values
*/
public function __construct(array $values)
{
$this->values = $values;
}

/**
* @return string[]|int[]
*/
public function getValues(): array
{
return $this->values;
}

/**
* @Factory()
* @param string[] $values
*/
public static function create(array $values): self
{
return new self($values);
}
}
24 changes: 24 additions & 0 deletions tests/Integration/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,28 @@ public function testEndToEnd2Iterators()
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);

}

public function testEndToEndStaticFactories()
{
/**
* @var Schema $schema
*/
$schema = $this->mainContainer->get(Schema::class);

$queryString = '
query {
echoFilters(filter: {values: ["foo", "bar"]})
}
';

$result = GraphQL::executeQuery(
$schema,
$queryString
);

$this->assertSame([
'echoFilters' => [ "foo", "bar" ]
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
}

}