I have a problem with lighthouse graphql
on laravel
.
Everything works great with queries or simple create/update, but when it comes to relations it becomes hard.
It doesn't save relation.
My example:
Models
class Product extends Model
{
public $timestamps = false;
protected $table = 'products';
protected $primaryKey = 'id_product';
public function manufacturer(): HasOne
{
return $this->hasOne(Manufacturer::class, 'id_product_manufacturer', 'id_product_manufacturer');
}
}
class Manufacturer extends Model
{
protected $table = 'product_manufacturer';
protected $primaryKey = 'id_product_manufacturer';
protected $fillable = ['name'];
public function products(): BelongsTo
{
return $this->belongsTo(Product::class, 'id_product_manufacturer', 'id_product_manufacturer');
}
}
sql schema
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id_product');
$table->bigInteger('id_product_manufacturer')->unsigned()->index()->nullable();
});
Schema::create('product_manufacturer', function (Blueprint $table) {
$table->bigIncrements('id_product_manufacturer');
$table->string('name', 64);
});
graphql file
extend type Mutation {
createProduct(
input: CreateProduct! @spread
): Product @create
}
input CreateProduct {
manufacturer: CreateProductManufacturerRel
}
input CreateProductManufacturerRel {
connect: ID
create: CreateProductManufacturer
}
input CreateProductManufacturer{
name: String!
}
query sended to server
mutation {
createProduct(
input: {
manufacturer: {
create: {
name: "test"
}
}
}
){
id_product
}
}
This query create new product and manufacturer but it doesn't save id_product_manufacturer
into products
table.
What i have already tried:
https://lighthouse-php.com/master/eloquent/nested-mutations.html#return-types-required
and
https://lighthouse-php.com/master/concepts/arg-resolvers.html#solution
but i don't know where and how create this resolver function
.
These examples do not explain much.