Skip to content

Mixing REST and GraphQL operation names #6421

@danaki

Description

@danaki

API Platform version: 3,3

My resource description looks like so:

#[ApiResource(
    security: "is_granted('ROLE_USER')",
    order: ['id' => 'DESC'],
    normalizationContext: ['groups' => ["batch:query"]],
    denormalizationContext: ['groups' => ["batch:mutation"]],
    operations: [
        new Get(),
        new GetCollection(),
        new Post(
            name: 'create',
            controller: CreateBatchController::class,
        ),
        new Post(
            name: 'start',
            controller: BatchTransitionStartController::class,
            uriTemplate: '/batches/{id}/start',
            denormalizationContext: ['groups' => ["batch:transition"]]
        ),
        new Post(
            name: 'finish',
            controller: BatchTransitionFinishController::class,
            uriTemplate: '/batches/{id}/finish',
            denormalizationContext: ['groups' => ["batch:transition"]]
        ),
        new Post(
            name: 'pause',
            controller: BatchTransitionPauseController::class,
            uriTemplate: '/batches/{id}/pause',
            denormalizationContext: ['groups' => ["batch:transition"]]
        )
    ],
    graphQlOperations: [
        new Query(),
        new QueryCollection(),
        new Mutation(
            name: 'create',
            resolver: CreateBatchMutationResolver::class
        ),
        new Mutation(
            name: 'start',
            resolver: BatchTransitionMutationResolver::class,
            denormalizationContext: ['groups' => ["batch:transition"]]
        ),
        new Mutation(
            name: 'finish',
            resolver: BatchTransitionMutationResolver::class,
            denormalizationContext: ['groups' => ["batch:transition"]]
        ),
        new Mutation(
            name: 'pause',
            resolver: BatchTransitionMutationResolver::class,
            denormalizationContext: ['groups' => ["batch:transition"]]
        ),
    ]
)]

My goal is to reproduce the same API for REST and GraphQL endpoints. Having this getting strange server error in

vendor\/api-platform\/core\/src\/GraphQl\/Type\/TypeConverter.php","line":185

after some digging I see exception firing:

        if (!$operation instanceof Operation) {
            throw new OperationNotFoundException();
        }

following

$operation = $resourceMetadataCollection->getOperation($operationName);
``

FIX. In my case, for example:
    new Post(
        name: 'start',
        controller: BatchTransitionStartController::class,
        uriTemplate: '/batches/{id}/start',
    ),
and
    new Mutation(
        name: 'start',
        resolver: BatchTransitionMutationResolver::class
    ),

both have "start" as a name, while I can't change the name for Mutation since it will be reflected in autogenerated GraphQL operation naming thus breaking API for existing users, It's safe to name the Post to let's say "start_rest" providing uriTemplate and error is gone. It's either a bug or documentation issue leading to cryptic error messages.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions