Mutation to add a relation field fail as Bad request - What I am doing wrong?

System Information
  • Strapi Version: 4.8.2
  • Plugin-graphql: 4.8.2
  • Operating System: Windows 11
  • Database:
  • Node Version: 18.2
  • NPM Version: 8.19.2
  • Yarn Version:

I am building comments for articles (articlefeedback Collection Type). I am able to add the users using the Strapi Users inteface, however, I am trying to replicate the same behavior with my Frontend application and the Mutation to add the relation field fail as Bad request. What I am doing wrong? Thanks in advance for your help.
Using the STRAPI Interface

Mutation Fail

mutation updateArticlefeedback($id: ID!, $usrliked: ID!) {
updateArticlefeedback(id: $id, data: { usrliked: { data: { attributes: { username: $usrliked }}}} ) {
data {
id
attributes {
liked,
nlikes,
usrliked {
data {
id,
attributes {
username
}
}
}
}
}
}
}

Are you sure userliked is ID type?

1 Like

@viac92 Thanks! Based on your question, I realized that I have to pass “usrliked” as an Array. If I send the Mutation with no “usrliked” parameters the record get inserted with an empty array. Now, I am trying to figure out how to pass an array in the query variables. I have tried with $usrliked: Array! but GraphQL respond with error “Unknown type “Array”.” - The error message change if I define $usrliked as Int!, or ID! or String! → “message”: “Variable “usrliked” of type \Int!” used in position expecting type “[ID]”." - Do you know how to pass an array in Query Variables?
Check the errors


Check the errors

Check the errors

Check the errors

Thanks in advance for your help!

I’m not sure but try to set the userliked var like this userliked: [ID] or with ! if is non-nullable.

1 Like

Thanks @viac92 you were right!!
1

Thanks a million!11