api-platform: GraphQL: @ApiProperty(required=false) does not work
In my Entity, I have a property:
/**
* @var int The account balance
*
* @ORM\Column(type="integer")
* @ApiProperty(required=false)
* @Assert\Type(type="integer")
*/
private $balance = 0;
I want this property to be optional in my GraphQL query, but not in database.
So I added the required=false attributes, but it doesn’t work.
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 4
- Comments: 15 (2 by maintainers)
So this is still an issue? Is there at least a workaround? For example I am trying to add a property to an entity that does not need to persist to the db at all. But it’s still a required field for graphql. At least when creating. when updating this does not seam to be a problem (since the values already exist and just get rehydrated into the entity already).
I am using a boolean with a default, which also should be a case where the GraphQL input is not required.
In the docs at https://api-platform.com/docs/core/graphql/#securing-properties-including-associations a note says:
So a workaround may be to add some dummy security via ApiProperty-Annotation to the property which would make it optional in GraphQL:
I had a look and it seems this forces the nullable field via
$forceNullableparam at https://github.com/api-platform/core/blob/v3.0.9/src/GraphQl/Type/FieldsBuilder.php#L250This method is called a little bit up at https://github.com/api-platform/core/blob/v3.0.9/src/GraphQl/Type/FieldsBuilder.php#L216
So maybe just passing
null !== $propertyMetadata->getSecurity() || null !== $propertyMetadata->getDefault()instead of justnull !== $propertyMetadata->getSecurity()would already be enough but needs to be further investigated …+1
+1
while
@ORM\Column(nullable=true)sets the default for a field being nullable (or not) it should be configurable as well.