更新
|
这是 GraphQL Library 7 版本的文档。对于长期支持 (LTS) 版本 5,请参考 GraphQL Library 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: { set: "Some new content for this Post!" } }
) {
posts {
content
}
}
}
这将通过添加句子“Some new content for this Post!”来更新帖子。
使用 update 的嵌套 create
您可以不使用 create 突变创建 Post 再将其连接到 User,而是在突变中更新 User 并 create 一个 Post。
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
}
}
}
}
欲了解更多关系操作示例,请参阅 关系操作。
数组方法
数组方法允许在这些实体的 update 突变中修改已有的属性数组
-
节点
-
关系属性
-
接口
为此,可使用以下运算符
-
push -
pop
push
push 必须符合类型定义中所规定的输入类型。
考虑以下类型定义:一个具有名为 tags 的 String 类型属性数组的 Movie
type Movie @node {
title: String
tags: [String!]
}
您可以向 tags 属性数组中 push(添加)标签
push 的突变mutation {
updateMovies (update: { tags: { push: "another tag" } }) {
movies {
title
tags
}
}
}
| 之前 | 之后 |
|---|---|
|
|
或在一次更新中 push 多个元素
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:对两个不同数组使用 _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(移除)元素
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
pop 的突变mutation {
updateMovies(update: { tags: { pop: 1 }, moreTags: { pop: 2 } }) {
movies {
title
tags
moreTags
}
}
}
| 之前 | 之后 |
|---|---|
|
|
数学运算符
您可以使用数学运算符基于原始值在单个数据库事务中更新数值字段。为此,不同数值类型(Int、Float 和 BigInt)提供特定运算符。这些运算符在这些实体中受支持。
-
节点
-
关系属性
-
接口
针对 Int、Float 和 BigInt 类型,可使用以下运算符
-
add -
subtract
对于 Float 类型,还可使用以下额外运算符
-
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: { add: 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 {
properties {
revenue
}
}
}
}
}
}