更新
这是 GraphQL 库版本 6 的文档。对于长期支持 (LTS) 版本 5,请参阅 GraphQL 库版本 5 LTS。 |
考虑以下类型定义
type Post @node {
id: ID! @id
content: String!
creator: User! @relationship(type: "HAS_POST", direction: IN)
}
type User @node {
id: ID! @id
name: String
posts: [Post!]! @relationship(type: "HAS_POST", direction: OUT)
}
这些 update
变异和响应类型是生成的
type UpdatePostsMutationResponse {
posts: [Post!]!
}
type UpdateUsersMutationResponse {
users: [User!]!
}
type Mutation {
updatePosts(
where: PostWhere
update: PostUpdateInput
): UpdatePostsMutationResponse!
updateUsers(
where: UserWhere
update: UserUpdateInput
): UpdateUsersMutationResponse!
}
由于已使用 |
单个 update
您可以通过执行以下 GraphQL 语句来更新 Post
的内容
mutation {
updatePosts(
where: {
id_EQ: "892CC104-A228-4BB3-8640-6ADC9F2C2A5F"
}
update: {
content: "Some new content for this Post!"
}
) {
posts {
content
}
}
}
这通过添加句子“Some new content for this Post!”来更新帖子。
使用 update
嵌套 create
而不是使用 create
变异创建 Post
然后将其连接到 User
,您可以更新 User
并将 Post
作为变异的一部分进行 create
mutation {
updateUsers(
where: { name_EQ: "John Doe" }
update: {
posts: {
create: [
{ node: { content: "An interesting way of adding a new Post!" } }
]
}
}
) {
users {
id
name
posts {
content
}
}
}
}
connectOrCreate
关系
请考虑 create
页面中提供的示例
mutation {
createActors(input: {
name: "Tom Hanks",
movies: {
connectOrCreate: {
where: { node: { id_EQ: "1234" } }
onCreate: { node: { title: "Forrest Gump" } }
}
}
}) {
info {
nodesCreated
}
}
}
connectOrCreate
也可用于 update
操作
mutation {
updateActors(
update: {
movies: {
connectOrCreate: {
where: { node: { id_EQ: "1234" } }
onCreate: { node: { title: "Forrest Gump" } }
}
}
},
where: { name_EQ: "Tom Hanks" }
) {
info {
nodesCreated
}
}
}
此操作等效于前面的示例。
数组方法
数组方法允许在这些实体内的 update
变异中修改现有的属性数组
-
节点
-
关系属性
-
接口
为此,可以使用以下运算符
-
_PUSH
-
_POP
_PUSH
_PUSH
符合类型定义中定义的输入类型。
考虑以下类型定义,一个名为 tags
的 String
类型属性数组的 Movie
type Movie @node {
title: String
tags: [String]
}
您可以将标签推送到 tags
属性数组
_PUSH
的变异mutation {
updateMovies (update: { tags_PUSH: "another tag" }) {
movies {
title
tags
}
}
}
之前 | 之后 |
---|---|
|
|
或者在单个更新中推送多个元素
_PUSH
的变异mutation {
updateMovies (update: { tags_PUSH: ["another tag", "one more tag"] }) {
movies {
title
tags
}
}
}
之前 | 之后 |
---|---|
|
|
类似地,您可以有多个数组属性字段并在同一查询中更新它们
type Movie @node {
title: String
tags: [String]
moreTags: [String]
}
您还可以推送到 tags
和 moreTags
属性数组:.具有 _PUSH
推送到两个不同数组的变异
mutation {
updateMovies (update: { tags_PUSH: "another tag", moreTags_PUSH: "a different tag" }) {
movies {
title
tags
moreTags
}
}
}
之前 | 之后 |
---|---|
|
|
_POP
_POP
期望单个 Int
值作为输入。
考虑以下类型定义,一个名为 tags
的属性数组的 Movie
type Movie @node {
title: String
tags: [String]
}
您可以从此 tags
属性数组中弹出
_POP
的变异mutation {
updateMovies (update: { tags_POP: 1 }) {
movies {
title
tags
}
}
}
之前 | 之后 |
---|---|
|
|
或者,对于数组中的多个属性
_POP
的变异mutation {
updateMovies (update: { tags_POP: 2 }) {
movies {
title
tags
}
}
}
之前 | 之后 |
---|---|
|
|
类似地,您可以有多个数组属性字段并在同一查询中更新它们
type Movie @node {
title: String
tags: [String]
moreTags: [String]
}
然后,您可以从 tags
和 moreTags
属性数组中弹出
_POP
的变异mutation {
updateMovies (update: { tags_POP: 1, moreTags_POP: 2 }) {
movies {
title
tags
moreTags
}
}
}
之前 | 之后 |
---|---|
|
|
数学运算符
您可以使用数学运算符在单个数据库事务中根据其原始值更新数值字段。为此,在不同的数值类型上提供了特定的运算符:Int
、Float
和 BigInt
。它们在这些实体中受支持
-
节点
-
关系属性
-
接口
对于 Int
和 BigInt
类型,可以使用以下运算符
-
_INCREMENT
-
_DECREMENT
对于 Float
类型,可以使用以下运算符
-
_ADD
-
_SUBTRACT
-
_MULTIPLY
-
_DIVIDE
运算符仍然作为可选字段可用。如果在未定义的字段中使用了数学运算符,则会导致 GraphQL 错误。 |
例如,考虑以下社交视频平台的 GraphQL 架构
type Video @node {
id: ID @id
views: Int
ownedBy: User @relationship(type: "OWN_VIDEO", properties: "OwnVideo", direction: IN)
}
type User @node {
id: ID @id
ownVideo: [Video!]! @relationship(type: "OWN_VIDEO", properties: "OwnVideo", direction: OUT)
}
type OwnVideo @relationshipProperties {
revenue: Float
}
假设用户在该平台上观看了一个视频,并且您想将该视频的 viewersCount
增加 1
。以下是如何操作的
mutation incrementViewCountMutation {
updateVideos(
where: { id_EQ: "VideoID" }
update: { views_INCREMENT: 1 }
) {
videos {
id
views
}
}
}
现在,假设您希望社交平台奖励用户 0.01 美元以观看视频。为此,您必须更新关系属性 revenue
mutation addRevenueMutation {
updateUsers(
where: { id_EQ: "UserID" },
update: { ownVideo: [{ update: { edge: { revenue_ADD: 0.01 } } }] }
) {
users {
id
ownVideoConnection {
edges {
revenue
}
}
}
}
}