Skip to content

Commit eab51b3

Browse files
authored
Merge pull request overblog#749 from Vincz/provider-target-type
Add attributes `targetTypeQuery` and `targetTypeMutation` on `@Provider`
2 parents 22d48e9 + 2623cbd commit eab51b3

File tree

8 files changed

+115
-15
lines changed

8 files changed

+115
-15
lines changed

docs/annotations/annotations-reference.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,18 @@ class SecretArea {
381381
## @Mutation
382382

383383
This annotation applies on methods for classes tagged with the `@Provider` annotation. It indicates that the method on this class will resolve a Mutation field.
384-
The resulting field is added to the root Mutation type (defined in configuration at key `overblog_graphql.definitions.schema.mutation`).
384+
The corresponding GraphQL field is added to the GraphQL type(s) following the logic:
385+
- The type(s) specified in the `targetTypes` attribute of the `@Mutation` annotation if it's defined.
386+
or
387+
- The type(s) specified in the `targetMutationTypes` attribute of the `@Provider` annotation if it's defined.
388+
or
389+
- The root Query type of the default schema (defined in configuration at key `overblog_graphql.definitions.schema.mutation` or `overblog_graphql.definitions.schema.default.mutation`).
390+
385391
The class exposing the mutation(s) must be declared as a [service](https://symfony.com/doc/current/service_container.html).
386392

387393
Optional attributes:
388394

389-
- **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.
395+
- **targetTypes** : 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.
390396

391397
Example:
392398

@@ -426,17 +432,25 @@ You can use `@Access` and/or `@IsPublic` on a provider class to add default acce
426432

427433
Optional attributes:
428434

429-
- **prefix** : A prefix to apply to all field names from this provider
435+
- **prefix**: A prefix to apply to all field names from this provider
436+
- **targetQueryTypes**: The default GraphQL type(s) to attach the provider `@Query` to
437+
- **targetMutationTypes**: The default GraphQL type(s) to attach the provider `@Mutation` to
430438

431439
## @Query
432440

433441
This annotation applies on methods for classes tagged with the `@Provider` annotation. It indicates that on this class a method will resolve a Query field.
434-
By default, the resulting field is added to the root Query type (define in configuration at key `overblog_graphql.definitions.schema.query`).
442+
The corresponding GraphQL field is added to the GraphQL type(s) following the logic:
443+
- The type(s) specified in the `targetTypes` attribute of the `@Query` annotation if it's defined.
444+
or
445+
- The type(s) specified in the `targetQueryTypes` attribute of the `@Provider` annotation if it's defined.
446+
or
447+
- The root Query type of the default schema (defined in configuration at key `overblog_graphql.definitions.schema.query` or `overblog_graphql.definitions.schema.default.query`).
448+
435449
The class exposing the query(ies) must be declared as a [service](https://symfony.com/doc/current/service_container.html).
436450

437451
Optional attributes:
438452

439-
- **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.
453+
- **targetTypes** : 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.
440454

441455
Example:
442456

src/Annotation/Mutation.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@
1212
*/
1313
final class Mutation extends Field
1414
{
15+
/**
16+
* @var array<string>
17+
*
18+
* @deprecated This property is deprecated since 1.0 and will be removed in 1.1. Use $targetTypes instead.
19+
*/
20+
public array $targetType;
21+
1522
/**
1623
* The target types to attach this mutation to (useful when multiple schemas are allowed).
1724
*
1825
* @var array<string>
1926
*/
20-
public array $targetType;
27+
public array $targetTypes;
2128
}

src/Annotation/Provider.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,22 @@ final class Provider implements Annotation
1414
{
1515
/**
1616
* Optionnal prefix for provider fields.
17-
*
17+
*
1818
* @var string
1919
*/
2020
public string $prefix;
21+
22+
/**
23+
* The default target types to attach the provider queries to.
24+
*
25+
* @var array<string>
26+
*/
27+
public array $targetQueryTypes;
28+
29+
/**
30+
* The default target types to attach the provider mutations to.
31+
*
32+
* @var array<string>
33+
*/
34+
public array $targetMutationTypes;
2135
}

src/Annotation/Query.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@
1212
*/
1313
final class Query extends Field
1414
{
15+
/**
16+
* @var array<string>
17+
*
18+
* @deprecated This property is deprecated since 1.0 and will be removed in 1.1. Use $targetTypes instead.
19+
*/
20+
public array $targetType;
21+
1522
/**
1623
* The target types to attach this query to.
1724
*
1825
* @var array<string>
1926
*/
20-
public array $targetType;
27+
public array $targetTypes;
2128
}

src/Config/Parser/AnnotationParser.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,16 @@ private static function getGraphQLFieldsFromProviders(GraphClass $graphClass, st
710710
continue;
711711
}
712712

713-
$annotationTargets = $annotation->targetType ?? null;
713+
// TODO: Remove old property check in 1.1
714+
$annotationTargets = $annotation->targetTypes ?? $annotation->targetType ?? null;
715+
716+
if (null === $annotationTargets) {
717+
if ($annotation instanceof GQL\Mutation && isset($providerAnnotation->targetMutationTypes)) {
718+
$annotationTargets = $providerAnnotation->targetMutationTypes;
719+
} elseif ($annotation instanceof GQL\Query && isset($providerAnnotation->targetQueryTypes)) {
720+
$annotationTargets = $providerAnnotation->targetQueryTypes;
721+
}
722+
}
714723

715724
if (null === $annotationTargets) {
716725
if ($isDefaultTarget) {
@@ -728,7 +737,7 @@ private static function getGraphQLFieldsFromProviders(GraphClass $graphClass, st
728737
}
729738

730739
if (!$annotation instanceof $expectedAnnotation) {
731-
if (GQL\Mutation::class == $expectedAnnotation) {
740+
if (GQL\Mutation::class === $expectedAnnotation) {
732741
$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);
733742
} else {
734743
$message = sprintf('The provider "%s" try to add a mutation on type "%s" (through @Mutation on method "%s") but "%s" is not a mutation.', $providerMetadata->getName(), $targetType, $method->getName(), $targetType);

tests/Config/Parser/AnnotationParserTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ public function testProviders(): void
276276
'access' => '@=default_access',
277277
'public' => '@=default_public',
278278
],
279+
'countSecretWeapons' => [
280+
'type' => 'Int!',
281+
'resolve' => "@=call(service('Overblog\\\\GraphQLBundle\\\\Tests\\\\Config\\\\Parser\\\\fixtures\\\\annotations\\\\Repository\\\\WeaponRepository').countSecretWeapons, arguments({}, args))",
282+
],
279283
],
280284
]);
281285

@@ -316,6 +320,10 @@ public function testProvidersMultischema(): void
316320
'access' => '@=default_access',
317321
'public' => '@=default_public',
318322
],
323+
'hasSecretWeapons' => [
324+
'type' => 'Boolean!',
325+
'resolve' => "@=call(service('Overblog\\\\GraphQLBundle\\\\Tests\\\\Config\\\\Parser\\\\fixtures\\\\annotations\\\\Repository\\\\WeaponRepository').hasSecretWeapons, arguments({}, args))",
326+
],
319327
],
320328
]);
321329

