Skip to content

Commit 4cd359d

Browse files
authored
fix(graphql): resolver before validation (#6363)
fixes #6354
1 parent 20c9165 commit 4cd359d

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

features/graphql/mutation.feature

+17
Original file line numberDiff line numberDiff line change
@@ -1034,3 +1034,20 @@ Feature: GraphQL mutation support
10341034
"""
10351035
Then the response status code should be 200
10361036
And the JSON node "data.uploadMultipleMediaObject.mediaObject.contentUrl" should be equal to "test.gif"
1037+
1038+
@!mongodb
1039+
Scenario: Mutation should run before validation
1040+
When I send the following GraphQL request:
1041+
"""
1042+
mutation {
1043+
createActivityLog(input: {name: ""}) {
1044+
activityLog {
1045+
name
1046+
}
1047+
}
1048+
}
1049+
"""
1050+
Then the response status code should be 200
1051+
And the response should be in JSON
1052+
And the header "Content-Type" should be equal to "application/json"
1053+
And the JSON node "data.createActivityLog.activityLog.name" should be equal to "hi"

src/Symfony/Bundle/Resources/config/graphql.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@
158158
<argument type="tagged_locator" tag="api_platform.parameter_provider" index-by="key" />
159159
</service>
160160

161-
<!-- Validation occurs at 200, we want validation to be over when we reach custom resolvers -->
162-
<service id="api_platform.graphql.state_provider.resolver" class="ApiPlatform\GraphQl\State\Provider\ResolverProvider" decorates="api_platform.graphql.state_provider" decoration-priority="190">
161+
<!-- Validation occurs at 200, we want validation to be after we reach custom resolvers -->
162+
<service id="api_platform.graphql.state_provider.resolver" class="ApiPlatform\GraphQl\State\Provider\ResolverProvider" decorates="api_platform.graphql.state_provider" decoration-priority="220">
163163
<argument type="service" id="api_platform.graphql.state_provider.resolver.inner" />
164164
<argument type="service" id="api_platform.graphql.resolver_locator" />
165165
</service>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6354;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\GraphQl\Mutation;
18+
use Symfony\Component\Validator\Constraints\NotBlank;
19+
20+
#[ApiResource(
21+
graphQlOperations: [
22+
new Mutation(
23+
resolver: 'app.graphql.mutation_resolver.activity_log',
24+
name: 'create'
25+
),
26+
]
27+
)]
28+
class ActivityLog
29+
{
30+
public function __construct(#[NotBlank()] public ?string $name = null)
31+
{
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6354;
15+
16+
use ApiPlatform\GraphQl\Resolver\MutationResolverInterface;
17+
18+
class CreateActivityLogResolver implements MutationResolverInterface
19+
{
20+
/**
21+
* @param object|null $item
22+
* @param mixed[] $context
23+
*/
24+
public function __invoke($item, array $context): ActivityLog
25+
{
26+
if (!$item instanceof ActivityLog) {
27+
throw new \InvalidArgumentException('Missing input of type ActivityLog');
28+
}
29+
30+
$item->name = 'hi';
31+
32+
return $item;
33+
}
34+
}

tests/Fixtures/app/config/config_common.yml

+5
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,8 @@ services:
455455
tags:
456456
- name: 'api_platform.parameter_provider'
457457
key: 'ApiPlatform\Tests\Fixtures\TestBundle\Parameter\CustomGroupParameterProvider'
458+
459+
app.graphql.mutation_resolver.activity_log:
460+
class: 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6354\CreateActivityLogResolver'
461+
tags:
462+
- name: 'api_platform.graphql.resolver'

0 commit comments

Comments
 (0)