GraphQL Mutation Error in doctrine OneToOne nested relation with API Platform

GraphQL Mutation Error in doctrine OneToOne nested relation with API Platform


1

I am currently facing an issue with a GraphQL mutation in API Platform and was hoping to find some help here.

Here is the background:

I am working with two entities, Recepe and Picture, which have a OneToOne relationship. In my Recepe entity, the mainPicture field is linked to the Picture entity. Below are the PHP classes defining these entities:

// Recepe.php
<?php

namespace AppEntity;

use ApiPlatformDoctrineOrmFilterSearchFilter;
use ApiPlatformMetadataApiFilter;
use ApiPlatformMetadataApiProperty;
use ApiPlatformMetadataApiResource;
use ApiPlatformMetadataGraphQlMutation;
use ApiPlatformMetadataGraphQlQuery;
use ApiPlatformMetadataGraphQlQueryCollection;
use AppRepositoryRecepeRepository;
use DoctrineORMMapping as ORM;
use SymfonyComponentSerializerAnnotationGroups;
use GedmoTranslatableTranslatable;

#[ORMEntity(repositoryClass: RecepeRepository::class)]
#[ApiResource(
    graphQlOperations: [
        new Query(),
        new QueryCollection(),
        new Mutation(
            normalizationContext: ['groups' => ['recepe:read']],
            denormalizationContext: ['groups' => ['recepe:write']],
            name: 'create',
        ),
        new Mutation(
            normalizationContext: ['groups' => ['recepe:read']],
            denormalizationContext: ['groups' => ['recepe:write']],
            name: 'update',
        )
    ]
)]
#[ApiFilter(SearchFilter::class, properties: ['name' => 'exact'])]
class Recepe implements Translatable
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn]
    #[Groups(['recepe:read'])]
    private ?int $id = null;

    #[ORMColumn(length: 255)]
    #[Groups(['recepe:read', 'recepe:write'])]
    private ?string $name = null;

    #[ORMOneToOne(cascade: ['persist', 'remove'], orphanRemoval: true)]
    #[ORMJoinColumn(nullable: true)]
    #[Groups(['recepe:read', 'recepe:write'])]
    #[ApiProperty(required: false)]
    private ?Picture $mainPicture = null;

    public function __construct()
    {

    }

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

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getMainPicture(): ?Picture
    {
        return $this->mainPicture;
    }

    public function setMainPicture(?Picture $mainPicture): self
    {
        $this->mainPicture = $mainPicture;

        return $this;
    }
}
// Picture.php
<?php

namespace AppEntity;

use ApiPlatformMetadataApiProperty;
use ApiPlatformMetadataApiResource;
use AppRepositoryPictureRepository;
use DoctrineORMMapping as ORM;
use SymfonyComponentSerializerAnnotationGroups;

#[ORMEntity(repositoryClass: PictureRepository::class)]
#[ApiResource]
class Picture
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn]
    private ?int $id = null;

    #[ORMColumn(length: 255, nullable: true)]
    #[Groups(['recepe:read', 'recepe:write'])]
    private ?string $url = null;

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

    public function getUrl(): ?string
    {
        return $this->url;
    }

    public function setUrl(?string $url): self
    {
        $this->url = $url;

        return $this;
    }
}

I have set up normalization and denormalization groups for GraphQL operations as shown in the code snippets above.

I am trying to perform an updateRecepe mutation where I set the mainPicture field to null, but I am facing the following error:

Variable "$input" got invalid value null at "input.mainPicture"; Expected non-nullable type "updatePictureNestedInput!" not to be null.

Here is the mutation query I am running:

mutation updateRecepe($input: updateRecepeInput!) {
  updateRecepe(input: $input) {
  recepe {
    id
    name
    mainPicture {
      url
    }
  }
  }
}

With the variables:

{
  "input": {
    "id": "/recepes/1",
    "mainPicture": null
  }
}

I understand that the error is occurring because the mainPicture field is expecting a non-nullable type, but I don’t understand what is going on in my setup.

I would greatly appreciate any guidance or suggestions on how to resolve this. Has anyone encountered a similar issue or have insights on how to properly handle this kind of mutation in API Platform?

Thank you in advance for your assistance!


Load 6 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *