thecodingmachine / graphqlite

Use PHP Attributes/Annotations to declare your GraphQL API

Home Page:https://graphqlite.thecodingmachine.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Field nullable union type is throwing an error

oprypkhantc opened this issue · comments

Having a type like this:

#[Type]
class File {
	#[Field(name: 'sizes')]
	public function sizes(): ImageSizes|VideoSizes|null {}
}

throws an error saying null can't be converted to a GraphQL type. GraphQL spec does allow nullable unions, so this seems to be a bug with graphqlite.

For the above function phpDocumentor reports the following type:

new Nullable(
	realType: new Compound(
		types: [
			new Object_(),
			new Object_(),
			new Null_(),
		]
	)
)

so when NullableTypeMapperAdapter attempts to get a non-nullable version of that it goes through https://github.com/thecodingmachine/graphqlite/blob/master/src/Mappers/Root/NullableTypeMapperAdapter.php#L129
and returns the Compound type instance with Null_ being one of the types.

Fix should be trivial: return return $this->getNonNullable($type->getActualType()) instead of return $type->getActualType()

I can PR this change.

Seems like a bug - yea. Since union types are a newer PHP feature, that was never fully incorporated. A PR would be great, just need to make sure we have test coverage.