[GraphQL] Cannot convert to a nullable type in default create mutation
Hi everyone,
Context:
- Symfony 6
- Api-Platform 2.6 with GraphQL
I implemented a custom type converter (that implement the TypeConverterInterface) where I customize the type of a specific property. The problem I face is on the default "create" mutation: I cannot set my input property as nullable.
I'm creating a list of my specific type like so:
// The method to implement to customize types
public function convertType(...) {
// Customize the type of a specific property to "[myType]"
if(...) {
$type = $this->typesContainer->get("myType");
return GraphQLType::listOf(GraphQLType::getNullableType($type)));
}
return $this->defaultTypeConverter->convertType(...);
}
Since GraphQLType::listOf(...) creates a ListOfType object that implements the NullableType interface, I expect the type on the Schema to be: [myType]. But the type I get is [myType]!, forcing the input property to be filled.
I figured out the problem comes from the FieldsBuilder that forces any class that implement the NullableType to be NonNullable (notice the the "!" that invert the check of the instance):
https://github.com/api-platform/core/blob/4fe0821fb1b4760606b739040aedd862dd214087/src/Core/GraphQl/Type/FieldsBuilder.php#L514-L516
I can understand the idea behind having required input property, but it would be interesting as well to have them optional (with default value), especially if the type is manually set on the TypeConverter.
Is that a mistake or maybe a feature I didn't understand ?