【发布时间】:2021-10-13 04:30:18
【问题描述】:
我对@987654324@ 上的lighthouse graphql 有疑问。
查询或简单的创建/更新一切都很好,但是当涉及到关系时就变得困难了。
它不保存关系。
我的例子:
模型
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::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 文件
extend type Mutation {
createProduct(
input: CreateProduct! @spread
): Product @create
}
input CreateProduct {
manufacturer: CreateProductManufacturerRel
}
input CreateProductManufacturerRel {
connect: ID
create: CreateProductManufacturer
}
input CreateProductManufacturer{
name: String!
}
查询发送到服务器
mutation {
createProduct(
input: {
manufacturer: {
create: {
name: "test"
}
}
}
){
id_product
}
}
此查询创建新产品和制造商,但不保存id_product_manufacturer
进入products 表。
我已经尝试过的:
https://lighthouse-php.com/master/eloquent/nested-mutations.html#return-types-required
和
https://lighthouse-php.com/master/concepts/arg-resolvers.html#solution
但我不知道在哪里以及如何创建这个resolver function。
这些例子解释不多。
【问题讨论】:
标签: laravel graphql laravel-lighthouse