0

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?

2
  • According to the error message, the correct input format is [{"val1": "one", "val2_id": "349464"}, {"val1": "two", "val2_id": "374362"}]
    – Bergi
    Commented May 12, 2023 at 5:39
  • 2
    Please post the schema that defines createField and ArrInput if you need further help with this
    – Bergi
    Commented May 12, 2023 at 5:39

0