【问题标题】:Laravel Lighthouse Nested Mutations Not WorkingLaravel Lighthouse 嵌套突变不起作用
【发布时间】:2020-08-18 15:40:22
【问题描述】:

我正在使用 Laravel Lighthouse 为我的新应用创建 GraphQL API,现在 Nested Belongs to and Belongs to Many 不适用于创建和更新方法,在数据透视表中,没有添加任何内容,我收到以下错误:

"SQLSTATE[HY000]: General error: 1364 Field 'category_id' doesn't have a default value (SQL: insert into `products` (`title`, `updated_at`, `created_at`) values (new product, 2020-05-03 19:40:59, 2020-05-03 19:40:59))"

产品迁移:

public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->unsignedBigInteger('category_id');
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->integer('status')->default(1);
            $table->softDeletes();
            $table->timestamps();
        });
    }

标签迁移:

public function up()
    {
        Schema::create('tags', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->unsignedBigInteger('category_id');
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->integer('status')->default(1);
            $table->softDeletes();
            $table->timestamps();
        });
    }

枢轴迁移:

public function up()
    {
        Schema::create('product_tag', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('product_id');
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->unsignedBigInteger('tag_id');
            $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
            $table->timestamps();
        });
    }

产品型号:

class Product extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'title', 'category_id', 'status'
    ];

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

    public function tags()
    {
        return $this->belongsToMany(Tag::class);
    }
}

标签模型:

class Tag extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'title', 'category_id', 'status'
    ];

    protected static function booted()
    {
        static::deleted(function ($tag) {
           $tag->products()->detach();
        });
    }

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

    public function products()
    {
        return $this->belongsToMany(Product::class);
    }
}

产品架构

extend type Query @guard(with: ["sanctum"]) {
    products(orderBy: _ @orderBy trashed: Trashed @trashed): [Product] @all
}

extend type Mutation @guard(with: ["sanctum"]) {
    createProduct(
        title: String! @rules(apply: ["required"])
        category: CreateCategoryRelation
        tags: UpdateProductTags
    ): Product @create

    updateProduct(
        id: ID!
        title: String! @rules(apply: ["required"])
        category: CreateCategoryRelation
        tags: UpdateProductTags
    ): Product @update

    deleteProduct(
        id: ID!
    ): Product @delete
}

type Product {
    id: ID!
    title: String!
    category: Category!
    tags: [Tag!]
}

input CreateCategoryRelation {
    connect: ID!
}

input UpdateProductTags {
    connect: [ID!]
    sync: [ID!]
}

有谁知道问题出在哪里?

【问题讨论】:

    标签: laravel laravel-lighthouse


    【解决方案1】:

    我找到了解决方案,Lighthouse 使用类型提示来确定它应该如何处理这种关系。

    use Illuminate\Database\Eloquent\Relations\BelongsTo;
    
    class Post extends Model 
    {
        // WORKS
        public function user(): BelongsTo
        {
            return $this->belongsTo(User::class);
        }
    
        // DOES NOT WORK
        public function comments()
        {
            return $this->hasMany(Comment::class);        
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-21
      • 2020-04-20
      • 2020-12-25
      • 2020-04-08
      • 1970-01-01
      • 2020-05-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      相关资源
      最近更新 更多