Skip to content

Commit 8e2b613

Browse files
committed
Add targetType property to Mutation
1 parent 5e87e22 commit 8e2b613

File tree

7 files changed

+100
-2
lines changed

7 files changed

+100
-2
lines changed

docs/annotations/annotations-reference.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@ This annotation applies on methods for classes tagged with the `@Provider` annot
384384
The resulting field is added to the root Mutation type (defined in configuration at key `overblog_graphql.definitions.schema.mutation`).
385385
The class exposing the mutation(s) must be declared as a [service](https://symfony.com/doc/current/service_container.html).
386386

387+
Optional attributes:
388+
389+
- **targetType** : The GraphQL type to attach the field to (by default, it'll be the root Mutation type).
390+
387391
Example:
388392

389393
This will add an `updateUserEmail` mutation, with as resolver `@=service('App\Graphql\MutationProvider').updateUserEmail(...)`.

src/Annotation/Mutation.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@
1212
*/
1313
final class Mutation extends Field
1414
{
15+
/**
16+
* The target type to attach this query to.
17+
*
18+
* @var string
19+
*/
20+
public $targetType;
1521
}

src/Config/Parser/AnnotationParser.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,22 @@ private static function typeAnnotationToGQLConfiguration(
289289
$isRootMutation = ($rootMutationType && $gqlName === $rootMutationType);
290290
$currentValue = ($isRootQuery || $isRootMutation) ? \sprintf("service('%s')", self::formatNamespaceForExpression($reflectionEntity->getName())) : 'value';
291291

292+
$queryType = $isRootMutation ? 'Mutation' : 'Query';
293+
294+
if (isset($configs['definitions']['schema'])) {
295+
$mutationTypes = [];
296+
297+
foreach ($configs['definitions']['schema'] as $key => $value) {
298+
if (isset($configs['definitions']['schema'][$key]['mutation'])) {
299+
$mutationTypes[] = $configs['definitions']['schema'][$key]['mutation'];
300+
}
301+
}
302+
303+
$queryType = in_array($gqlName, $mutationTypes) ? 'Mutation' : 'Query';
304+
}
305+
292306
$gqlConfiguration = self::graphQLTypeConfigFromAnnotation($classAnnotation, $classAnnotations, $properties, $methods, $reflectionEntity->getNamespaceName(), $currentValue);
293-
$providerFields = self::getGraphQLFieldsFromProviders($reflectionEntity->getNamespaceName(), $isRootMutation ? 'Mutation' : 'Query', $gqlName, ($isRootQuery || $isRootMutation));
307+
$providerFields = self::getGraphQLFieldsFromProviders($reflectionEntity->getNamespaceName(), $queryType, $gqlName, ($isRootQuery || $isRootMutation));
294308
$gqlConfiguration['config']['fields'] = $providerFields + $gqlConfiguration['config']['fields'];
295309

296310
if ($classAnnotation instanceof GQL\Relay\Edge) {
@@ -673,7 +687,7 @@ private static function getGraphQLFieldsFromProviders(string $namespace, string
673687
continue;
674688
}
675689

676-
$annotationTarget = 'Query' === $annotationName ? $annotation->targetType : null;
690+
$annotationTarget = in_array($annotationName, ['Query', 'Mutation']) ? $annotation->targetType : null;
677691
if (!$annotationTarget && $isRoot) {
678692
$annotationTarget = $targetType;
679693
}

tests/Config/Parser/AnnotationParserTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class AnnotationParserTest extends TestCase
1414
'definitions' => [
1515
'schema' => [
1616
'default' => ['query' => 'RootQuery', 'mutation' => 'RootMutation'],
17+
'alternative' => ['query' => 'AlternativeQuery', 'mutation' => 'AlternativeMutation'],
1718
],
1819
],
1920
'doctrine' => [
@@ -229,6 +230,30 @@ public function testProviders(): void
229230
],
230231
],
231232
]);
233+
234+
$this->expect('AlternativeQuery', 'object', [
235+
'fields' => [
236+
'planet_sortPlanets' => [
237+
'type' => '[Planet]',
238+
'args' => ['direction' => ['type' => 'String!']],
239+
'resolve' => "@=call(service('Overblog\\\\GraphQLBundle\\\\Tests\\\\Config\\\\Parser\\\\fixtures\\\\annotations\\\\Repository\\\\PlanetRepository').sortPlanets, arguments({direction: \"String!\"}, args))",
240+
'access' => '@=default_access',
241+
'public' => '@=default_public',
242+
],
243+
],
244+
]);
245+
246+
$this->expect('AlternativeMutation', 'object', [
247+
'fields' => [
248+
'planet_destroyPlanet' => [
249+
'type' => 'Planet',
250+
'args' => ['planetInput' => ['type' => 'PlanetInput!']],
251+
'resolve' => "@=call(service('Overblog\\\\GraphQLBundle\\\\Tests\\\\Config\\\\Parser\\\\fixtures\\\\annotations\\\\Repository\\\\PlanetRepository').destroyPlanet, arguments({planetInput: \"PlanetInput!\"}, args))",
252+
'access' => '@=default_access',
253+
'public' => '@=override_public',
254+
],
255+
],
256+
]);
232257
}
233258

234259
public function testFullqualifiedName(): void

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ public function searchPlanet(string $keyword)
2323
return [];
2424
}
2525

26+
/**
27+
* @GQL\Query(type="[Planet]", targetType="AlternativeQuery", args={
28+
* @GQL\Arg(type="String!", name="direction")
29+
* })
30+
*/
31+
public function sortPlanets(string $direction)
32+
{
33+
return [];
34+
}
35+
2636
/**
2737
* @GQL\Mutation(type="Planet", args={
2838
* @GQL\Arg(type="PlanetInput!", name="planetInput")
@@ -34,6 +44,17 @@ public function createPlanet(array $planetInput)
3444
return [];
3545
}
3646

47+
/**
48+
* @GQL\Mutation(type="Planet", targetType="AlternativeMutation", args={
49+
* @GQL\Arg(type="PlanetInput!", name="planetInput")
50+
* })
51+
* @GQL\IsPublic("override_public")
52+
*/
53+
public function destroyPlanet(array $planetInput)
54+
{
55+
return [];
56+
}
57+
3758
/**
3859
* @GQL\Query(type="[Planet]", targetType="Droid", name="allowedPlanets")
3960
* @GQL\Access("override_access")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Tests\Config\Parser\fixtures\annotations\Type;
6+
7+
use Overblog\GraphQLBundle\Annotation as GQL;
8+
9+
/**
10+
* @GQL\Type
11+
*/
12+
class AlternativeMutation
13+
{
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Tests\Config\Parser\fixtures\annotations\Type;
6+
7+
use Overblog\GraphQLBundle\Annotation as GQL;
8+
9+
/**
10+
* @GQL\Type
11+
*/
12+
class AlternativeQuery
13+
{
14+
}

0 commit comments

Comments
 (0)