Skip to content

Adding the ability to case SourceFields to ID type #39

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
Oct 18, 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: 17 additions & 0 deletions src/Annotations/SourceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @Attribute("logged", type = "bool"),
* @Attribute("right", type = "TheCodingMachine\GraphQL\Controllers\Annotations\Right"),
* @Attribute("returnType", type = "string"),
* @Attribute("isId", type = "bool"),
* })
*/
class SourceField implements SourceFieldInterface
Expand All @@ -37,6 +38,11 @@ class SourceField implements SourceFieldInterface
*/
private $returnType;

/**
* @var bool
*/
private $id;

/**
* @param mixed[] $attributes
*/
Expand All @@ -46,6 +52,7 @@ public function __construct(array $attributes = [])
$this->logged = $attributes['logged'] ?? false;
$this->right = $attributes['right'] ?? null;
$this->returnType = $attributes['returnType'] ?? null;
$this->id = $attributes['isId'] ?? false;
}

/**
Expand Down Expand Up @@ -87,4 +94,14 @@ public function getReturnType(): ?string
{
return $this->returnType;
}

/**
* If the GraphQL type is "ID", isID will return true.
*
* @return bool
*/
public function isId(): bool
{
return $this->id;
}
}
7 changes: 7 additions & 0 deletions src/Annotations/SourceFieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@ public function isLogged(): bool;
* @return string|null
*/
public function getReturnType(): ?string;

/**
* If the GraphQL type is "ID", isID will return true.
*
* @return bool
*/
public function isId(): bool;
}
7 changes: 6 additions & 1 deletion src/ControllerQueryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,12 @@ private function getSourceFields(): array

$phpdocType = $typeResolver->resolve((string) $refMethod->getReturnType());

if ($sourceField->getReturnType()) {
if ($sourceField->isId()) {
$type = GraphQLType::id();
if (!$refMethod->getReturnType()->allowsNull()) {
$type = GraphQLType::nonNull($type);
}
} elseif ($sourceField->getReturnType()) {
$type = $this->registry->get($sourceField->getReturnType());
} else {
$docBlockReturnType = $this->getDocBlocReturnType($docBlockObj, $refMethod);
Expand Down
6 changes: 0 additions & 6 deletions src/Registry/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,6 @@ public function get($id)
}

// The registry will try to instantiate the type if the class exists and has an annotation.


/*if (is_a($id, ObjectType::class, true)) {
$this->values[$id] = new $id($this);
return $this->values[$id];
}*/
if ($this->isGraphqlType($id)) {
$refTypeClass = new \ReflectionClass($id);
if ($refTypeClass->hasMethod('__construct') && $refTypeClass->getMethod('__construct')->getNumberOfRequiredParameters() > 0) {
Expand Down
13 changes: 13 additions & 0 deletions tests/ControllerQueryProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\Common\Annotations\AnnotationReader;
use GraphQL\Type\Definition\BooleanType;
use GraphQL\Type\Definition\FloatType;
use GraphQL\Type\Definition\IDType;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\IntType;
use GraphQL\Type\Definition\ListOfType;
Expand All @@ -14,6 +15,7 @@
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestController;
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject;
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestType;
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestTypeId;
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestTypeMissingAnnotation;
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestTypeMissingField;
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestTypeWithSourceFieldInterface;
Expand Down Expand Up @@ -220,6 +222,17 @@ public function testSourceFieldDoesNotExists()
$queryProvider->getFields();
}

public function testSourceFieldIsId()
{
$queryProvider = new ControllerQueryProvider(new TestTypeId(), $this->getRegistry());
$fields = $queryProvider->getFields();
$this->assertCount(1, $fields);

$this->assertSame('test', $fields[0]->name);
$this->assertInstanceOf(NonNull::class, $fields[0]->getType());
$this->assertInstanceOf(IDType::class, $fields[0]->getType()->getWrappedType());
}

public function testFromSourceFieldsInterface()
{
$registry = new Registry(new EmptyContainer(),
Expand Down
15 changes: 15 additions & 0 deletions tests/Fixtures/TestTypeId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php


namespace TheCodingMachine\GraphQL\Controllers\Fixtures;

use TheCodingMachine\GraphQL\Controllers\Annotations\SourceField;
use TheCodingMachine\GraphQL\Controllers\Annotations\Type;

/**
* @Type(class=TestObject::class)
* @SourceField(name="test", isId=true)
*/
class TestTypeId
{
}