更新

考虑以下类型定义

type Post {
    id: ID! @id
    content: String!
    creator: User! @relationship(type: "HAS_POST", direction: IN)
}

type User {
    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
        connect: PostConnectInput
        disconnect: PostDisconnectInput
        create: PostCreateInput
        delete: PostDeleteInput
    ): UpdatePostsMutationResponse!
    updateUsers(
        where: UserWhere
        update: UserUpdateInput
        connect: UserConnectInput
        disconnect: UserDisconnectInput
        create: UserCreateInput
        delete: UserDeleteInput
    ): UpdateUsersMutationResponse!
}

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

单个 update

您可以通过执行以下 GraphQL 语句来更新 Post 的内容

mutation {
    updatePosts(
        where: {
            id: "892CC104-A228-4BB3-8640-6ADC9F2C2A5F"
        }
        update: {
            content: "Some new content for this Post!"
        }
    ) {
        posts {
            content
        }
    }
}

这将更新帖子,添加句子“Some new content for this Post!”。

使用 update 嵌套 create

您可以通过更新 User 并将 Post 作为变异的一部分进行 create 来更新 User,而不是使用 create 变异创建 Post 然后将其连接到 User

mutation {
    updateUsers(
        where: { name: "John Doe" }
        create: {
            posts: [
                { 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: "1234" } }
        onCreate: { node: { title: "Forrest Gump" } }
      }
    }
  }) {
    info {
      nodesCreated
    }
  }
}

对于 update 操作,您也可以使用 connectOrCreate 作为顶级输入来执行等效操作

mutation {
  updateActors(
      connectOrCreate: {
        movies: {
            where: { node: { id: "1234" } }
            onCreate: { node: { title: "Forrest Gump" } }
        }
      },
      where: { name: "Tom Hanks" }
  ) {
    info {
      nodesCreated
    }
  }
}

此操作等效于前面的示例。

数组方法

数组方法允许在这些实体内的 update 变异中修改现有的属性数组

  • 节点

  • 关系属性

  • 接口

为此,以下运算符可用

  • _PUSH

  • _POP

_PUSH

_PUSH 符合类型定义中定义的输入类型。

请考虑以下类型定义,一个名为 tagsMovie,它具有一个名为 String 的属性数组

type Movie {
    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 {
    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 值。

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

type Movie {
    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 {
    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。它们在这些实体内受支持

  • 节点

  • 关系属性

  • 接口

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

  • _INCREMENT

  • _DECREMENT

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

  • _ADD

  • _SUBTRACT

  • _MULTIPLY

  • _DIVIDE

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

例如,请考虑以下针对社交视频平台的 GraphQL 架构

type Video {
  id: ID @id
  views: Int
  ownedBy: User @relationship(type: "OWN_VIDEO", properties: "OwnVideo", direction: IN)
}

type User {
  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: "VideoID" }
    update: { views_INCREMENT: 1 }
  ) {
    videos {
      id
      views
    }
  }
}

现在,假设您希望社交平台因观看视频而奖励用户 0.01 美元。为此,您必须更新关系属性 revenue

mutation addRevenueMutation {
  updateUsers(
    where: { id: "UserID" },
    update: { ownVideo: [{ update: { edge: { revenue_ADD: 0.01 } } }] }
  ) {
    users {
      id
      ownVideoConnection {
        edges {
          revenue
        }
      }
    }
  }
}