Skip to content

Multiple ManyToMany on same class gives inconsistent results with GraphQL #5256

@remyber

Description

@remyber

API Platform version(s) affected: 3.0.6

Description
In an entity, I create several ManyToMany relations pointing to the same class. When retrieving data via a GraphQL query, all relations display the same results.

How to reproduce
Here is a minimalist entity affected by the bug:

#[ORM\Table(name: 'tests')]
#[ORM\Entity]
#[ApiResource(
    graphQlOperations: [new QueryCollection()]
)]
class Test
{
    #[ApiProperty(identifier: false)]
    #[ORM\Column(name: 'id', type: 'integer')]
    #[ORM\Id]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    private ?int $id = null;

    #[ApiProperty(identifier: true)]
    #[ORM\Column(type: 'uuid', unique: true)]
    private UuidInterface $uuid;

    #[ORM\JoinTable(name: 'tests_users1')]
    #[ORM\ManyToMany(targetEntity: User::class)]
    private iterable $users1;

    #[ORM\JoinTable(name: 'tests_users2')]
    #[ORM\ManyToMany(targetEntity: User::class)]
    private iterable $users2;

    public function __construct()
    {
        $this->uuid = Uuid::uuid4();

        $this->users1 = new ArrayCollection();
        $this->users2 = new ArrayCollection();
    }

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

    public function getUuid(): UuidInterface
    {
        return $this->uuid;
    }

    public function getUsers1(): iterable
    {
        return $this->users1;
    }

    public function getUsers2(): iterable
    {
        return $this->users2;
    }

To test, I added an entry in tests_users2. tests_users1 is empty.
Here is the GraphQL query:

{
  tests {
    id
    users1 {
      collection {
        username
      }
    }
    users2 {
      collection {
        username
      }
    }
  }
}

And the results. The 2 relations show the same results, while users1 should be empty.

{
  "data": {
    "tests": [
      {
        "id": "/api/tests/92aaea46-def9-4cb6-84ee-ef8954c4a894",
        "users1": {
          "collection": [
            {
              "username": "test51"
            }
          ]
        },
        "users2": {
          "collection": [
            {
              "username": "test51"
            }
          ]
        }
      }
    ]
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions