core icon indicating copy to clipboard operation
core copied to clipboard

No mapping found for field <field> on class <class> on graphql request when adding a getter with the same return type as a doctrine association

Open kstefanini opened this issue 2 years ago • 5 comments

API Platform version(s) affected: 3.2.16

Description
When an entity has a getter for a property with the same type as a relation an error is thrown when the property is request threw graphql.

How to reproduce Set up with last symfony, api, graphql and create some Book and Review entities:

symfony new api-bug --webapp && cd api-bug
composer config extra.symfony.allow-contrib true
echo "DATABASE_URL=sqlite:///%kernel.project_dir%/var/data.db" > .env.local
composer require api
echo "title,string,80,yes," | sed "s/,/\n/g"  | bin/console -a make:entity Book
echo "book,ManyToOne,Book,no,yes,reviews,yes," | sed "s/,/\n/g"  | bin/console -a make:entity Review
bin/console doctrine:schema:update --force --complete
composer require webonyx/graphql-php

Then add this getter to src/Entity/Book.php

    /**
     * @return Collection<int, Review>
     */
    public function getSomeReviews(): Collection
    {
        return $this->reviews;
    }

Push a book to the api

curl -X 'POST' \
  'http://127.0.0.1:8000/api/books' \
  -H 'accept: application/ld+json' \
  -H 'Content-Type: application/ld+json' \
  -d '{
  "title": "Symfony Fast Track"
}'

Then query someReviews property threw graphql to get the error:

{
  books {
    edges {
      node {
        someReviews {
          edges {
            node {
              id
            }
          }
        }
      }
    }
  }
}

"debugMessage": "No mapping found for field 'someReviews' on class 'App\\Entity\\Book'.",

