Skip to content

Commit 9a7e5aa

Browse files
committed
Add attributes targetTypeQuery and targetTypeMutation on `@Provider
1 parent ef33fa7 commit 9a7e5aa

File tree

5 files changed

+89
-5
lines changed

5 files changed

+89
-5
lines changed

docs/annotations/annotations-reference.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,13 @@ 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 `targetType` attribute of the `@Mutation` annotation if it defined.
386+
or
387+
- The type(s) specified in the `targetTypeMutation` attribute of the `@Provider` annotation if it is defined.
388+
or
389+
- The root Query type of the default schema (define 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:
@@ -427,11 +433,19 @@ You can use `@Access` and/or `@IsPublic` on a provider class to add default acce
427433
Optional attributes:
428434

429435
- **prefix** : A prefix to apply to all field names from this provider
436+
- **targetTypeQuery** : The default GraphQL type(s) to attach the provider `@Query` to
437+
- **targetTypeMutation** : 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 `targetType` attribute of the `@Query` annotation if it defined.
444+
or
445+
- The type(s) specified in the `targetTypeQuery` attribute of the `@Provider` annotation if it is defined.
446+
or
447+
- The root Query type of the default schema (define 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:

src/Annotation/Provider.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,20 @@ final class Provider implements Annotation
1414
{
1515
/**
1616
* Optionnal prefix for provider fields.
17-
*
18-
* @var string
1917
*/
2018
public string $prefix;
19+
20+
/**
21+
* The default target types to attach the provider queries to.
22+
*
23+
* @var array<string>
24+
*/
25+
public array $targetTypeQuery;
26+
27+
/**
28+
* The default target types to attach the provider mutations to.
29+
*
30+
* @var array<string>
31+
*/
32+
public array $targetTypeMutation;
2133
}

src/Config/Parser/AnnotationParser.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,15 @@ private static function getGraphQLFieldsFromProviders(GraphClass $graphClass, st
712712

713713
$annotationTargets = $annotation->targetType ?? null;
714714

715+
if (null === $annotationTargets) {
716+
if ($annotation instanceof GQL\Mutation && isset($providerAnnotation->targetTypeMutation)) {
717+
$annotationTargets = $providerAnnotation->targetTypeMutation;
718+
}
719+
if ($annotation instanceof GQL\Query && isset($providerAnnotation->targetTypeQuery)) {
720+
$annotationTargets = $providerAnnotation->targetTypeQuery;
721+
}
722+
}
723+
715724
if (null === $annotationTargets) {
716725
if ($isDefaultTarget) {
717726
$annotationTargets = [$targetType];
@@ -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
}
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(targetTypeQuery={"RootQuery2"}, targetTypeMutation="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(targetType="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)