[英]What is the correct way of using @scope directive?
我無法理解 @scope 指令的工作原理。 當我將指令添加到查詢定義並給出 IsOwner: true Lighthouse 將其添加到范圍時,但當我將其更改為 IsOwner:false 時問題就開始了。 Lighthouse 仍將范圍應用到查詢中。 只有當我從查詢中刪除 IsOwner 參數時,Lighthouse 才不應用范圍。
我的查詢定義如下;
listings(IsOwner: Boolean @scope(name:"IsOwner"), where: _ @whereConditions(columnsEnum: "ListingColumn"), orderBy: _ @orderBy(columnsEnum: "ListingColumn")) : [Listing!]! @paginate(maxCount: 50 defaultCount: 10)
實際查詢如下;
query ($min_lng:Float!, $min_lat:Float!, $max_lng:Float!, $max_lat:Float!, $first:Int, $page:Int, $withPaginatorInfo:Boolean!, $withListingImages:Boolean!, $withListingMeta:Boolean!, $withListingViews:Boolean!, $withListingActions:Boolean!, $withListingNotes:Boolean!, $withListingReminders:Boolean!, $withListingViewUser:Boolean = false, $conditions: ListingsWhereWhereConditions, $orderBy: [ListingsOrderByOrderByClause!]) {
listings(
bbox: {
min_lng: $min_lng
min_lat: $min_lat
max_lng: $max_lng
max_lat: $max_lat
}
IsOwner: false
where: $conditions
orderBy: $orderBy
first: $first
page: $page
) {
data {
...listingFields
}
paginatorInfo @include(if: $withPaginatorInfo) {
currentPage
lastPage
}
}
}
Model scope it like following;
public function scopeIsOwner($query)
{
return $query->join('listing_user', 'listing_user.listing_id', '=', 'listings.id')->where('listing_user.user_id', Auth::user()->id);
}
*** 更新 ***
我在評論后想通了,我將模型范圍更改為以下內容
public function scopeIsOwner($query, $isEnabled)
{
if ($isEnabled) {
return $query->join('listing_user', 'listing_user.listing_id', '=', 'listings.id')->where('listing_user.user_id', Auth::user()->id);
}
return $query;
}
我對架構進行了細微的更改,如下所示,
listings(scopeIsOwner: Boolean @scope(name:"IsOwner"), bbox: BoundingBoxInput! @builder(method: "App\\GraphQL\\Builders\\BoundingBoxSearch@bbSearch"), where: _ @whereConditions(columnsEnum: "ListingColumn"), orderBy: _ @orderBy(columnsEnum: "ListingColumn")) : [Listing!]! @guard(with: ["api"]) @paginate(maxCount: 50 defaultCount: 10)
我的最終查詢及其變量如下所示,
query ($min_lng:Float!, $min_lat:Float!, $max_lng:Float!, $max_lat:Float!, $first:Int, $page:Int, $withPaginatorInfo:Boolean!, $withListingImages:Boolean!, $withListingMeta:Boolean!, $withListingViews:Boolean!, $withListingActions:Boolean!, $withListingNotes:Boolean!, $withListingReminders:Boolean!, $withListingViewUser:Boolean = false, $scopeIsOwner:Boolean = false $conditions: ListingsWhereWhereConditions, $orderBy: [ListingsOrderByOrderByClause!]) {
listings(
bbox: {
min_lng: $min_lng
min_lat: $min_lat
max_lng: $max_lng
max_lat: $max_lat
}
scopeIsOwner: $scopeIsOwner
where: $conditions
orderBy: $orderBy
first: $first
page: $page
) {
data {
...listingFields
}
paginatorInfo @include(if: $withPaginatorInfo) {
currentPage
lastPage
}
}
}
變量;
{
"min_lng": 29.0401317,
"min_lat": 41.0028473,
"max_lng": 29.0308512,
"max_lat": 40.9916271,
"withPaginatorInfo" : true,
"first": 10,
"page": 1,
"withListingImages": false,
"withListingMeta": false,
"withListingViews": false,
"withListingActions": false,
"withListingNotes": false,
"withListingReminders": false,
"withListingViewUser": false,
"conditions": {"AND": [{"column": "PRICE", "operator": "LTE","value": "190"}]},
"orderBy": [{ "column": "TENURE_ID", "order": "DESC" }],
"scopeIsOwner": false
}
正如您所理解的,當我對 scopeIsOwner 變量端點給予 true 時,會啟用范圍並相應地操作查詢。
你是誤會了。 見https://lighthouse-php.com/master/api-reference/directives.html#scope
scope 方法將接收客戶端給定的參數值作為第二個參數。
您的范圍應該對客戶端傳遞的參數做出反應。
Lighthouse 庫中有一個錯誤。 當參數存在而不檢查值時添加范圍。 ScopeDirective 類和 handleBuilder 方法非常簡單
public function handleBuilder($builder, $value)
{
$scope = $this->directiveArgValue('name');
try {
return $builder->{$scope}($value);
} catch (BadMethodCallException $exception) {
throw new DefinitionException(
$exception->getMessage()." in {$this->name()} directive on {$this->nodeName()} argument.",
$exception->getCode(),
$exception->getPrevious()
);
}
}
我認為 $value 包含 true 或 false 並且不應將此變量注入到此處的作用域中:
$builder->{$scope}($value);
相反,應該是這樣的:
if ($value == true) {
$builder->{$scope}();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.