如何向标准模型搜索添加条件?

How do I add a condition to a standard model search?

我的graphql.schema描述了搜索查询(分页,按ID搜索等),但除此之外我还需要检查更多条件,例如,在我的数据库中有一个包含信息的字段关于一条记录是否被删除,即我实际上不是删除记录,而是覆盖该字段的值。

因此,现在我得到了所有记录,而不管 exist 字段等于什么。

我只需要获取该字段等于true的记录。

我花了很多时间在 CO 中寻找答案并阅读文档,但我一直没有找到我需要的东西。我发现的示例假设条件将在客户端设置,但我需要在 backend 端执行此操作。

使用 laravel 我可以用这段代码得到等效的结果,所以一个解决方案可能是一种覆盖使用 lighthouse[ 的东西的方法=59=]:

$product = Product::where('id', '=', $id)
                ->where('exist', '=', true)
                ->first();

可能是我哪里做错了,或者有什么不明白,但是好像没有人有这样的需求或者问题。

有谁知道如何做到这一点?

graphql.schema:

type Query {
    products: [Product!]! @paginate
    product(id: ID @eq): Product @find
}

type Product {
    id: ID!
    title: String!
    description: String!
    tags: [Tag!]! @belongsToMany(relation: "tags")
    view: File! @belongsTo(relation: "view")
    price: Float!
    discount: Float!
    quantity: Int!
    created_at: DateTime!
    updated_at: DateTime!
    exist: Boolean!
}

Product.php

<?php

namespace App\Components\Product;

use App\Components\Tag\Tag;
use App\Components\File\File;
use App\Core\Model\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Product extends Model
{
    protected $fillable = [
        'title', 'description', 'tags', 'view_id', 'price', 'discount', 'quantity', 'exist'
    ];

    /**
     * @return BelongsToMany
     */
    public function tags(): BelongsToMany
    {
        return $this->belongsToMany(Tag::class);
    }

    /**
     * @return BelongsTo
     */
    public function view(): BelongsTo
    {
        return $this->belongsTo(File::class);
    }
}

提前致谢!


UPD 1:

另外,我需要用同样的方法找到关系。我怀疑它可能与 lighthouse 无关,可以通过添加 middleware.

来解决

更新 2:

我知道我可以使用 @field 指令为所有查询创建解析器,但此解决方案不适用于关系。

我想我找到了解决办法!显然最初我在文档中忽略了这一点并开始编写我自己的实现

现在我改用 SoftDeletes 并且一切都按我的需要工作:)