如何为 Laravel Lighthouse 中的突变定义多态的一对多关系?

How does one define a polymorphic one-to-many relation for mutations in Laravel Lighthouse?

所以我一直在用头撞墙解决这个问题,到目前为止我还没有找到解决办法。

我用最新的 Lighthouse(4.12) 创建了一个新的、脆弱的 Laravel 项目,当我试图按照相关文档进行操作时,我完全迷路了。

因此,根据 docs,我制定了以下架构:

type Query {
    posts: [Post!]! @paginate(defaultCount: 10)
}

type Mutation {
  createPost(input: CreatePostInput! @spread): Post @create
}

input CreatePostInput {
  title: String!
  content: String
  images: CreateImageMorphToMany
}

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


type User {
    id: ID!
    name: String!
    email: String!
    created_at: DateTime!
    updated_at: DateTime!
    images: Image! @morphOne
}

type Post {
    id: ID!
    title: String
    content: String
    images: [Image]! @morphMany
}

type Image {
    id: ID!
    src: String
    name: String
    imageable: Imageable @morphTo
}

union Imageable = Post | User

与以下 类:

Post 型号

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class Post extends Model
{

    protected $fillable = [
        'title', 'content'
    ];

    public function images(): MorphMany
    {
        return $this->morphMany('App\Image', 'imageable');
    }
}

图片模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Image extends Model
{
    public function imageable(): MorphTo
    {
        return $this->morphTo();
    }
}


和迁移:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('content');
            $table->timestamps();
        });
        // This is only here for the sake of simplicity
        Schema::create('images', function (Blueprint $table) {
            $table->id();
            $table->string('src');
            $table->string('name');
            $table->integer('imageable_id');
            $table->string('imageable_type');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
        Schema::dropIfExists('images');
    }
}

并且使用提供的 GraphQL 游乐场,我希望有以下变化:

mutation {
  createPost(input: {
    title: "asd", 
    content: "asd",
    images: {
      connect: [1]
    }

  }) {
    title
    images {
      src
    }
  }
}

会 return 创建 Post 的 connected/synced 图像,但只有 post 是 returned。

如果我手动设置关系并查询它,一切正常。

提前致谢!

事实证明,我误解了文档(自我提出问题后更新的内容),因为吗啡酮的工作原理与 hasone 完全一样,所以正确的方法是创建、删除、更新