突变不保存关系 ID

mutation doesn't save ralation id

我在 laravel 上遇到 lighthouse graphql 的问题。

一切都适用于查询或简单的 create/update,但涉及到关系时就变得困难了。

它不保存关系。

我的例子:

型号

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 table.

我已经尝试过的:

https://lighthouse-php.com/master/eloquent/nested-mutations.html#return-types-required

https://lighthouse-php.com/master/concepts/arg-resolvers.html#solution

但我不知道在哪里以及如何创建这个 resolver function。 这些例子解释不多。

您应该使用相同的关系名称,并在相关输入中附加关系类型。例如,如果 Product 有一个 manufacturer,则输入名称应为:CreateManufacturerHasOne.

所以 graphql 文件应该是这样的:

extend type Mutation {
    createProduct(
       input: CreateProduct! @spread
    ): Product @create
}


input CreateProduct {
    manufacturer: CreateManufacturerHasOne
}

input CreateManufacturerHasOne {
    connect: ID
    create: CreateManufacturerInput
}

input CreateManufacturerInput {
    name: String!
}

有关 https://lighthouse-php.com/master/eloquent/nested-mutations.html#hasone 的更多信息。

感谢您的回复。 不幸的是,这两个答案都不起作用。

还是一样,发送请求后id_product_manufacturer为null

mutation {
    createProduct(
        input: {
            manufacturer: {
                create: {
                    name: "test"
                }
            }
        }
    ){
        id_product
    }
}

好的。我解决了问题。 问题出在 laravel 模型中的关系类型中。

这个有效:

型号

class Product extends Model
{
    public $timestamps = false;
    protected $table = 'products';
    protected $primaryKey = 'id_product';

    public function manufacturer(): BelongsTo
    {
        return $this->belongsTo(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(): HasMany
    {
        return $this->hasMany(Product::class, 'id_product_manufacturer', 'id_product_manufacturer');
    }
}