在 graphQL 中将对象数组作为变量传递时出错

Error when passing array of objects as variable in graphQL

提问人:nb_nb_nb 提问时间:5/12/2023 更新时间:5/12/2023 访问量:118

问:

我正在尝试将对象数组作为 graphQL 突变中的变量传递。变量从循环中填充,并将突变传递给公理函数。forEach

我的代码:

const query = "mutation myMutation($id: ID!, $label: String, $arr: [ArrInput]) {
  createField(input: {
    id: $id,
    label: $label,
    arr: $arr,
  }) {
    table_field {
      id
      label
    }
  }
}";

result.data.forEach(d=> {
  const variables = {
    label: d.label,
    arr: d.arr,
  };
  axiosFunc({ query, variables }).then((response) => response);
});

arr格式从:variables

"arr": [
        {
            "val1": "one",
            "val2": {
                "id": "349464"
            }
        },
        {
            "val1": "two",
            "val2": {
                "id": "374362"
            }
        },
]

错误信息

"errors": [
        {
            "message": "Variable $arr of type [ArrInput] was provided invalid value for 0.val2 (Field is not defined on ArrInput), 0.val2_id (Expected value to not be null), 1.val2 (Field is not defined on ArrInput), 1.val2_id (Expected value to not be null)
            "locations": [
                {
                    "line": 2,
                    "column": 185
                }
            ],

我该如何解决这个问题?

JavaScript 数组 对象 ecmascript-6 graphql

评论

0赞 Bergi 5/12/2023
根据错误消息,正确的输入格式是[{"val1": "one", "val2_id": "349464"}, {"val1": "two", "val2_id": "374362"}]
2赞 Bergi 5/12/2023
请发布定义的架构,如果您需要进一步的帮助createFieldArrInput

答: 暂无答案