Laravel Lighthouse GraphQL重命名关系突变

时间:2020-01-14 17:23:36

标签: php mysql laravel graphql laravel-lighthouse

我在使用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命名约定,并在Leads表上将列Lead_type_id命名为。如果取消将lead_type关系重命名为leadType,则可以成功运行更新突变,但是在保持关系重命名的同时,我不知道如何在列中使用正确的名称(lead_type_id)。

非常感谢您的帮助。

非常感谢

1 个答案:

答案 0 :(得分:1)

您是否尝试过@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');
    }
}

OR

使用@rename指令(我没有尝试过,但我的意思是它像这样工作):

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