【问题标题】:Submit an array of objects as a query variable in graphQL在 graphQL 中提交对象数组作为查询变量
【发布时间】:2018-02-05 04:56:21
【问题描述】:

当其中一个字段是对象数组时,如何将查询参数构造为 graphQL 突变?当我对突变进行硬编码时,这是有效且有效的:

mutation{
  submitResponse(user: "1234", responses: [{ answerId: "wmtBCWtkSeDs5meBe", selected: false}, { answerId: "wmtBCWtkSeDs5meBz", selected: true}]) {
    id
  }
}

我终其一生都无法弄清楚如何为响应对象数组传递查询参数。如何定义对象中每个字段的类型。这就是它想要的用户 ID 并且工作正常,但我无法弄清楚它的响应部分。

mutation submitResponse($user: ID!){
  submitResponse(user: $user, responses: [{ answerId: "wmtBCWtkSeDs5meBe", selected: false}, { answerId: "wmtBCWtkSeDs5meBz", selected: true}]) {
    id
  }
}

谢谢!

【问题讨论】:

    标签: graphql graphql-js


    【解决方案1】:

    我知道这个问题很老,但也许其他人需要简单的解决方案。

    1. responses的类型设置为String!

      mutation submitResponse($user: ID!, $responses: String!){
        submitResponse(user: $user, responses: $responses) {
          id
        }
      }
      
    2. 然后:

      yourData = [{ answerId: "wmtBCWtkSeDs5meBe", selected: false}, { answerId: "wmtBCWtkSeDs5meBz", selected: true}]
      responses = JSON.stringify(yourData)
      
    3. 在后端我使用 Python,所以在后端接收 responses,然后简单地将 JSON(字符串)转换为 Python 的字典,类似于:

      import json
      responses = json.loads(input.get('responses'))
      

    【讨论】:

      猜你喜欢
      • 2019-07-29
      • 2023-03-19
      • 2017-03-31
      • 2020-01-22
      • 1970-01-01
      • 2018-08-07
      • 2019-05-17
      • 2023-03-12
      • 2019-07-14
      相关资源
      最近更新 更多