core icon indicating copy to clipboard operation
core copied to clipboard

GraphQL: Field id not defined if update mutation has resolver configured

Open durimjusaj opened this issue 1 year ago • 3 comments

API Platform version(s) affected: 3.2.11

Description
After api-platform/core update from 3.1.16 to 3.2.11, graphql update mutation with resolver, can not find id property.

How to reproduce
Contact.php -> Entity

#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(name: 'contacts_id', type: "integer")]
private int $id;

We use ApiResource attribute in Entity

#[ApiResource(
graphQlOperations: [
     new Mutation(
        resolver: ContactResolver::class,
        security: "is_granted('ContactUpdate')",
        name: "update",
        processor: ContactProcessor::class
    )
  ]
)

GraphQL mutation query

mutation {
  updateContact(input: { id: "api/contacts/1" }) {
    contact {
      id
    }
  }
}

I got this error in all update mutation query which have resolver configured in ApiResource operation

{
  "errors": [
    {
      "message": "Field \"id\" is not defined by type \"updateContactInput\".",
      "locations": [
        {
          "line": 2,
          "column": 25
        }
      ],
      "extensions": {
        "file": "/var/www/html/vendor/webonyx/graphql-php/src/Validator/Rules/ValuesOfCorrectType.php",
        "line": 117
      }
    }
  ]
}

durimjusaj avatar Jan 17 '24 10:01 durimjusaj

not sure how to fix this it's quite hard, to you really need the resolver? can't you use a provider instead?

soyuka avatar Jan 17 '24 14:01 soyuka

I had pretty much the same problem after upgrading apiPlatform from 3.1 to 3.2.

I think the change originated here: https://github.com/api-platform/core/pull/5095 the introduced breaking change got kind of fixed here: https://github.com/api-platform/core/pull/5359

so the solution seems to be to add extraArgs to new Mutation. In the example of @durimjusaj :

#[ApiResource(
graphQlOperations: [
     new Mutation(
        resolver: ContactResolver::class,
        
        // add the following line with api platform 3.2 upgrade:
        extraArgs: ['id' => ['type' => 'ID!']],
        
        security: "is_granted('ContactUpdate')",
        name: "update",
        processor: ContactProcessor::class
    )
  ]
)

tft7000 avatar Jan 21 '24 21:01 tft7000

@tft7000 yes, I just tested this real quick with extraArgs and is working. Thank you.

durimjusaj avatar Jan 21 '24 21:01 durimjusaj