graphqlite
graphqlite copied to clipboard
Allow @UseInputType on Factories
Consider the following situation - no idea how to handle this without this annotation. Any ideas or alternate approaches?
The goal here is to not have to create separate input type classes for Dimensions where there is already a good domain model class that can be used.
class ProductController
{
public function createProduct(CreateProduct $input) { // stuff }
public function updateProduct(UpdateProduct $input) { // stuff }
}
class CreateProduct
{
/**
* Factory to make a CreateProductInput type
*
* @GraphQLite\Factory()
* @GraphQLite\UseInputType(for="$dimensions", inputType="CreateDimensionsInput")
*/
public static function make(
Money $price,
float $weight,
Dimensions $dimensions,
...
): CreateProduct
{
$product = new self();
$product->setPrice($price);
$product->setWeight($weight);
...
return $product;
}
}
class UpdateProduct
{
/**
* Factory to make a UpdateProductInput type
*
* @GraphQLite\Factory()
* @GraphQLite\UseInputType(for="$dimensions", inputType="UpdateDimensionsInput")
*/
public static function make(
Money $price,
float $weight,
Dimensions $dimensions,
...
): UpdateProduct
{
$product = new self();
$product->setPrice($price);
$product->setWeight($weight);
...
return $product;
}
}
class CreateDimensions
{
/**
* Factory to make a CreateDimensionsInput type
*
* @GraphQLite\Factory(name="CreateDimensionsInput", default=false)
*/
public static function make(
float $height,
float $length,
float $width,
...
): Dimensions
{
$dimensions = new Dimensions();
$dimensions->setHeight($height);
$dimensions->setLength($length);
$dimensions->setWidth($width);
...
return $dimensions;
}
}
class UpdateDimensions
{
/**
* Factory to make a UpdateDimensionsInput type
*
* @GraphQLite\Factory(name="UpdateDimensionsInput", default=false)
*/
public static function make(
ID, $id,
float $height,
float $length,
float $width,
...
): Dimensions
{
$dimensions = $this->get('dimensions_repository')->findOrDie($id);
$dimensions->setHeight($height);
$dimensions->setLength($length);
$dimensions->setWidth($width);
...
return $dimensions;
}
}
Hey @oojacoboo ,
@UseInputType should definitely be usable in factories and your example should work.
Did you face an issue trying to do this?
@moufmouf Yea, it didn't work and the docs say that annotation isn't supported on factories...
Ok, I'll check this.