温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - Laravel Lighthouse Graphql belongsToMany mutation with pivot values
graphql laravel laravel-lighthouse

其他 - Laravel Lighthouse Graphql属于具有透视值的ToMany突变

发布于 2020-04-07 11:57:56

我有一些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将数据插入数据透视表?

查看更多

提问者
Hendrik Jan
被浏览
57
lorado 2020-01-08 03:16

目前不存在对数据透视表进行突变的操作,但是上周我进行了PR。您可以关注PR进度

通常,它会以我做的方式包含在内,或者可能有所不同。在本期中进行了讨论

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