Skip to content

Commit

Permalink
fix(graphql): resolver before validation (#6363)
Browse files Browse the repository at this point in the history
fixes #6354
  • Loading branch information
soyuka committed May 10, 2024
1 parent 20c9165 commit 4cd359d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
17 changes: 17 additions & 0 deletions features/graphql/mutation.feature
Expand Up @@ -1034,3 +1034,20 @@ Feature: GraphQL mutation support
"""
Then the response status code should be 200
And the JSON node "data.uploadMultipleMediaObject.mediaObject.contentUrl" should be equal to "test.gif"

@!mongodb
Scenario: Mutation should run before validation
When I send the following GraphQL request:
"""
mutation {
createActivityLog(input: {name: ""}) {
activityLog {
name
}
}
}
"""
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/json"
And the JSON node "data.createActivityLog.activityLog.name" should be equal to "hi"
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/Resources/config/graphql.xml
Expand Up @@ -158,8 +158,8 @@
<argument type="tagged_locator" tag="api_platform.parameter_provider" index-by="key" />
</service>

<!-- Validation occurs at 200, we want validation to be over when we reach custom resolvers -->
<service id="api_platform.graphql.state_provider.resolver" class="ApiPlatform\GraphQl\State\Provider\ResolverProvider" decorates="api_platform.graphql.state_provider" decoration-priority="190">
<!-- Validation occurs at 200, we want validation to be after we reach custom resolvers -->
<service id="api_platform.graphql.state_provider.resolver" class="ApiPlatform\GraphQl\State\Provider\ResolverProvider" decorates="api_platform.graphql.state_provider" decoration-priority="220">
<argument type="service" id="api_platform.graphql.state_provider.resolver.inner" />
<argument type="service" id="api_platform.graphql.resolver_locator" />
</service>
Expand Down
33 changes: 33 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Issue6354/ActivityLog.php
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6354;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GraphQl\Mutation;
use Symfony\Component\Validator\Constraints\NotBlank;

#[ApiResource(
graphQlOperations: [
new Mutation(
resolver: 'app.graphql.mutation_resolver.activity_log',
name: 'create'
),
]
)]
class ActivityLog
{
public function __construct(#[NotBlank()] public ?string $name = null)
{
}
}
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6354;

use ApiPlatform\GraphQl\Resolver\MutationResolverInterface;

class CreateActivityLogResolver implements MutationResolverInterface
{
/**
* @param object|null $item
* @param mixed[] $context
*/
public function __invoke($item, array $context): ActivityLog
{
if (!$item instanceof ActivityLog) {
throw new \InvalidArgumentException('Missing input of type ActivityLog');
}

$item->name = 'hi';

return $item;
}
}
5 changes: 5 additions & 0 deletions tests/Fixtures/app/config/config_common.yml
Expand Up @@ -455,3 +455,8 @@ services:
tags:
- name: 'api_platform.parameter_provider'
key: 'ApiPlatform\Tests\Fixtures\TestBundle\Parameter\CustomGroupParameterProvider'

app.graphql.mutation_resolver.activity_log:
class: 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6354\CreateActivityLogResolver'
tags:
- name: 'api_platform.graphql.resolver'

0 comments on commit 4cd359d

Please sign in to comment.