簡體   English   中英

如何更新 Graphql 中的關系類型

[英]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!
}
  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
      }
    }
  }
}

在上面的查詢中,我們只更新用戶username Kilian 的用戶。 在這些用戶中,我們正在更新關系Questionnaire 從這些相關節點中,我們僅過濾 id 為Project 1的節點。 從這些過濾的節點中,我們更新OpenQuestions屬性。

請注意,我們需要將node添加到嵌套的whereupdate 這是因為我們可以使用edge更新關系屬性(如果有的話)。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM