I am trying to pass an array of objects as a variable in a graphQL mutation. The variables are populated from a forEach
loop and pass the mutation to an axios function.
My code:
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
format from variables
:
"arr": [
{
"val1": "one",
"val2": {
"id": "349464"
}
},
{
"val1": "two",
"val2": {
"id": "374362"
}
},
]
Error message
"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
}
],
How do I fix this?