如何在Graphql中更新关系类型



我是Graphql的新手,我想更新关系类型,但我不知道如何做到这一点。

我的类型定义如下:

type User {
email: String!
username: String!
password: String!
registered_at: Date!
role: String!
Questionnaire: [Questionnaire!]! @relationship(type: "has_questionnaire", direction: OUT)
}
type Questionnaire {
User: [User!]! @relationship(type: "has_questionnaire", direction: IN)
id: ID!
ClosedQuestions: [Answer!]! @relationship(type: "has_answered", direction: OUT)
OpenQuestions: [BigInt]
}
type Answer {
Questionnaire: Questionnaire! @relationship(type: "has_answered", direction: IN)
id: ID!
component: String!
scope: String!
answer: String!
answered_at: Date!
}
  1. 如果我想从特定用户(用户名(的特定问卷(id(更新OpenQuestions列表,我将如何编写我的突变。我的尝试如下:
mutation {
updateUsers(
where: {
username: „Kilian“
id: „Project 1“      ##This id refers to the Questionnaire id
}
update: {
OpenQuestions: [2, 3]
}
) {
users {
Questionnaire {
OpenQuestions
}
}
}
}

/\但这不起作用。。。

  1. 更进一步/更深入:如果我想更新特定用户的特定问卷中的特定答案(id(,我将如何编写我的突变

非常感谢您的帮助!✌️

要更新嵌套属性,您需要在where输入中添加更新的数据并过滤这些属性

为了实现您想要的,您可以运行类似以下的查询:

mutation {
updateUsers(
where: {
username: "Kilian"
}
update: {
Questionnaire: {
where: { node: { id: "Project 1" } }
update: { node: { OpenQuestions: [2, 3] } }
}
}
) {
users {
Questionnaire {
OpenQuestions
}
}
}
}

在上面的查询中,我们只更新具有usernameKilian的用户。在这些用户中,我们正在更新关系Questionnaire。从这些相关节点中,我们只筛选id为Project 1的节点。从这些过滤的节点中,我们更新OpenQuestions属性。

注意,我们需要将node添加到嵌套的whereupdate中。这是因为我们可以使用edge更新关系属性(如果有的话(。

要更新特定的答案,您可以在嵌套更新中遵循相同的模式,例如:

...
update: { node: { 
OpenQuestions: [2, 3]
ClosedQuestions: {
where: { 
id: "answerId"
}
update: { node: { answer: "my updated answer" } }
} 
}
...

最新更新