簡體   English   中英

Laravel Lighthouse GraphQL 重命名關系突變

[英]Laravel Lighthouse GraphQL renamed relationship mutation

我在使用 graphQL 查詢更新重命名的關系時遇到問題。

這是相關的架構和 Laravel 模型:

Laravel 模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Lead extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    // protected $fillable = [
    //     'lead_type_id',
    // ];

    protected $guarded = [];
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        //
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        // 'created_at' => 'timestamp',
        // 'updated_at' => 'timestamp'
    ];

    /**
     * Get the LeadActions for the Lead.
     */
    public function leadActions()
    {
        return $this->hasMany(\App\Models\LeadAction::class);
    }

    /**
     * Get the clients for the Lead.
     */
    public function clients(): BelongsToMany
    {
        return $this->belongsToMany(Client::class);
    }

    /**
     * Get the LeadType for the Lead.
     */
    public function leadType(): BelongsTo
    {
        return $this->belongsTo(\App\Models\LeadType::class, 'lead_type_id');
    }
}

?>

<?php

namespace App\Models;

use App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class LeadType extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        //
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'name' => 'string',
        'created_at' => 'timestamp',
        'updated_at' => 'timestamp'
    ];

    /**
     * Get the Leads for the LeadType.
     */
    public function leads(): HasMany
    {
        return $this->hasMany(Models\Lead::class);
    }

}
?>

GraphQl 模式

type Lead {
   id: ID!
   lead_type: LeadType @belongsTo(relation: "leadType")
   clients: [Client!]! @belongsToMany
   created_at: DateTime!
   updated_at: DateTime!
}
input UpdateLeadInput {
   id: ID!
   clients: UpdateClientsRelation
   lead_type: UpdateLeadTypeRelation
}
input UpdateLeadTypeRelation {
   create: CreateLeadTypeInput
   connect: ID
   update: UpdateLeadTypeInput
   upsert: UpsertLeadTypeInput
   delete: ID
   disconnect: ID
}

使用下面的 graphQl 查詢,我得到一個缺少列 Lead_type 的 SQL 錯誤:查詢

mutation UpdateLead {
  updateLead(input: {id: 1, lead_type: {connect: 1}}) {
    id
    clients {
      id
      name
    }
    lead_type {
      id
      name
    }
  }
}

SQL 錯誤

Column not found: 1054 Unknown column 'lead_type' in 'field list' (SQL: update `leads` set `lead_type` = {\"connect\":\"1\"}, `leads`.`updated_at` = 2020-01-14 17:11:17 where `id` = 1

我遵循了 Laravel 命名約定,並在 Lead_type_id 表上命名了列 Lead_type_id。 如果我將lead_type 關系的重命名刪除為leadType,我可以成功運行更新突變,但我無法弄清楚如何讓它使用正確的列名稱(lead_type_id)同時保持關系重命名。

任何幫助是極大的贊賞。

非常感謝

您是否嘗試過@rename指令? 我的意思是你必須在你的lead_type中的UpdateLeadInput上使用它,因為燈塔尋找名為lead_type關系,並且由於它沒有定義,它假設lead_type是一個參數。

要么在模型中重命名您的關系,例如:

class Lead extends Model
{
    public function lead_actions()
    {
        return $this->hasMany(\App\Models\LeadAction::class);
    }

    public function lead_type(): BelongsTo
    {
        return $this->belongsTo(\App\Models\LeadType::class, 'lead_type_id');
    }
}

或者

使用@rename指令(我沒有嘗試過,但我的意思是它是這樣工作的):

input UpdateLeadInput {
   id: ID!
   clients: UpdateClientsRelation
   lead_type: UpdateLeadTypeRelation @rename(attribute: "leadType")
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM