繁体   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