Create, update or delete
Say you run a fantastic movie community like The Movie Database. You have already implemented some basic features (see Basic Queries) and even some complex ones (see Complex queries). You only ran queries on data until now.
Now you want to create, update and delete a reaction.
Create movie review
- GraphQL request
- Response
mutation CreateMovieReview {
createReview(
data: { targetType: MOVIE, targetIdentifier: "550-fight-club", content: "I love this movie", rating: 5, language: "en" }
) {
content
rating
}
}
{
"data": {
"createReview": {
"content": "I love this movie",
"rating": 5
}
}
}
The user is logged in through one of our Authentication options.
Things we do behind the scenes:
- Check that the user is logged in and has sufficient privileges to create a review
- Check that the user already wrote a review on the same movie
- Check that the movie exists by using
targetType
andtargetIdentifier
to identify the movie through the external API - Check that the content is not void and the rating is not null
- Check that the rating is between 1 and 5
Once all these rules have been followed, you can finally create the review in the database.
Update movie review
Now the user wishes to update the review.
- GraphQL request
- Response
mutation UpdateMovieReview {
updateReview(
where: { id: "a75ae96d-5afe-4a2c-a1b9-2addc948d803" }
data: { content: "I do not love this movie anymore", rating: 1 }
) {
id
content
rating
}
}
{
"data": {
"updateReview": {
"id": "a75ae96d-5afe-4a2c-a1b9-2addc948d803",
"content": "I do not love this movie anymore",
"rating": 1
}
}
}
Things we do behind the scenes:
- Check that the user has sufficient privileges to update his or her review
- Check that the content is not void and the rating is not null
- Check that the rating is between 1 and 5
Delete movie review
Now the user wishes to delete the review.
- GraphQL request
- Response
mutation DeleteMovieReview {
deleteReview(where: { id: "a75ae96d-5afe-4a2c-a1b9-2addc948d803" }) {
id
}
}
{
"data": {
"deleteReview": {
"id": "a75ae96d-5afe-4a2c-a1b9-2addc948d803"
}
}
}
Things we do behind the scenes:
- Check that the user has sufficient privileges to delete his or her review