在 Graphcool / GraphQL 中发布更新突变时如何 "upsert" 一个 array/nested 字段?
How to "upsert" an array/nested field when issuing update mutation in Graphcool / GraphQL?
我有一个 Post
类型,而不是一个 tag
字段,它可以与许多 Tag
条目相关联(一对多关系)。我 运行 遇到的问题是在更新 Post
时 - 我需要为尚不存在的标签创建和关联新的 Tag
,同时保留现有的 [=14] =]->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
并添加 new 标签,但会覆盖 Post
的 [=15] 中的所有现有值=] 字段:
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
}
}
}
我遇到了 this post by @marktani 但不清楚如何实施他概述的 组合 方法:
Combined
You can also use tags and tagsIds in the same mutation, this would connect the new Tutorial node to all the tags in tagsIds and connect it to the new tags in tags. This is what you want to do if you want to only allow tags with a unique text, so for a new Tutorial, there will likely be some tags that already exists, and some that need to be created.
目前不可能用一种突变来做到这一点吗?在用新标签更新 post 以重新建立 Post
和现有 Tag
id 之间的关联后是否需要第二次突变,即必须重复调用 addToTagsOfPost(tagsTagId: ID!
postsPostId: ID!)
?谢谢!
好的,所以 there is currently a Graphcool bug where passing in both tags
and tagsIds
to the mutation will create and associate newly created Tags
but will not add the tagsIds
associations to existing Tags
. I posted an issue on Graphcool's GitHub repo and they have acknowledged it - https://github.com/graphcool/framework/issues/1288
我有一个 Post
类型,而不是一个 tag
字段,它可以与许多 Tag
条目相关联(一对多关系)。我 运行 遇到的问题是在更新 Post
时 - 我需要为尚不存在的标签创建和关联新的 Tag
,同时保留现有的 [=14] =]->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
并添加 new 标签,但会覆盖 Post
的 [=15] 中的所有现有值=] 字段:
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
}
}
}
我遇到了 this post by @marktani 但不清楚如何实施他概述的 组合 方法:
Combined You can also use tags and tagsIds in the same mutation, this would connect the new Tutorial node to all the tags in tagsIds and connect it to the new tags in tags. This is what you want to do if you want to only allow tags with a unique text, so for a new Tutorial, there will likely be some tags that already exists, and some that need to be created.
目前不可能用一种突变来做到这一点吗?在用新标签更新 post 以重新建立 Post
和现有 Tag
id 之间的关联后是否需要第二次突变,即必须重复调用 addToTagsOfPost(tagsTagId: ID!
postsPostId: ID!)
?谢谢!
好的,所以 there is currently a Graphcool bug where passing in both tags
and tagsIds
to the mutation will create and associate newly created Tags
but will not add the tagsIds
associations to existing Tags
. I posted an issue on Graphcool's GitHub repo and they have acknowledged it - https://github.com/graphcool/framework/issues/1288