Skip to content

Allow multiple target types #726

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 4 commits into from
Aug 13, 2020
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: 6 additions & 7 deletions docs/annotations/annotations-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ The class exposing the mutation(s) must be declared as a [service](https://symfo

Optional attributes:

- **targetType** : The GraphQL type to attach the field to. It must be a mutation. (by default, it'll be the root Mutation type of the default schema. see [Default Schema](../definitions/schema.md#default-schema)).
- **targetType** : The GraphQL type(s) to attach the field to. It must be a mutation. (by default, it'll be the root Mutation type of the default schema. see [Default Schema](../definitions/schema.md#default-schema)). You can specify one or multiple target types.

Example:

Expand Down Expand Up @@ -422,7 +422,7 @@ class MutationProvider {

This annotation applies on classes to indicate that it contains methods tagged with `@Query` or `@Mutation`.
Without it, the `@Query` and `@Mutation` are ignored. When used, **remember to have a corresponding service with the fully qualified name of the class as service id**.
You can use `@Access` and/or `@IsPublic` on a provider class to add default access or visibility on defined query or mutation.
You can use `@Access` and/or `@IsPublic` on a provider class to add default access or visibility on defined query or mutation.

Optional attributes:

Expand All @@ -436,7 +436,7 @@ The class exposing the query(ies) must be declared as a [service](https://symfon

Optional attributes:

- **targetType** : The GraphQL type to attach the field to (by default, it'll be the root Query type of the default schema. see [Default Schema](../definitions/schema.md#default-schema)).
- **targetType** : The GraphQL type(s) to attach the field to (by default, it'll be the root Query type of the default schema. see [Default Schema](../definitions/schema.md#default-schema)). You can specify one or multiple target types.

Example:

Expand Down Expand Up @@ -589,19 +589,18 @@ class Pet {
}
```


## @Relay\Connection

This annotation extends the `@Type` annotation so it uses the same attributes.
This annotation extends the `@Type` annotation so it uses the same attributes.
It prepends the `RelayConnectionFieldsBuilder` to the list of fields builders.

The extra attributes are :

- **edge** : The GraphQL type of the connection's edges
- **node** : The GraphQL type of the node of the connection's edges'
- **node** : The GraphQL type of the node of the connection's edges'

You must define one and only one of this attributes.
If the `edge` attribute is used, the declaration is the same as adding a `RelayConnectionFieldsBuilder`
If the `edge` attribute is used, the declaration is the same as adding a `RelayConnectionFieldsBuilder`

```php
<?php
Expand Down
4 changes: 2 additions & 2 deletions src/Annotation/Mutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
final class Mutation extends Field
{
/**
* The target type to attach this mutation to (usefull when multiple schemas are allowed).
* The target types to attach this mutation to (useful when multiple schemas are allowed).
*
* @var string
* @var array<string>
*/
public $targetType;
}
4 changes: 2 additions & 2 deletions src/Annotation/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
final class Query extends Field
{
/**
* The target type to attach this query to.
* The target types to attach this query to.
*
* @var string
* @var array<string>
*/
public $targetType;
}
21 changes: 15 additions & 6 deletions src/Config/Parser/AnnotationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,19 +674,28 @@ private static function getGraphQLFieldsFromProviders(GraphClass $graphClass, st
continue;
}

$annotationTarget = $annotation->targetType;
if (!$annotationTarget && $isDefaultTarget) {
$annotationTarget = $targetType;
if (!($annotation instanceof $expectedAnnotation)) {
$annotationTargets = $annotation->targetType;

if (null === $annotationTargets) {
if ($isDefaultTarget) {
$annotationTargets = [$targetType];
if (!$annotation instanceof $expectedAnnotation) {
continue;
}
} else {
continue;
}
}

if ($annotationTarget !== $targetType) {
if (is_string($annotationTargets)) {
$annotationTargets = [$annotationTargets];
}

if (!in_array($targetType, $annotationTargets)) {
continue;
}

if (!($annotation instanceof $expectedAnnotation)) {
if (!$annotation instanceof $expectedAnnotation) {
if (GQL\Mutation::class == $expectedAnnotation) {
$message = sprintf('The provider "%s" try to add a query field on type "%s" (through @Query on method "%s") but "%s" is a mutation.', $providerMetadata->getName(), $targetType, $method->getName(), $targetType);
} else {
Expand Down
40 changes: 40 additions & 0 deletions tests/Config/Parser/AnnotationParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public function testTypes(): void
'access' => '@=override_access',
'public' => '@=default_public',
],
'planet_armorResistance' => [
'type' => 'Int!',
'resolve' => '@=call(service(\'Overblog\\\\GraphQLBundle\\\\Tests\\\\Config\\\\Parser\\\\fixtures\\\\annotations\\\\Repository\\\\PlanetRepository\').getArmorResistance, arguments({}, args))',
'access' => '@=default_access',
'public' => '@=default_public',
],
],
]);

Expand Down Expand Up @@ -228,6 +234,12 @@ public function testInterfaceAutoguessed(): void
'fields' => [
'name' => ['type' => 'String!', 'description' => 'The name of the character'],
'friends' => ['type' => '[Character]', 'description' => 'The friends of the character', 'resolve' => "@=resolver('App\\\\MyResolver::getFriends')"],
'planet_armorResistance' => [
'type' => 'Int!',
'resolve' => '@=call(service(\'Overblog\\\\GraphQLBundle\\\\Tests\\\\Config\\\\Parser\\\\fixtures\\\\annotations\\\\Repository\\\\PlanetRepository\').getArmorResistance, arguments({}, args))',
'access' => '@=default_access',
'public' => '@=default_public',
],
],
]);
}
Expand All @@ -253,6 +265,13 @@ public function testProviders(): void
'access' => '@=default_access',
'public' => '@=default_public',
],
'planet_isPlanetDestroyed' => [
'type' => 'Boolean!',
'args' => ['planetId' => ['type' => 'Int!']],
'resolve' => "@=call(service('Overblog\\\\GraphQLBundle\\\\Tests\\\\Config\\\\Parser\\\\fixtures\\\\annotations\\\\Repository\\\\PlanetRepository').isPlanetDestroyed, arguments({planetId: \"Int!\"}, args))",
'access' => '@=default_access',
'public' => '@=default_public',
],
],
]);

Expand All @@ -265,6 +284,13 @@ public function testProviders(): void
'access' => '@=default_access',
'public' => '@=override_public',
],
'planet_destroyPlanet' => [
'type' => 'Boolean!',
'args' => ['planetId' => ['type' => 'Int!']],
'resolve' => "@=call(service('Overblog\\\\GraphQLBundle\\\\Tests\\\\Config\\\\Parser\\\\fixtures\\\\annotations\\\\Repository\\\\PlanetRepository').destroyPlanet, arguments({planetId: \"Int!\"}, args))",
'access' => '@=default_access',
'public' => '@=default_public',
],
],
]);
}
Expand All @@ -279,6 +305,13 @@ public function testProvidersMultischema(): void
'access' => '@=default_access',
'public' => '@=default_public',
],
'planet_isPlanetDestroyed' => [
'type' => 'Boolean!',
'args' => ['planetId' => ['type' => 'Int!']],
'resolve' => "@=call(service('Overblog\\\\GraphQLBundle\\\\Tests\\\\Config\\\\Parser\\\\fixtures\\\\annotations\\\\Repository\\\\PlanetRepository').isPlanetDestroyed, arguments({planetId: \"Int!\"}, args))",
'access' => '@=default_access',
'public' => '@=default_public',
],
],
]);

Expand All @@ -291,6 +324,13 @@ public function testProvidersMultischema(): void
'access' => '@=default_access',
'public' => '@=override_public',
],
'planet_destroyPlanet' => [
'type' => 'Boolean!',
'args' => ['planetId' => ['type' => 'Int!']],
'resolve' => "@=call(service('Overblog\\\\GraphQLBundle\\\\Tests\\\\Config\\\\Parser\\\\fixtures\\\\annotations\\\\Repository\\\\PlanetRepository').destroyPlanet, arguments({planetId: \"Int!\"}, args))",
'access' => '@=default_access',
'public' => '@=default_public',
],
],
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,28 @@ public function createPlanetSchema2(array $planetInput): array
{
return [];
}

/**
* @GQL\Mutation(targetType={"RootMutation", "RootMutation2"})
*/
public function destroyPlanet(int $planetId): bool
{
return true;
}

/**
* @GQL\Query(targetType={"RootQuery", "RootQuery2"})
*/
public function isPlanetDestroyed(int $planetId): bool
{
return true;
}

/**
* @GQL\Query(targetType={"Droid", "Mandalorian"}, name="armorResistance")
*/
public function getArmorResistance(): int
{
return 10;
}
}