How can I specify a default value for a required field with API Platform GraphQL?

Viewed 544

I would like a required boolean field on my entity to have a default value of TRUE. I have defined my #api-platform attribute as follows.

#[ApiResource(
    attributes: ['security' => "is_granted('ROLE_USER')"],
    graphql: [
        'item_query' => ['security' => "is_granted('ROLE_USER') and object.owner == user"],
        'collection_query' => ['security' => "is_granted('ROLE_ADMIN')"],
        'delete' => ['security' => "is_granted('ROLE_DEVELOPER')"],
        'update' => ['security' => "is_granted('ROLE_DEVELOPER')"],
        'create' => [
            'defaults' => ['active' => TRUE],
            'security' => "is_granted('ROLE_DEVELOPER')"
        ]
    ]
)]

And the active field itself is annotated with

    /**
     * @ORM\Column(type="boolean", options={"default":true})
     */
    private $active;

This does set a default value in the database schema, however the GraphQL mutation still seems to be requiring that this property be provided in the request. I recieve the following error when trying to create a new entity w/out specifying the active prop.

... Field value.active of required type Boolean! was not provided.
1 Answers

Should setting a default value not look like this.

    /**
     * @ORM\Column(type="boolean", options={"default":true})
     */
    private bool $active = true;
Related