【问题标题】:How to "upsert" an array/nested field when issuing update mutation in Graphcool / GraphQL?在 Graphcool / GraphQL 中发布更新突变时如何“更新”数组/嵌套字段?
【发布时间】:2018-04-30 10:18:05
【问题描述】:

我有一个Post 类型而不是一个tag 字段,该字段可以与许多Tag 条目相关联(一对多关系)。我遇到的问题是在更新Post 时 - 我需要为尚不存在的标签创建和关联新的Tag,同时保留现有的Post->Tag 关系。基本上,当在嵌套的一对多字段上发布突变更新时,我正在寻找类似于 upsert 的东西。

这是我的架构:

type Post @model {
  createdAt: DateTime!
  createdBy: User @relation(name: "PostsByUser")
  description: String @defaultValue(value: "''")
  id: ID! @isUnique
  tags: [Tag!]! @relation(name: "TagsOfPost")
  ...
}

type Tag @model {
  id: ID! @isUnique
  tag: String!
  createdBy: User @relation(name: "TagsByUser")
  createdAt: DateTime!
  posts: [Post!]! @relation(name: "TagsOfPost")
}

此突变可用于更新 Post 并添加 标签,但会覆盖 Posttag 字段中的所有现有值:

mutation updatePost(
  $id: ID!
  $createdById: ID!
  $timestamp: DateTime!
  $description: String
  $tags: [PosttagsTag!]!
) {
  updatePost(
    id: $id
    createdById: $createdById
    timestamp: $timestamp
    description: $description
    tags: $tags
  ) {
    id
    timestamp
    description
    tags {
      id
      tag
    }
    createdBy {
      id
      username
    }
  }
}

我通过@marktani 遇到了this post,但不清楚如何实现他概述的combined 方法:

合并 您还可以在同一突变中使用标签和标签ID,这会将新的教程节点连接到标签ID中的所有标签,并将其连接到标签中的新标签。如果您只想允许带有唯一文本的标签,这就是您想要做的,因此对于新教程,可能会有一些标签已经存在,而一些需要创建。

目前是否不可能用一个突变来做到这一点?在使用新标签更新帖子以重新建立Post 和现有Tag id 之间的关联后,是否需要进行第二次突变,即必须反复调用addToTagsOfPost(tagsTagId: ID! postsPostId: ID!)?谢谢!

【问题讨论】:

    标签: graphql apollo react-apollo apollo-client graphcool


    【解决方案1】:

    好的,所以there is currently a Graphcool bugtagstagsIds 传递给突变将创建并关联新创建的 Tags,但不会将tagsIds 关联添加到现有 Tags。我在 Graphcool 的 GitHub 存储库上发布了一个问题,他们已经承认了 - https://github.com/graphcool/framework/issues/1288

    【讨论】:

      猜你喜欢
      • 2018-01-08
      • 2017-07-05
      • 2012-03-25
      • 2021-12-02
      • 2020-12-22
      • 1970-01-01
      • 2021-03-24
      • 2019-05-22
      • 2023-03-14
      相关资源
      最近更新 更多