[英]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.