Upsert mutations allow you to perform add or update operations based on whether a particular ID exists in the database.
add or update operations based on
whether a particular ID exists in the database. The IDs must be external IDs,
defined using the @id directive in the schema.
For example, to demonstrate how upserts work in GraphQL, take the following
schema:
add mutation, as shown below:
text field of a post with the ID mm2. But you
also want to create a new post with that ID in case it doesnโt already exist. To
do this, you use the addPost mutation, but with an additional input variable
upsert.
This is a Boolean variable. Setting it to true results in an upsert
operation.
It performs an update mutation and carry out the changes you specify in your
request if the particular ID exists. Otherwise, it falls back to a default add
operation and create a new Post with that ID and the details you provide.
Setting upsert to false is the same as using a plain add operationโitโll
either fail or succeed, depending on whether the ID exists or not.
Example: add mutation with upsert true:
mm2 exists, it updates the post with the new details.
Otherwise, itโll create a new Post with that ID and the values you provided.
In either case, youโll get the following response back:
upsert is false.Add and Update mutations is such that they donโt
update deep level nodes. So Add mutations with upsert set to true only
updates values at the root level.