My graphql.schema
describes search queries (pagination, search by ID, etc.), but in addition to that I need to check some more conditions, for example, in my database there is a field containing information about whether a record has been deleted or not, i.e. I am not actually deleting the records, but just overwriting the value of that field.
Accordingly, now I get all the records, regardless of what the exist
field is equal to.
I only need to get records where this field is equal to true
.
I spent a lot of time looking for an answer in CO and reading the documentation, but I never found what I needed. The examples I've found assume the condition will be set on the client side, but I need to do this on the backend side.
Using laravel I can get an equivalent result with this code, so a solution might be a way to overwrite something that uses lighthouse:
$product = Product::where('id', '=', $id)
->where('exist', '=', true)
->first();
Maybe I’m doing something wrong or I don’t understand something, but it looks like no one had such a need or problem.
Does anyone know how this can be done?
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);
}
}
Thanks in advance!
UPD 1:
In addition, I need to find relations in the same way. I suspect it might not be lighthouse related and is solved by adding middleware
.
UPD 2:
I know what I can create a resolver using the @field
directive for all queries, but this solution won't work for relations.
I think I found a solution! Apparently initially I missed this point in the documentation and began to write my own implementation
Now I switched to using SoftDeletes
and everything worked as I needed :)