提交对象的数组作为graphQL查询变量(Submit an array of objects as

2019-09-29 08:51发布

我怎样才能构建查询参数的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
  }
}

谢谢!

Answer 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')) 


文章来源: Submit an array of objects as a query variable in graphQL