Skip to content

[GraphQL] Cannot convert to a nullable type in default create mutationΒ #4797

@Dev-TeamGS

Description

@Dev-TeamGS

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):

return $forceNullable || !$graphqlType instanceof NullableType || $type->isNullable() || (null !== $mutationName && 'update' === $mutationName)
? $graphqlType
: GraphQLType::nonNull($graphqlType);

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions