[英]How to update relational types in 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!
}
mutation {
updateUsers(
where: {
username: „Kilian“
id: „Project 1“ ##This id refers to the Questionnaire id
}
update: {
OpenQuestions: [2, 3]
}
) {
users {
Questionnaire {
OpenQuestions
}
}
}
}
/\ 但这不起作用...
非常感谢您的帮助! ✌️
要更新嵌套属性,您需要在where
输入中添加更新的数据并过滤这些属性
要实现您想要的,您可以运行类似于以下内容的查询:
mutation {
updateUsers(
where: {
username: "Kilian"
}
update: {
Questionnaire: {
where: { node: { id: "Project 1" } }
update: { node: { OpenQuestions: [2, 3] } }
}
}
) {
users {
Questionnaire {
OpenQuestions
}
}
}
}
在上面的查询中,我们只更新用户username
Kilian 的用户。 在这些用户中,我们正在更新关系Questionnaire
。 从这些相关节点中,我们仅过滤 id 为Project 1
的节点。 从这些过滤的节点中,我们更新OpenQuestions
属性。
请注意,我们需要将node
添加到嵌套的where
和update
。 这是因为我们可以使用edge
更新关系属性(如果有的话)。
要更新特定答案,您可以在嵌套更新中遵循相同的模式,例如:
...
update: { node: {
OpenQuestions: [2, 3]
ClosedQuestions: {
where: {
id: "answerId"
}
update: { node: { answer: "my updated answer" } }
}
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.