php - Laravel Lighthouse GraphQL 重命名关系突变
问题描述
我在使用 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 关系重命名为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');
}
}
或者
use@rename
指令(我没有尝试过,但我的意思是它是这样工作的):
input UpdateLeadInput {
id: ID!
clients: UpdateClientsRelation
lead_type: UpdateLeadTypeRelation @rename(attribute: "leadType")
}
推荐阅读
- java - Tapestry Page 翻到下一页:点击下一页(如果下一页的表格内容是可编辑的,tapestry 会报错)
- html - 如何解决 3 个重叠按钮?
- android - GridLayout中RecyclerView时最后一项无法显示
- javascript - Card Mismatch & Card Match (记忆游戏)
- python - 为什么if条件在256之后得到满足?
- ionic-framework - 用户需要时动态更改离子标题
- c# - c#中的标量函数
- xcode - WatchKit 扩展 [31646:1403662] libMobileGestalt utility.c:421:未找到密钥 1129072723 的值
- kubernetes - 从 kubernetes 中提取本地存储库 docker 映像
- python - 是否有能够读取表格单元格背景颜色的 python-pptx 函数?