@@ -335,6 +343,10 @@ public function testProvidersMultischema(): void
335343
'access' => '@=default_access',
336344
'public' => '@=default_public',
337345
],
346+
'createLightsaber' => [
347+
'type' => 'Boolean!',
348+
'resolve' => "@=call(service('Overblog\\\\GraphQLBundle\\\\Tests\\\\Config\\\\Parser\\\\fixtures\\\\annotations\\\\Repository\\\\WeaponRepository').createLightsaber, arguments({}, args))",
349+
],
338350
],
339351
]);
340352
}

tests/Config/Parser/fixtures/annotations/Repository/PlanetRepository.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ public function getAllowedPlanetsForDroids(): array
4545
}
4646

4747
/**
48-
* @GQL\Query(type="Planet", targetType="RootQuery2")
48+
* @GQL\Query(type="Planet", targetTypes="RootQuery2")
4949
*/
5050
public function getPlanetSchema2(): ?Planet
5151
{
5252
return null;
5353
}
5454

5555
/**
56-
* @GQL\Mutation(type="Planet", targetType="RootMutation2", args={
56+
* @GQL\Mutation(type="Planet", targetTypes="RootMutation2", args={
5757
* @GQL\Arg(type="PlanetInput!", name="planetInput")
5858
* })
5959
* @GQL\IsPublic("override_public")
@@ -64,23 +64,23 @@ public function createPlanetSchema2(array $planetInput): array
6464
}
6565

6666
/**
67-
* @GQL\Mutation(targetType={"RootMutation", "RootMutation2"})
67+
* @GQL\Mutation(targetTypes={"RootMutation", "RootMutation2"})
6868
*/
6969
public function destroyPlanet(int $planetId): bool
7070
{
7171
return true;
7272
}
7373

7474
/**
75-
* @GQL\Query(targetType={"RootQuery", "RootQuery2"})
75+
* @GQL\Query(targetTypes={"RootQuery", "RootQuery2"})
7676
*/
7777
public function isPlanetDestroyed(int $planetId): bool
7878
{
7979
return true;
8080
}
8181

8282
/**
83-
* @GQL\Query(targetType={"Droid", "Mandalorian"}, name="armorResistance")
83+
* @GQL\Query(targetTypes={"Droid", "Mandalorian"}, name="armorResistance")
8484
*/
8585
public function getArmorResistance(): int
8686
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Tests\Config\Parser\fixtures\annotations\Repository;
6+
7+
use Overblog\GraphQLBundle\Annotation as GQL;
8+
9+
/**
10+
* @GQL\Provider(targetQueryTypes={"RootQuery2"}, targetMutationTypes="RootMutation2")
11+
*/
12+
class WeaponRepository
13+
{
14+
/**
15+
* @GQL\Query
16+
*/
17+
public function hasSecretWeapons(): bool
18+
{
19+
return true;
20+
}
21+
22+
/**
23+
* @GQL\Query(targetTypes="RootQuery")
24+
*/
25+
public function countSecretWeapons(): int
26+
{
27+
return 2;
28+
}
29+
30+
/**
31+
* @GQL\Mutation
32+
*/
33+
public function createLightsaber(): bool
34+
{
35+
return true;
36+
}
37+
}

0 commit comments

Comments
 (0)