在graphQL中提交对象数组作为查询变量 [英] Submit an array of objects as a query variable in graphQL

查看:425
本文介绍了在graphQL中提交对象数组作为查询变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I structure the query parameters a graphQL mutation when one of the fields is an array of objects? When I hard-code the mutation this is valid and works:

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

我一辈子都想不通如何为对象的响应数组传递查询参数.如何定义对象中每个字段的类型.这就是用户ID所需的,并且可以正常工作,但我无法弄清楚它的响应内容.

I can't for the life of me figure out how to pass in query parameters for the responses array of objects. How can I define the type of each field in the object. This is what it would like for the user id and that works fine but I can't figure out the responses piece of it.

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

谢谢!

推荐答案

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

I know that the question is old, but maybe someone else needs simple solution.

  1. responses的类型设置为String!

mutation submitResponse($user: ID!, $responses: String!){
  submitResponse(user: $user, responses: $responses) {
    id
  }
}

  • 然后:

  • Then:

    yourData = [{ answerId: "wmtBCWtkSeDs5meBe", selected: false}, { answerId: "wmtBCWtkSeDs5meBz", selected: true}]
    responses = JSON.stringify(yourData)
    

  • 对于我在BackEnd中的情况,我使用Python,因此在BackEnd中接收responses,然后将JSON(字符串)简单地转换为Python的字典,就像这样:

  • In my case in BackEnd I use Python, so receive responses in BackEnd and then simply convert JSON (string) to Python's dict, something like:

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

  • 这篇关于在graphQL中提交对象数组作为查询变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