question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Graphql & DataTransformer with Output Entity "No resource class found for object of type [...]"

See original GitHub issue

Hi there!

(using the current 2.4 master)

I have an Entity Class that I want to transform before I return it to the user:

/**
 * @ApiResource(
 *     attributes={"_access_control"="is_granted('ROLE_USER')"},
 *     output=LanguageOutput::class
 * )
 * @ORM\Entity(repositoryClass="App\Repository\LanguageRepository")
 * @UniqueEntity("code")
 */
class Language
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ApiProperty(identifier=false)
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     * @ApiProperty(identifier=true)
     * @Assert\NotBlank()
     */
    private $code;

   // [...]
}

I created a DataTransformer for it:

final class LanguageOutputTransformer implements DataTransformerInterface
{
    /**
     * {@inheritdoc}
     */
    public function transform($data, string $to, array $context = [])
    {
        $parameters = GraphqlQueryParameter::getParametersFromRequest($this->requestStack->getCurrentRequest());

        /** @var Language $data */
        $output = new LanguageOutput(
            $data->getId(),
            $data->getCode(),
            'en'
        );
        return $output;
    }

    /**
     * {@inheritdoc}
     */
    public function supportsTransformation($data, string $to, array $context = []): bool
    {
        return LanguageOutput::class === $to && $data instanceof Language;
    }
}

and created my Output class:

class LanguageOutput
{
    private $id;
    private $in_locale = 'en';
    private $code;

    public function __construct($id, string $code, ?string $in_locale)
    {
        $this->id   = $id;
        $this->code = $code;
        if ($in_locale) {
            $this->in_locale = $in_locale;
        }
    }


    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @ApiProperty(identifier=true)
     */
    public function getCode(): ?string
    {
        return $this->code;
    }
}

using this with accept: application/json works just fine - but if I want to query it using graphql, I get

{
  "errors": [
    {
      "debugMessage": "No resource class found for object of type \"App\\Entity\\Dto\\LanguageOutput\".",
      "message": "Internal server error",
      "extensions": {
        "category": "internal"
      },
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "languages"
      ],
      "trace": [
        {
          "file": "/app/src/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php",
          "line": 126,
          "call": "ApiPlatform\\Core\\Api\\ResourceClassResolver::getResourceClass(instance of App\\Entity\\Dto\\LanguageOutput, 'App\\Entity\\Dto\\LanguageOutput', true)"
        },
        {
          "file": "/app/src/vendor/api-platform/core/src/Serializer/ItemNormalizer.php",
          "line": 52,
          "call": "ApiPlatform\\Core\\Serializer\\AbstractItemNormalizer::normalize(instance of App\\Entity\\Dto\\LanguageOutput, 'graphql', array(3))"
        },
        {
          "file": "/app/src/vendor/api-platform/core/src/GraphQl/Serializer/ItemNormalizer.php",
          "line": 43,
          "call": "ApiPlatform\\Core\\Serializer\\ItemNormalizer::normalize(instance of App\\Entity\\Dto\\LanguageOutput, 'graphql', array(3))"
        },
[...]

So I tried annotating my LanguageOutput class with @ApiResource, but now graphql returns items of the id /api/language_outputs/[id] - which again cannot be used as id for query or mutation actions, because it refers to a different entity.

Also, I have a set of additional REST endpoints now that I don’t really want.

Is there a correct way to use graphql with a DataTransformerInterface?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
soyukacommented, Mar 13, 2019

I’ve patched this but won’t be able to propose a proper patch before tomorrow, until then here’s the diff https://github.com/api-platform/core/pull/2615/commits/b391c97f5f02d6e77e7e3b4e9dcf9c2642db2b38

1reaction
soyukacommented, Mar 13, 2019

Hi, don’t declare an @ApiResource on the LanguageOutput it’s wrong.

I’ve reproduced your problem and am looking into it 😃.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bad Performance when using a output DTO with doctrine ...
The DataTransformer's method transform puts the data into the DTO by using therespective getters of the entities, e. g.: $output = new ...
Read more >
Using Data Transfer Objects (DTOs) - API Platform
Implementing a Write Operation With an Input Different From the Resource. Using an input, the request body will be denormalized to the input...
Read more >
Completely Custom Resource > API Platform Part 3
Even though they have some custom fields, both of our API resources are truly bound to ... We could have created a totally...
Read more >
Building GraphQL APIs with TypeGraphQL and TypeORM
js before, chances are, you define the schema for your object types, mutations, and queries in GraphQL schema language and the resolvers in ......
Read more >
Execution - GraphQL Java
Execution. Queries​. To execute a query against a schema, build a new GraphQL object with the appropriate arguments and then call execute() ....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found