更新

这是 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!
}

id 字段无法更新,因为已使用 @id 指令。

单个 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 符合类型定义中定义的输入类型。

考虑以下类型定义,一个具有名为 tagsString 类型属性数组的 Movie

type Movie @node {
    title: String
    tags: [String!]
}

您可以将标签推送到 tags 属性数组

单个 push 变更
mutation {
    updateMovies (update: { tags: { push: "another tag" } }) {
        movies {
            title
            tags
        }
    }
}
之前 之后

['some tag']

['some tag', 'another tag']

或者在一个更新中推送多个元素

两个 push 变更
mutation {
    updateMovies (update: { tags: { push: ["another tag", "one more tag"] } }) {
        movies {
            title
            tags
        }
    }
}
之前 之后

['some tag']

['some tag', 'another tag', 'one more tag']

同样,您可以拥有多个数组属性字段并在同一查询中更新它们

type Movie @node {
    title: String
    tags: [String!]
    moreTags: [String!]
}

您还可以推送到 tagsmoreTags 属性数组:使用 _PUSH 推送到两个不同数组的变更

mutation {
    updateMovies (update: { tags: { push: "another tag" }, moreTags: { push: "a different tag" } }) {
        movies {
            title
            tags
            moreTags
        }
    }
}
之前 之后
    tags: ['some tag']
    moreTags: []
    tags: ['some tag', 'another tag']
    moreTags ['a different tag']

_POP

_POP 期望单个 Int 值作为输入。

考虑以下类型定义,一个具有名为 tags 的属性数组的 Movie

type Movie @node {
    title: String
    tags: [String!]
}

您可以从这个 tags 属性数组中弹出

单个 _POP 变更
mutation {
    updateMovies (update: { tags: { pop: 1 } }) {
        movies {
            title
            tags
        }
    }
}
之前 之后
tags: ['a', 'b', 'c']
tags: ['a', 'b']

或者,对于数组中的多个属性

两个 _POP 变更
mutation {
  updateMovies(update: { tags: { pop: 2 } }) {
    movies {
      title
      tags
    }
  }
}
之前 之后
tags: ['a', 'b', 'c']
tags: ['a']

同样,您可以拥有多个数组属性字段并在同一查询中更新它们

type Movie @node {
    title: String
    tags: [String!]
    moreTags: [String!]
}

然后,您可以从 tagsmoreTags 属性数组中弹出

从两个不同数组中 _POP 的变更
mutation {
  updateMovies(update: { tags: { pop: 1 }, moreTags: { pop: 2 } }) {
    movies {
      title
      tags
      moreTags
    }
  }
}
之前 之后
tags: ['a', 'b', 'c']
moreTags: ['x', 'y', 'z']
    tags: ['a', 'b']
    moreTags: ['x']

数学运算符

您可以使用数学运算符,根据数值字段的原始值在单个数据库事务中更新它们。为此,特定的运算符可用于不同的数值类型:IntFloatBigInt。它们在以下实体中受支持

  • 节点

  • 关系属性

  • 接口

对于 IntFloatBigInt 类型,以下运算符可用

  • add

  • subtract

对于 Float 类型,还提供以下附加运算符

  • multiply

  • divide

运算符仍然作为可选字段可用。如果在未定义字段中使用了数学运算符,则会导致 GraphQL 错误。

例如,考虑以下用于社交视频平台的 GraphQL schema

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
          }
        }
      }
    }
  }
}
© . All rights reserved.