api-platform / api-platform/core

[GraphQL] Iterable Type is required for mutations

Open
#1,944 1 comment 1 reaction 0 assignees View on GitHub
GraphQL
Dominant language
PHP
Stars
2.6k
Forks
980
Avg merge
2d 5h
Merged PRs (30d)
48

Description

When the schema is generated for an array field, it seems like it always lists it as required when the setting has the default value set to an empty array.

For example, the following model will cause the `privileges` field to always be required, even though it should not be.

```php
class User {
/**
* @ORM\Column(type="text_array", nullable=true)
*
* @Groups({"read", "write"})
*/
private $privileges = [];

public function getPrivileges(): ?array
{
return $this->privileges;
}

public function setPrivileges(array $privileges = []): void
{
$this->privileges = array_unique($privileges);
}
}
```

The only way I have found to make it optional like it should be is to modify the setter to this:

```php
public function setPrivileges(array $privileges = null): void
{
$this->privileges = array_unique($privileges ?? []);
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.