Skip to content

Adding support for conditional source fields #35

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 3 commits into from
Oct 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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: php
php:
- 7.1
- 7.2
cache:
directories:
- $HOME/.composer/cache
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,34 @@ class PostType extends AbstractAnnotatedObjectType
}
```

In some very particular cases, you might not know exactly the list of @SourceField annotations at development time.
If you need to decide the list of @SourceField at runtime, you can implement the `FromSourceFieldsInterface`:

```php
/**
* @Type(class=Post::class)
*/
class PostType implements FromSourceFieldsInterface
{
/**
* Dynamically returns the array of source fields to be fetched from the original object.
*
* @return SourceFieldInterface[]
*/
public function getSourceFields(): array
{
// You can want to enable fields conditionnaly based on feature flags...
if (ENABLE_STATUS_GLOBALLY) {
return [
new SourceField(['name'=>'status', 'logged'=>true]),
];
} else {
return [];
}
}
}
```



Troubleshooting
Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/SourceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @Attribute("returnType", type = "string"),
* })
*/
class SourceField
class SourceField implements SourceFieldInterface
{
/**
* @var Right|null
Expand Down
38 changes: 38 additions & 0 deletions src/Annotations/SourceFieldInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace TheCodingMachine\GraphQL\Controllers\Annotations;


/**
* SourceFields are fields that are directly source from the base object into GraphQL.
*/
interface SourceFieldInterface
{
/**
* Returns the GraphQL right to be applied to this source field.
*
* @return Right|null
*/
public function getRight(): ?Right;

/**
* Returns the name of the GraphQL query/mutation/field.
* If not specified, the name of the method should be used instead.
*
* @return null|string
*/
public function getName(): ?string;

/**
* @return bool
*/
public function isLogged(): bool;

/**
* Returns the GraphQL return type of the request (as a string).
* The string can represent the FQCN of the type or an entry in the container resolving to the GraphQL type.
*
* @return string|null
*/
public function getReturnType(): ?string;
}
5 changes: 5 additions & 0 deletions src/ControllerQueryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace TheCodingMachine\GraphQL\Controllers;

use function array_merge;
use GraphQL\Type\Definition\InputType;
use GraphQL\Type\Definition\OutputType;
use phpDocumentor\Reflection\DocBlock;
Expand Down Expand Up @@ -216,6 +217,10 @@ private function getSourceFields(): array
return $annotation instanceof SourceField;
});

if ($this->controller instanceof FromSourceFieldsInterface) {
$sourceFields = array_merge($sourceFields, $this->controller->getSourceFields());
}

if (empty($sourceFields)) {
return [];
}
Expand Down
22 changes: 22 additions & 0 deletions src/FromSourceFieldsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php


namespace TheCodingMachine\GraphQL\Controllers;

use TheCodingMachine\GraphQL\Controllers\Annotations\SourceFieldInterface;

/**
* This interface exposes dynamically a list of SourceFields.
* It can be used as an alternative to the SourceField annotation when the list of source fields is dynamic.
*
* Note: whenever possible, it is advised to NOT use this interface and instead rely on the SourceField annotation.
*/
interface FromSourceFieldsInterface
{
/**
* Dynamically returns the array of source fields to be fetched from the original object.
*
* @return SourceFieldInterface[]
*/
public function getSourceFields(): array;
}
26 changes: 25 additions & 1 deletion tests/ControllerQueryProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestType;
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestTypeMissingAnnotation;
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestTypeMissingField;
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestTypeWithSourceFieldInterface;
use TheCodingMachine\GraphQL\Controllers\Registry\EmptyContainer;
use TheCodingMachine\GraphQL\Controllers\Registry\Registry;
use TheCodingMachine\GraphQL\Controllers\Security\AuthenticationServiceInterface;
Expand Down Expand Up @@ -194,7 +195,7 @@ public function isAllowed(string $right): bool
$this->getTypeMapper(),
$this->getHydrator());

$queryProvider = new ControllerQueryProvider(new TestType($this->getRegistry()), $registry);
$queryProvider = new ControllerQueryProvider(new TestType(), $registry);
$fields = $queryProvider->getFields();
$this->assertCount(3, $fields);

Expand All @@ -218,4 +219,27 @@ public function testSourceFieldDoesNotExists()
$this->expectExceptionMessage("There is an issue with a @SourceField annotation in class \"TheCodingMachine\GraphQL\Controllers\Fixtures\TestTypeMissingField\": Could not find a getter or a isser for field \"notExists\". Looked for: \"TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject::getNotExists()\", \"TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject::isNotExists()");
$queryProvider->getFields();
}

public function testFromSourceFieldsInterface()
{
$registry = new Registry(new EmptyContainer(),
new class implements AuthorizationServiceInterface {
public function isAllowed(string $right): bool
{
return true;
}
},
new VoidAuthenticationService(),
new AnnotationReader(),
$this->getTypeMapper(),
$this->getHydrator());

$queryProvider = new ControllerQueryProvider(new TestTypeWithSourceFieldInterface(), $registry);
$fields = $queryProvider->getFields();
$this->assertCount(1, $fields);

$this->assertSame('test', $fields[0]->name);

}

}
1 change: 0 additions & 1 deletion tests/Fixtures/TestType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace TheCodingMachine\GraphQL\Controllers\Fixtures;

use TheCodingMachine\GraphQL\Controllers\AbstractAnnotatedObjectType;
use TheCodingMachine\GraphQL\Controllers\Annotations\Right;
use TheCodingMachine\GraphQL\Controllers\Annotations\SourceField;
use TheCodingMachine\GraphQL\Controllers\Annotations\Field;
Expand Down
27 changes: 27 additions & 0 deletions tests/Fixtures/TestTypeWithSourceFieldInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php


namespace TheCodingMachine\GraphQL\Controllers\Fixtures;

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

/**
* @Type(class=TestObject::class)
*/
class TestTypeWithSourceFieldInterface implements FromSourceFieldsInterface
{
/**
* Dynamically returns the array of source fields to be fetched from the original object.
*
* @return SourceFieldInterface[]
*/
public function getSourceFields(): array
{
return [
new SourceField(['name'=>'test']),
];
}
}
1 change: 0 additions & 1 deletion tests/Fixtures/Types/FooType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace TheCodingMachine\GraphQL\Controllers\Fixtures\Types;

use TheCodingMachine\GraphQL\Controllers\AbstractAnnotatedObjectType;
use TheCodingMachine\GraphQL\Controllers\Annotations\Right;
use TheCodingMachine\GraphQL\Controllers\Annotations\SourceField;
use TheCodingMachine\GraphQL\Controllers\Annotations\Type;
Expand Down