微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Lighthouse GraphQL-数据透视表上的HasManyThrough

如何解决Lighthouse GraphQL-数据透视表上的HasManyThrough

我有一个项目要从Rails REST API转换为Laravel GraphQL API(我是Laravel的新手),并且正在建立模型和关系。

我有一个categoriescategory_itemsitems表。

category_items表中包含category_iditem_idposition的列。

items表中有一个category_id

当我打开工匠修补匠并运行App\Models\Category::find(1)->items()->get();时,它将返回所有预期的内容包括嵌套在Item对象下方的数据透视表)。但是,当我在graphql-playground中运行它时,出现错误

"debugMessage": "sqlSTATE[42S02]: Base table or view not found: 1146 Table 'my_project.category_item' doesn't exist (sql: select * from `category_item` where 0 = 1)",

我有种感觉,我忽略了它的细微之处,如错字或我不知道的命名约定。

我还想做的是将数据透视表中的position列包含在item对象中。本质上,我希望不是将“ position”列嵌套在Item的枢轴内部,而是希望将“ position”作为Item对象的一部分包括在内,但是我不确定是否可行。

Schema.graphql

type Query {
    categories: [Category!]! @field(resolver: "CategoriesQuery@find_by_user")
    items: [Item!]! @field(resolver: "ItemsQuery@find_by_user")
    notifications: [Notification!]! @field(resolver: "NotificationQuery@find_by_user")
}

type Category {
    id: ID!
    name: String!
    created_at: DateTime!
    updated_at: DateTime!

    category_items: [CategoryItem!]! @hasMany
    items: [Item!]! @belongsToMany
}

type CategoryItem {
    id: ID!
    position: Int!
    created_at: DateTime!
    updated_at: DateTime!

    categories: [Category!]! @hasMany
    items: [Item!]! @hasMany
}

type Item {
    id: ID!
    name: String
    quantity: Int
    created_at: DateTime!
    updated_at: DateTime!

    categories: [Category]! @belongsToMany
    category_item: CategoryItem! @belongsTo
}

Category.PHP

<?PHP

namespace App\Models;

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

class Category extends Model
{
    protected $fillable = [
        'name'
    ];

    // automatically eager load items of a category
    protected $with = ['items'];

    // define relationships
    public function category_items(): HasMany
    {
        return $this->hasMany(CategoryItem::class);
    }

    public function items(): BelongsToMany
    {
        return $this
            ->belongsToMany('App\Models\Item','category_items','item_id','category_id')
            ->using('App\Models\CategoryItem')
            ->withPivot(['position'])
            ->withTimestamps();
    }
}

CategoryItem.PHP

<?PHP

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\Relations\HasMany;

class CategoryItem extends Pivot
{
    protected $fillable = [
        'category_id','position'
    ];

    // public function category(): HasMany
    // {
    //     return $this->hasMany(Category::class);
    // }

    // public function item(): HasMany
    // {
    //     return $this->hasMany(Item::class);
    // }
}

Item.PHP

<?PHP

namespace App\Models;

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

class Item extends Model
{
    protected $fillable = [
        'name','price','category_id','quantity'
    ];

    // define relationships
    public function categories(): BelongsToMany
    {
        return $this
            ->belongsToMany('App\Models\Category','category_id')
            ->using('App\Models\CategoryItem')
            ->withPivot(['position'])
            ->withTimestamps();
    }

    public function category_item(): BelongsTo
    {
        return $this->belongsTo('App\Models\CategoryItem');
    }
}

最后,我的问题是:

  1. 是什么原因导致运动场中的GraphQL错误
  2. 是否可以将数据透视表属性附加到其直接父对象?

解决方法

@ miken32帮了大忙。我删除了不必要的CategoryItem.php模型,将数据库表从category_items重命名为category_item,然后在我的schema.graphql中将类型重新定义为以下类型:

type Category {
    id: ID!
    name: String!
    created_at: DateTime!
    updated_at: DateTime!

    pivot: CategoryItemPivot
    items: [Item!]! @belongsToMany
}

type CategoryItemPivot {
    id: ID!
    position: Int!
    created_at: DateTime!
    updated_at: DateTime!

    categories: [Category!]! @hasMany
    items: [Item!]! @hasMany
}

type Item {
    id: ID!
    name: String
    quantity: Int
    created_at: DateTime!
    updated_at: DateTime!

    categories: [Category!]! @belongsToMany
    pivot: CategoryItemPivot
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。