Update one to many field with Graphql in GraphCMS
Currently I am working on a migration from a Django based website to a replace it with GraphCMS. As we have lots of entries in our database a manuel migration is not a feasible option and would lead to errors during the migration process. Therefore, I want to transfer the data between the two systems with standardized graphql / api calls.
Problem
The challenge I had was the following: In django we have a one-to-many field but I could not find the appropriate mutation wich updates the field correctly. You can do something like this (Please not that you have to customize the command for you GraphCMS schema):
mutation MyMutation {
__typename
updateYourModel(data: {onetomanyfield: {set: {id: "xyz"}}}, where: {id: "xyz"}) {
id
}
}
but the problem is that you have afterwards just a one-to-one relationship.
Solution
What you want to do instead is the following:
- First Mutation
mutation MyMutation {
__typename
updateYourModel(data: {onetomanyfield: {connect: {where: {id: "xyz"}}}}, where: {id: "xyz"}) {
id
}
}
- Second Mutation
mutation MyMutation {
__typename
updateYourModel(data: {onetomanyfield: {connect: {where: {id: "xyzabs"}}}}, where: {id: "xyz"}) {
id
}
}
The difference between the first mutation and second is the changed id field: xyzabs
Example