From 9a7e5aa91bcec48fd07ed19bbd9f2322c8d125f6 Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 19 Aug 2020 15:48:37 +0200 Subject: [PATCH 1/6] Add attributes `targetTypeQuery` and `targetTypeMutation` on `@Provider --- docs/annotations/annotations-reference.md | 18 ++++++++- src/Annotation/Provider.php | 16 +++++++- src/Config/Parser/AnnotationParser.php | 11 +++++- tests/Config/Parser/AnnotationParserTest.php | 12 ++++++ .../Repository/WeaponRepository.php | 37 +++++++++++++++++++ 5 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 tests/Config/Parser/fixtures/annotations/Repository/WeaponRepository.php diff --git a/docs/annotations/annotations-reference.md b/docs/annotations/annotations-reference.md index 8f4a5feff..cc1e7ed08 100644 --- a/docs/annotations/annotations-reference.md +++ b/docs/annotations/annotations-reference.md @@ -381,7 +381,13 @@ class SecretArea { ## @Mutation 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. -The resulting field is added to the root Mutation type (defined in configuration at key `overblog_graphql.definitions.schema.mutation`). +The corresponding GraphQL field is added to the GraphQL type(s) following the logic : +- The type(s) specified in the `targetType` attribute of the `@Mutation` annotation if it defined. + or +- The type(s) specified in the `targetTypeMutation` attribute of the `@Provider` annotation if it is defined. + or +- 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`). + The class exposing the mutation(s) must be declared as a [service](https://symfony.com/doc/current/service_container.html). Optional attributes: @@ -427,11 +433,19 @@ You can use `@Access` and/or `@IsPublic` on a provider class to add default acce Optional attributes: - **prefix** : A prefix to apply to all field names from this provider +- **targetTypeQuery** : The default GraphQL type(s) to attach the provider `@Query` to +- **targetTypeMutation** : The default GraphQL type(s) to attach the provider `@Mutation` to ## @Query 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. -By default, the resulting field is added to the root Query type (define in configuration at key `overblog_graphql.definitions.schema.query`). +The corresponding GraphQL field is added to the GraphQL type(s) following the logic : +- The type(s) specified in the `targetType` attribute of the `@Query` annotation if it defined. + or +- The type(s) specified in the `targetTypeQuery` attribute of the `@Provider` annotation if it is defined. + or +- 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`). + The class exposing the query(ies) must be declared as a [service](https://symfony.com/doc/current/service_container.html). Optional attributes: diff --git a/src/Annotation/Provider.php b/src/Annotation/Provider.php index 388b20770..f5ee9cec6 100644 --- a/src/Annotation/Provider.php +++ b/src/Annotation/Provider.php @@ -14,8 +14,20 @@ final class Provider implements Annotation { /** * Optionnal prefix for provider fields. - * - * @var string */ public string $prefix; + + /** + * The default target types to attach the provider queries to. + * + * @var array + */ + public array $targetTypeQuery; + + /** + * The default target types to attach the provider mutations to. + * + * @var array + */ + public array $targetTypeMutation; } diff --git a/src/Config/Parser/AnnotationParser.php b/src/Config/Parser/AnnotationParser.php index 890e5430e..78d9bf486 100644 --- a/src/Config/Parser/AnnotationParser.php +++ b/src/Config/Parser/AnnotationParser.php @@ -712,6 +712,15 @@ private static function getGraphQLFieldsFromProviders(GraphClass $graphClass, st $annotationTargets = $annotation->targetType ?? null; + if (null === $annotationTargets) { + if ($annotation instanceof GQL\Mutation && isset($providerAnnotation->targetTypeMutation)) { + $annotationTargets = $providerAnnotation->targetTypeMutation; + } + if ($annotation instanceof GQL\Query && isset($providerAnnotation->targetTypeQuery)) { + $annotationTargets = $providerAnnotation->targetTypeQuery; + } + } + if (null === $annotationTargets) { if ($isDefaultTarget) { $annotationTargets = [$targetType]; @@ -728,7 +737,7 @@ private static function getGraphQLFieldsFromProviders(GraphClass $graphClass, st } if (!$annotation instanceof $expectedAnnotation) { - if (GQL\Mutation::class == $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 { $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); diff --git a/tests/Config/Parser/AnnotationParserTest.php b/tests/Config/Parser/AnnotationParserTest.php index 3585ebae0..3c391ecbe 100644 --- a/tests/Config/Parser/AnnotationParserTest.php +++ b/tests/Config/Parser/AnnotationParserTest.php @@ -276,6 +276,10 @@ public function testProviders(): void 'access' => '@=default_access', 'public' => '@=default_public', ], + 'countSecretWeapons' => [ + 'type' => 'Int!', + 'resolve' => "@=call(service('Overblog\\\\GraphQLBundle\\\\Tests\\\\Config\\\\Parser\\\\fixtures\\\\annotations\\\\Repository\\\\WeaponRepository').countSecretWeapons, arguments({}, args))", + ], ], ]); @@ -316,6 +320,10 @@ public function testProvidersMultischema(): void 'access' => '@=default_access', 'public' => '@=default_public', ], + 'hasSecretWeapons' => [ + 'type' => 'Boolean!', + 'resolve' => "@=call(service('Overblog\\\\GraphQLBundle\\\\Tests\\\\Config\\\\Parser\\\\fixtures\\\\annotations\\\\Repository\\\\WeaponRepository').hasSecretWeapons, arguments({}, args))", + ], ], ]); @@ -335,6 +343,10 @@ public function testProvidersMultischema(): void 'access' => '@=default_access', 'public' => '@=default_public', ], + 'createLightsaber' => [ + 'type' => 'Boolean!', + 'resolve' => "@=call(service('Overblog\\\\GraphQLBundle\\\\Tests\\\\Config\\\\Parser\\\\fixtures\\\\annotations\\\\Repository\\\\WeaponRepository').createLightsaber, arguments({}, args))", + ], ], ]); } diff --git a/tests/Config/Parser/fixtures/annotations/Repository/WeaponRepository.php b/tests/Config/Parser/fixtures/annotations/Repository/WeaponRepository.php new file mode 100644 index 000000000..913c23ef3 --- /dev/null +++ b/tests/Config/Parser/fixtures/annotations/Repository/WeaponRepository.php @@ -0,0 +1,37 @@ + Date: Thu, 20 Aug 2020 08:39:25 +0200 Subject: [PATCH 2/6] Apply suggestions from code review Co-authored-by: Timur Murtukov --- docs/annotations/annotations-reference.md | 22 +++++++++++----------- src/Config/Parser/AnnotationParser.php | 3 +-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/docs/annotations/annotations-reference.md b/docs/annotations/annotations-reference.md index cc1e7ed08..185c1272b 100644 --- a/docs/annotations/annotations-reference.md +++ b/docs/annotations/annotations-reference.md @@ -381,12 +381,12 @@ class SecretArea { ## @Mutation 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. -The corresponding GraphQL field is added to the GraphQL type(s) following the logic : -- The type(s) specified in the `targetType` attribute of the `@Mutation` annotation if it defined. +The corresponding GraphQL field is added to the GraphQL type(s) following the logic: +- The type(s) specified in the `targetType` attribute of the `@Mutation` annotation if it's defined. or -- The type(s) specified in the `targetTypeMutation` attribute of the `@Provider` annotation if it is defined. +- The type(s) specified in the `targetTypeMutation` attribute of the `@Provider` annotation if it's defined. or -- 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`). +- 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`). The class exposing the mutation(s) must be declared as a [service](https://symfony.com/doc/current/service_container.html). @@ -432,19 +432,19 @@ You can use `@Access` and/or `@IsPublic` on a provider class to add default acce Optional attributes: -- **prefix** : A prefix to apply to all field names from this provider -- **targetTypeQuery** : The default GraphQL type(s) to attach the provider `@Query` to -- **targetTypeMutation** : The default GraphQL type(s) to attach the provider `@Mutation` to +- **prefix**: A prefix to apply to all field names from this provider +- **targetTypeQuery**: The default GraphQL type(s) to attach the provider `@Query` to +- **targetTypeMutation**: The default GraphQL type(s) to attach the provider `@Mutation` to ## @Query 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. -The corresponding GraphQL field is added to the GraphQL type(s) following the logic : -- The type(s) specified in the `targetType` attribute of the `@Query` annotation if it defined. +The corresponding GraphQL field is added to the GraphQL type(s) following the logic: +- The type(s) specified in the `targetType` attribute of the `@Query` annotation if it's defined. or -- The type(s) specified in the `targetTypeQuery` attribute of the `@Provider` annotation if it is defined. +- The type(s) specified in the `targetTypeQuery` attribute of the `@Provider` annotation if it's defined. or -- 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`). +- 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`). The class exposing the query(ies) must be declared as a [service](https://symfony.com/doc/current/service_container.html). diff --git a/src/Config/Parser/AnnotationParser.php b/src/Config/Parser/AnnotationParser.php index 0c47c1805..3e1f60902 100644 --- a/src/Config/Parser/AnnotationParser.php +++ b/src/Config/Parser/AnnotationParser.php @@ -715,8 +715,7 @@ private static function getGraphQLFieldsFromProviders(GraphClass $graphClass, st if (null === $annotationTargets) { if ($annotation instanceof GQL\Mutation && isset($providerAnnotation->targetTypeMutation)) { $annotationTargets = $providerAnnotation->targetTypeMutation; - } - if ($annotation instanceof GQL\Query && isset($providerAnnotation->targetTypeQuery)) { + } elseif ($annotation instanceof GQL\Query && isset($providerAnnotation->targetTypeQuery)) { $annotationTargets = $providerAnnotation->targetTypeQuery; } } From 9ed385fa93a78421601bea502ee2b4b70d5ed01c Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 20 Aug 2020 08:47:46 +0200 Subject: [PATCH 3/6] Fix @Provider annotation --- src/Annotation/Provider.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Annotation/Provider.php b/src/Annotation/Provider.php index f5ee9cec6..d87559b6f 100644 --- a/src/Annotation/Provider.php +++ b/src/Annotation/Provider.php @@ -14,6 +14,8 @@ final class Provider implements Annotation { /** * Optionnal prefix for provider fields. + * + * @var string */ public string $prefix; From f37f644bd18e55f092eee74fffdec24e0ba1eeab Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 20 Aug 2020 12:05:55 +0200 Subject: [PATCH 4/6] Change annotation attributes names --- docs/annotations/annotations-reference.md | 12 ++++++------ src/Annotation/Mutation.php | 8 +++++++- src/Annotation/Provider.php | 4 ++-- src/Annotation/Query.php | 8 +++++++- src/Config/Parser/AnnotationParser.php | 11 ++++++----- .../annotations/Repository/PlanetRepository.php | 10 +++++----- .../annotations/Repository/WeaponRepository.php | 4 ++-- 7 files changed, 35 insertions(+), 22 deletions(-) diff --git a/docs/annotations/annotations-reference.md b/docs/annotations/annotations-reference.md index 185c1272b..eefb92214 100644 --- a/docs/annotations/annotations-reference.md +++ b/docs/annotations/annotations-reference.md @@ -382,9 +382,9 @@ class SecretArea { 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. The corresponding GraphQL field is added to the GraphQL type(s) following the logic: -- The type(s) specified in the `targetType` attribute of the `@Mutation` annotation if it's defined. +- The type(s) specified in the `targetTypes` attribute of the `@Mutation` annotation if it's defined. or -- The type(s) specified in the `targetTypeMutation` attribute of the `@Provider` annotation if it's defined. +- The type(s) specified in the `targetMutationTypes` attribute of the `@Provider` annotation if it's defined. or - 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`). @@ -392,7 +392,7 @@ The class exposing the mutation(s) must be declared as a [service](https://symfo Optional attributes: -- **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. +- **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. Example: @@ -440,9 +440,9 @@ Optional attributes: 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. The corresponding GraphQL field is added to the GraphQL type(s) following the logic: -- The type(s) specified in the `targetType` attribute of the `@Query` annotation if it's defined. +- The type(s) specified in the `targetTypes` attribute of the `@Query` annotation if it's defined. or -- The type(s) specified in the `targetTypeQuery` attribute of the `@Provider` annotation if it's defined. +- The type(s) specified in the `targetQueryTypes` attribute of the `@Provider` annotation if it's defined. or - 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`). @@ -450,7 +450,7 @@ The class exposing the query(ies) must be declared as a [service](https://symfon Optional attributes: -- **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. +- **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. Example: diff --git a/src/Annotation/Mutation.php b/src/Annotation/Mutation.php index febd82040..5eddf4db5 100644 --- a/src/Annotation/Mutation.php +++ b/src/Annotation/Mutation.php @@ -12,10 +12,16 @@ */ final class Mutation extends Field { + /** + * @deprecated + * @var array + */ + public array $targetType; + /** * The target types to attach this mutation to (useful when multiple schemas are allowed). * * @var array */ - public array $targetType; + public array $targetTypes; } diff --git a/src/Annotation/Provider.php b/src/Annotation/Provider.php index d87559b6f..0b04438bf 100644 --- a/src/Annotation/Provider.php +++ b/src/Annotation/Provider.php @@ -24,12 +24,12 @@ final class Provider implements Annotation * * @var array */ - public array $targetTypeQuery; + public array $targetQueryTypes; /** * The default target types to attach the provider mutations to. * * @var array */ - public array $targetTypeMutation; + public array $targetMutationTypes; } diff --git a/src/Annotation/Query.php b/src/Annotation/Query.php index 993156c1c..158b74e99 100644 --- a/src/Annotation/Query.php +++ b/src/Annotation/Query.php @@ -12,10 +12,16 @@ */ final class Query extends Field { + /** + * @deprecated + * @var array + */ + public array $targetType; + /** * The target types to attach this query to. * * @var array */ - public array $targetType; + public array $targetTypes; } diff --git a/src/Config/Parser/AnnotationParser.php b/src/Config/Parser/AnnotationParser.php index 3e1f60902..8ca6a6901 100644 --- a/src/Config/Parser/AnnotationParser.php +++ b/src/Config/Parser/AnnotationParser.php @@ -710,13 +710,14 @@ private static function getGraphQLFieldsFromProviders(GraphClass $graphClass, st continue; } - $annotationTargets = $annotation->targetType ?? null; + // TODO: Remove old property check in 1.1 + $annotationTargets = $annotation->targetTypes ?? $annotation->targetType ?? null; if (null === $annotationTargets) { - if ($annotation instanceof GQL\Mutation && isset($providerAnnotation->targetTypeMutation)) { - $annotationTargets = $providerAnnotation->targetTypeMutation; - } elseif ($annotation instanceof GQL\Query && isset($providerAnnotation->targetTypeQuery)) { - $annotationTargets = $providerAnnotation->targetTypeQuery; + if ($annotation instanceof GQL\Mutation && isset($providerAnnotation->targetMutationTypes)) { + $annotationTargets = $providerAnnotation->targetMutationTypes; + } elseif ($annotation instanceof GQL\Query && isset($providerAnnotation->targetQueryTypes)) { + $annotationTargets = $providerAnnotation->targetQueryTypes; } } diff --git a/tests/Config/Parser/fixtures/annotations/Repository/PlanetRepository.php b/tests/Config/Parser/fixtures/annotations/Repository/PlanetRepository.php index 562dce356..ccb5a2490 100644 --- a/tests/Config/Parser/fixtures/annotations/Repository/PlanetRepository.php +++ b/tests/Config/Parser/fixtures/annotations/Repository/PlanetRepository.php @@ -45,7 +45,7 @@ public function getAllowedPlanetsForDroids(): array } /** - * @GQL\Query(type="Planet", targetType="RootQuery2") + * @GQL\Query(type="Planet", targetTypes="RootQuery2") */ public function getPlanetSchema2(): ?Planet { @@ -53,7 +53,7 @@ public function getPlanetSchema2(): ?Planet } /** - * @GQL\Mutation(type="Planet", targetType="RootMutation2", args={ + * @GQL\Mutation(type="Planet", targetTypes="RootMutation2", args={ * @GQL\Arg(type="PlanetInput!", name="planetInput") * }) * @GQL\IsPublic("override_public") @@ -64,7 +64,7 @@ public function createPlanetSchema2(array $planetInput): array } /** - * @GQL\Mutation(targetType={"RootMutation", "RootMutation2"}) + * @GQL\Mutation(targetTypes={"RootMutation", "RootMutation2"}) */ public function destroyPlanet(int $planetId): bool { @@ -72,7 +72,7 @@ public function destroyPlanet(int $planetId): bool } /** - * @GQL\Query(targetType={"RootQuery", "RootQuery2"}) + * @GQL\Query(targetTypes={"RootQuery", "RootQuery2"}) */ public function isPlanetDestroyed(int $planetId): bool { @@ -80,7 +80,7 @@ public function isPlanetDestroyed(int $planetId): bool } /** - * @GQL\Query(targetType={"Droid", "Mandalorian"}, name="armorResistance") + * @GQL\Query(targetTypes={"Droid", "Mandalorian"}, name="armorResistance") */ public function getArmorResistance(): int { diff --git a/tests/Config/Parser/fixtures/annotations/Repository/WeaponRepository.php b/tests/Config/Parser/fixtures/annotations/Repository/WeaponRepository.php index 913c23ef3..4a596fac2 100644 --- a/tests/Config/Parser/fixtures/annotations/Repository/WeaponRepository.php +++ b/tests/Config/Parser/fixtures/annotations/Repository/WeaponRepository.php @@ -7,7 +7,7 @@ use Overblog\GraphQLBundle\Annotation as GQL; /** - * @GQL\Provider(targetTypeQuery={"RootQuery2"}, targetTypeMutation="RootMutation2") + * @GQL\Provider(targetQueryTypes={"RootQuery2"}, targetMutationTypes="RootMutation2") */ class WeaponRepository { @@ -20,7 +20,7 @@ public function hasSecretWeapons(): bool } /** - * @GQL\Query(targetType="RootQuery") + * @GQL\Query(targetTypes="RootQuery") */ public function countSecretWeapons(): int { From fcb39c1edaacacc6b5ee1854948589cccc1b100a Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 20 Aug 2020 12:48:22 +0200 Subject: [PATCH 5/6] Apply suggestions from code review Co-authored-by: Timur Murtukov --- docs/annotations/annotations-reference.md | 4 ++-- src/Annotation/Query.php | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/annotations/annotations-reference.md b/docs/annotations/annotations-reference.md index eefb92214..8dabff70b 100644 --- a/docs/annotations/annotations-reference.md +++ b/docs/annotations/annotations-reference.md @@ -433,8 +433,8 @@ You can use `@Access` and/or `@IsPublic` on a provider class to add default acce Optional attributes: - **prefix**: A prefix to apply to all field names from this provider -- **targetTypeQuery**: The default GraphQL type(s) to attach the provider `@Query` to -- **targetTypeMutation**: The default GraphQL type(s) to attach the provider `@Mutation` to +- **targetQueryTypes**: The default GraphQL type(s) to attach the provider `@Query` to +- **targetMutationTypes**: The default GraphQL type(s) to attach the provider `@Mutation` to ## @Query diff --git a/src/Annotation/Query.php b/src/Annotation/Query.php index 158b74e99..bd698fef1 100644 --- a/src/Annotation/Query.php +++ b/src/Annotation/Query.php @@ -13,8 +13,9 @@ final class Query extends Field { /** - * @deprecated * @var array + * + * @deprecated This property is deprecated since 1.0 and will be removed in 1.1. Use $targetTypes instead. */ public array $targetType; From 2623cbdedacdc99b657cd74266fd17fb7db97a7b Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 20 Aug 2020 15:07:56 +0200 Subject: [PATCH 6/6] Update src/Annotation/Mutation.php Co-authored-by: Timur Murtukov --- src/Annotation/Mutation.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Annotation/Mutation.php b/src/Annotation/Mutation.php index 5eddf4db5..f1dd2b040 100644 --- a/src/Annotation/Mutation.php +++ b/src/Annotation/Mutation.php @@ -13,8 +13,9 @@ final class Mutation extends Field { /** - * @deprecated * @var array + * + * @deprecated This property is deprecated since 1.0 and will be removed in 1.1. Use $targetTypes instead. */ public array $targetType;