So I have this array this I'm trying to send to graphql in react...
[{…}]
0: {id: 1, customers_top_competitors: 'asdfasd', top_competitors_url: 'https://www.asdfasd.com/', __typename: 'TopCompetitors'}
length: 1
[[Prototype]]: Array(0)
Here's my query...
export const UPDATE_CUSTOMER_COMPETITORS = gql`
mutation($customer_id: Int, $data: [TopCompetitorsInput]) {
updateTopCompetitors(customer_id: $customer_id, data: $data)
}
`;
and my models...
input TopCompetitorsInput {
TopCompetitorsInputArray: [TopCompetitorsInputElement]
}
input TopCompetitorsInputElement {
id: Int
customers_top_competitors: String
top_competitors_url: String
}
No matter what I try graphql doesn't like the array's index. Comes back with this error...
react_devtools_backend.js:4026 [GraphQL error]: Message: Variable "$data" got invalid value
{ 0: { id: 1, customers_top_competitors: "asdfasd", top_competitors_url: "https://www.asdfasd.com/",
__typename: "TopCompetitors" } }; Field "0" is not defined by type TopCompetitorsInput., Location: [object Object], Path: undefined
Appreciate any guidance!
EDIT:
Per @Disco's request here's how I'm getting the array from the database
let { data: all_data } = useQuery(GET_TOP_COMPETITORS, {
skip: !state.customers?.selected?.id,
variables: { customer_id: state.customers?.selected?.id },
});
useEffect(() => {
setcompetitorData(all_data?.getTopCompetitors);
}, [all_data]);
and a user can add elements to the array...
const AddCompetitor = () =>
{
if(highestCompetitorID){
//competitorData.push({id: highestCompetitorID, customers_top_competitors: '', top_competitors_url: ''});
setcompetitorData((competitorData) => [...competitorData, {id: highestCompetitorID, customers_top_competitors: '', top_competitors_url: ''}])
sethighestCompetitorID(competitorData[competitorData.length - 1].id + 1)
}else {
//competitorData.push({id: 1, customers_top_competitors: '', top_competitors_url: ''});
setcompetitorData((competitorData) => [...competitorData, {id: 1, customers_top_competitors: '', top_competitors_url: ''}]);
sethighestCompetitorID(competitorData[competitorData.length - 1].id + 1)
}
}
EDIT 2: here is the result of console.log(JSON.stringify(competitorData));...
[{"id":1,"customers_top_competitors":"sdfasfdfasdfsd","top_competitors_url":"https://www.asdfdfasd.com/","__typename":"TopCompetitors"}]
EDIT 3: Played around with the graphql interface and tried the data like my react is sending. Plus have one that was successful.
Final Edit:
So I found a way to make my code work by passing the array elements 1 by 1. This can't be the best way. But maybe this will help someone else. Here's what worked...
<Button
color="orange"
type="submit"
onClick={() => {
for(let x = 0; x < competitorData?.length; x++)
{
updateCustomerCompetitors({
variables: {
customer_id: state.customers?.selected?.id,
data: omit(competitorData[x], ["__typename"])
}
})
}
}
}
>
Submit
</Button>


competitorDatathen you should not push to it directly. setcompetitorData((oldState) => [...oldState, {id: highestCompetitorID, customers_top_competitors: '', top_competitors_url: ''}]) Could just try to print the array before you pass it as data? Printing it as console.log(JSON.stringify(data)) might help in this case.__typename, try to omit that from the object. And as I mentioned below when the data is[{"id":1,"customers_top_competitors":"sdfasfdfasdfsd","top_competitors_url":"https://www.asdfdfasd.com/","__typename":"TopCompetitors"}]Should be of type$data: [TopCompetitorsInputElement]if you do not do anymore with the data