core icon indicating copy to clipboard operation
core copied to clipboard

Resolvers are not run on the default deletion mutation

Open Wykaz opened this issue 2 years ago • 6 comments

API Platform version(s) affected: 3.1.3

Description With GraphQL when we try to add a resolver on the delete mutation it doesn't work. It is not executed.

How to reproduce

#[ApiResource(
    graphQlOperations: [
        new Mutation(
            resolver: CreateResolver::class,
            args: [
                'test' => [
                    'type' => 'String!',
                ]
            ],
            name: 'create'
        ),
        new DeleteMutation(
            resolver: DeleteResolver::class,
            args: [
                'id' => [
                    'type' => 'ID!',
                ],
            ],
            name: 'delete',
        )
    ]
)]

Possible Solution

In src/GraphQl/Resolver/Factory/ItemMutationResolverFactory.php If the operation is "delete" the resolver are not executed because the resolver are executed after.

if ('delete' === $operation->getName() || $operation instanceof DeleteOperationInterface) {
    ($this->securityPostDenormalizeStage)($resourceClass, $operation, $resolverContext + [
        'extra_variables' => [
            'object' => $item,
            'previous_object' => $previousItem,
        ],
    ]);
    $item = ($this->writeStage)($item, $resourceClass, $operation, $resolverContext);

    return ($this->serializeStage)($item, $resourceClass, $operation, $resolverContext);
}

$item = ($this->deserializeStage)($item, $resourceClass, $operation, $resolverContext);

$mutationResolverId = $operation->getResolver();
if (null !== $mutationResolverId) {
    /** @var MutationResolverInterface $mutationResolver */
    $mutationResolver = $this->mutationResolverLocator->get($mutationResolverId);
    $item = $mutationResolver($item, $resolverContext);
    if (null !== $item && $resourceClass !== $itemClass = $this->getObjectClass($item)) {
        throw new \LogicException(sprintf('Custom mutation resolver "%s" has to return an item of class %s but returned an item of class %s.', $mutationResolverId, $operation->getShortName(), (new \ReflectionClass($itemClass))->getShortName()));
    }
}

Wykaz avatar Feb 28 '23 15:02 Wykaz

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 29 '23 16:04 stale[bot]

Still buggy in 3.1.10

Wykaz avatar May 01 '23 21:05 Wykaz

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 30 '23 22:06 stale[bot]

A fix is coming in 3.2

soyuka avatar Jul 01 '23 18:07 soyuka

Any news on this ?

cuberinooo avatar Aug 25 '23 10:08 cuberinooo

@cuberinooo I updated from v.3.2.19 to v.3.3.9 and it seems to work now.

HasBert avatar Jul 15 '24 15:07 HasBert