当其中一个字段是对象数组时,如何将查询参数构造为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
}
}
谢谢!
答案 0 :(得分:0)
我知道这个问题很老,但也许其他人需要简单的解决方案。
将responses
的类型设置为String!
mutation submitResponse($user: ID!, $responses: String!){
submitResponse(user: $user, responses: $responses) {
id
}
}
然后:
yourData = [{ answerId: "wmtBCWtkSeDs5meBe", selected: false}, { answerId: "wmtBCWtkSeDs5meBz", selected: true}]
responses = JSON.stringify(yourData)
在我的BackEnd案例中,我使用Python,因此在BackEnd中接收responses
然后只需将JSON(字符串)转换为Python的dict,如:
import json
responses = json.loads(input.get('responses'))