【发布时间】:2022-01-07 10:38:25
【问题描述】:
我的graphql.schema 描述了搜索查询(分页、按 ID 搜索等),但除此之外我还需要检查一些其他条件,例如,在我的数据库中有一个字段包含有关是否有记录的信息是否已删除,即我实际上并没有删除记录,而只是覆盖了该字段的值。
因此,现在我得到了所有记录,不管exist 字段等于什么。
我只需要获取该字段等于true的记录。
我花了很多时间在 CO 中寻找答案并阅读文档,但我从未找到我需要的东西。我发现的示例假设条件将在客户端设置,但我需要在 后端 端执行此操作。
使用 laravel 我可以使用此代码获得等效结果,因此解决方案可能是一种覆盖使用 lighthouse 的内容的方法:
$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 来解决。
UPD 2:
我知道我可以为所有查询使用 @field 指令创建解析器,但此解决方案不适用于关系。
【问题讨论】:
标签: php laravel graphql lighthouse