Error log ```json { "errors": [ { "message": "Internal server error", "locations": [ { "line": 5, "column": 9 } ], "path": [ "books", "edges", 0, "node", "someReviews" ], "extensions": { "debugMessage": "No mapping found for field 'someReviews' on class 'App\\Entity\\Book'.", "file": "/home/user/api-bug/vendor/doctrine/orm/src/Mapping/MappingException.php", "line": 121, "trace": [ { "file": "/home/user/api-bug/vendor/doctrine/orm/src/Mapping/ClassMetadata.php", "line": 1073, "call": "Doctrine\\ORM\\Mapping\\MappingException::mappingNotFound()" }, { "file": "/home/user/api-bug/vendor/api-platform/core/src/Doctrine/Orm/State/LinksHandlerTrait.php", "line": 83, "call": "Doctrine\\ORM\\Mapping\\ClassMetadata::getAssociationMapping()" }, { "file": "/home/user/api-bug/vendor/api-platform/core/src/Doctrine/Orm/State/CollectionProvider.php", "line": 67, "call": "ApiPlatform\\Doctrine\\Orm\\State\\CollectionProvider::handleLinks()" }, { "file": "/home/user/api-bug/vendor/api-platform/core/src/State/CallableProvider.php", "line": 43, "call": "ApiPlatform\\Doctrine\\Orm\\State\\CollectionProvider::provide()" }, { "file": "/home/user/api-bug/vendor/api-platform/core/src/GraphQl/State/Provider/ReadProvider.php", "line": 114, "call": "ApiPlatform\\State\\CallableProvider::provide()" }, { "file": "/home/user/api-bug/vendor/api-platform/core/src/Symfony/Security/State/AccessCheckerProvider.php", "line": 53, "call": "ApiPlatform\\GraphQl\\State\\Provider\\ReadProvider::provide()" }, { "file": "/home/user/api-bug/vendor/api-platform/core/src/GraphQl/State/Provider/DenormalizeProvider.php", "line": 38, "call": "ApiPlatform\\Symfony\\Security\\State\\AccessCheckerProvider::provide()" }, { "file": "/home/user/api-bug/vendor/api-platform/core/src/Symfony/Security/State/AccessCheckerProvider.php", "line": 53, "call": "ApiPlatform\\GraphQl\\State\\Provider\\DenormalizeProvider::provide()" }, { "file": "/home/user/api-bug/vendor/api-platform/core/src/Symfony/Validator/State/ValidateProvider.php", "line": 32, "call": "ApiPlatform\\Symfony\\Security\\State\\AccessCheckerProvider::provide()" }, { "file": "/home/user/api-bug/vendor/api-platform/core/src/Symfony/Security/State/AccessCheckerProvider.php", "line": 53, "call": "ApiPlatform\\Symfony\\Validator\\State\\ValidateProvider::provide()" }, { "file": "/home/user/api-bug/vendor/api-platform/core/src/GraphQl/State/Provider/ResolverProvider.php", "line": 36, "call": "ApiPlatform\\Symfony\\Security\\State\\AccessCheckerProvider::provide()" }, { "file": "/home/user/api-bug/vendor/api-platform/core/src/GraphQl/Resolver/Factory/ResolverFactory.php", "line": 58, "call": "ApiPlatform\\GraphQl\\State\\Provider\\ResolverProvider::provide()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 714, "call": "ApiPlatform\\GraphQl\\Resolver\\Factory\\ResolverFactory::ApiPlatform\\GraphQl\\Resolver\\Factory\\{closure}()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 631, "call": "GraphQL\\Executor\\ReferenceExecutor::resolveFieldValueOrError()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 1317, "call": "GraphQL\\Executor\\ReferenceExecutor::resolveField()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 1256, "call": "GraphQL\\Executor\\ReferenceExecutor::executeFields()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 1210, "call": "GraphQL\\Executor\\ReferenceExecutor::collectAndExecuteSubfields()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 893, "call": "GraphQL\\Executor\\ReferenceExecutor::completeObjectValue()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 753, "call": "GraphQL\\Executor\\ReferenceExecutor::completeValue()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 640, "call": "GraphQL\\Executor\\ReferenceExecutor::completeValueCatchingError()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 1317, "call": "GraphQL\\Executor\\ReferenceExecutor::resolveField()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 1256, "call": "GraphQL\\Executor\\ReferenceExecutor::executeFields()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 1210, "call": "GraphQL\\Executor\\ReferenceExecutor::collectAndExecuteSubfields()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 893, "call": "GraphQL\\Executor\\ReferenceExecutor::completeObjectValue()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 753, "call": "GraphQL\\Executor\\ReferenceExecutor::completeValue()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 985, "call": "GraphQL\\Executor\\ReferenceExecutor::completeValueCatchingError()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 871, "call": "GraphQL\\Executor\\ReferenceExecutor::completeListValue()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 753, "call": "GraphQL\\Executor\\ReferenceExecutor::completeValue()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 640, "call": "GraphQL\\Executor\\ReferenceExecutor::completeValueCatchingError()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 1317, "call": "GraphQL\\Executor\\ReferenceExecutor::resolveField()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 1256, "call": "GraphQL\\Executor\\ReferenceExecutor::executeFields()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 1210, "call": "GraphQL\\Executor\\ReferenceExecutor::collectAndExecuteSubfields()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 893, "call": "GraphQL\\Executor\\ReferenceExecutor::completeObjectValue()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 753, "call": "GraphQL\\Executor\\ReferenceExecutor::completeValue()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 640, "call": "GraphQL\\Executor\\ReferenceExecutor::completeValueCatchingError()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 1317, "call": "GraphQL\\Executor\\ReferenceExecutor::resolveField()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 298, "call": "GraphQL\\Executor\\ReferenceExecutor::executeFields()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php", "line": 237, "call": "GraphQL\\Executor\\ReferenceExecutor::executeOperation()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/Executor/Executor.php", "line": 159, "call": "GraphQL\\Executor\\ReferenceExecutor::doExecute()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/GraphQL.php", "line": 162, "call": "GraphQL\\Executor\\Executor::promiseToExecute()" }, { "file": "/home/user/api-bug/vendor/webonyx/graphql-php/src/GraphQL.php", "line": 96, "call": "GraphQL\\GraphQL::promiseToExecute()" }, { "file": "/home/user/api-bug/vendor/api-platform/core/src/GraphQl/Executor.php", "line": 43, "call": "GraphQL\\GraphQL::executeQuery()" }, { "file": "/home/user/api-bug/vendor/api-platform/core/src/GraphQl/Action/EntrypointAction.php", "line": 79, "call": "ApiPlatform\\GraphQl\\Executor::executeQuery()" }, { "file": "/home/user/api-bug/vendor/symfony/http-kernel/HttpKernel.php", "line": 178, "call": "ApiPlatform\\GraphQl\\Action\\EntrypointAction::__invoke()" }, { "file": "/home/user/api-bug/vendor/symfony/http-kernel/HttpKernel.php", "line": 76, "call": "Symfony\\Component\\HttpKernel\\HttpKernel::handleRaw()" }, { "file": "/home/user/api-bug/vendor/symfony/http-kernel/Kernel.php", "line": 185, "call": "Symfony\\Component\\HttpKernel\\HttpKernel::handle()" }, { "file": "/home/user/api-bug/vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php", "line": 35, "call": "Symfony\\Component\\HttpKernel\\Kernel::handle()" }, { "file": "/home/user/api-bug/vendor/autoload_runtime.php", "line": 29, "call": "Symfony\\Component\\Runtime\\Runner\\Symfony\\HttpKernelRunner::run()" }, { "file": "/home/user/api-bug/public/index.php", "line": 5, "function": "require_once('/home/user/api-bug/vendor/autoload_runtime.php')" } ] } } ], "data": { "books": { "edges": [ { "node": { "someReviews": null } } ] } } } ```

Possible Solution
Maybe the fix from #5200 doesn't work anymore?

Additional Context

kstefanini avatar Mar 07 '24 08:03 kstefanini

https://github.com/api-platform/core/commit/f891f16d50b665f25e4699291526161e16ce0fa7 I guess this is why, we need to catch that exception or just check for the existence of the association

soyuka avatar Mar 07 '24 09:03 soyuka

Not a bug, we can't do aliases like this with doctrine or you need to create your own link handler.

soyuka avatar Mar 07 '24 10:03 soyuka

OK thanks for the update. Do you have some resources about links handling logic? I'm not sure about what to do here.

kstefanini avatar Mar 07 '24 11:03 kstefanini

SerializedName probably, best is to have a DTO representing the data you want on your API and to fetch the entity in a provider.

soyuka avatar Mar 15 '24 09:03 soyuka

@soyuka Can you provide an example? As I can see, handleLinks is called from within a provider, so you can't modify the provider output in any way, and that output overrides the calculated property. It should check for Doctrine mappings before calling the provider, and let the property just return calculated result like scalar ones do.

AlgronovM avatar Apr 12 '24 15:04 AlgronovM