【问题标题】:Laravel Lighthouse Graphql belongsToMany mutation with pivot valuesLaravel Lighthouse Graphql 属于ToMany 具有枢轴值的突变
【发布时间】:2020-04-25 07:41:20
【问题描述】:

我有一些 Laravel 模型,它们通过 belongsToMany 关系中的数据透视表关联。
现在我尝试在 Laravel Lighthouse 中创建一个突变来加入模型并填充枢轴值。
不知怎的,我不知道该怎么做。

课程模型如下所示:

class Course extends Model
{
    public function programs(): BelongsToMany
    {
        return $this->belongsToMany(\App\Models\Program::class, 'Course_Program')
                    ->withPivot('id', 'year_id')
                    ->withTimestamps();
    }
}

我的 GraphQL 代码如下所示:

type Mutation {
    createCourse(input: CreateCourseInput! @spread): Course! @create
}

type Course {
    id: ID!
    name: String!
    programs: [ProgramWithPivot!]! @belongsToMany
}

type Program {
    id: ID!
    name: String!
}

type ProgramWithPivot {
    id: ID!
    name: String!
    year_id: ID!
}

input CreateCourseInput {
    name: String!
    programs: CreateCourseProgramsRelation!
}

input CreateCourseProgramsRelation {
    create: [CreateCourseProgramInput!]
}

input CreateCourseProgramInput {
    id: ID!
    year_id: ID!
}

问题是当我尝试在我的课程中创建这样的程序时:

mutation {
  createCourse(input: {
    name: "new cours"
    programs: {
      create: [{
        id: 1
        year_id: 2
      }]
    }
  }) {
    id
    programs {
      id
      year_id
    }
  }
}

Laravel Lighthouse 尝试将数据插入到 Program 表中(并抱怨 Program.name 没有默认值)。
但是,我想将数据(course_id、year_id 和 program_id)插入到 Course_Program 表中。

如何告诉 Laravel Lighthouse 在数据透视表中插入数据?

【问题讨论】:

    标签: laravel graphql laravel-lighthouse


    【解决方案1】:

    有什么方法可以通过嵌套突变来更新/更新数据?

    【讨论】:

      【解决方案2】:

      目前不存在变异数据,但我上周为此做了 PR。你可以follow the PR progress

      一般来说,它会以我的方式包含在内,或者可能会有所不同。是discussed in this issue

      目前您只能使用自定义解析器/指令来更新您的数据透视数据。但可能最好的方法就是等到 PR 被合并。

      【讨论】:

      猜你喜欢
      • 2020-05-01
      • 2019-07-27
      • 2020-05-11
      • 2017-10-27
      • 2016-07-08
      • 2018-08-28
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多