Skip to content

Commit

Permalink
Add delete batch mutation (#151)
Browse files Browse the repository at this point in the history
* Add delete batch mutation
* Update tests to cover this feature
  • Loading branch information
karatakis authored Dec 14, 2023
1 parent 812ba6c commit 43aff8e
Show file tree
Hide file tree
Showing 10 changed files with 473 additions and 44 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

* add `update_mutation`

This module enabled the update mutation for entities. The update mutation takes an entity data object with a filter condition object,
This module enables the update mutation for entities. The update mutation takes an entity data object with a filter condition object,
applies the update to the database and returns the modified entities.


* add `delete_mutation`

This module enables the delete mutation for entities. The delete mutation takes an entity condition filter object,
deletes the selected entities from database and returns the number of deleted items.

## 1.0.2 - Pending

* add `create_one_mutation`
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* Order by any column
* Guard fields, queries or relations
* Rename fields
* Mutations (create, update, delete)

(Right now there is no mutation, but it's on our plan!)

Expand Down
88 changes: 88 additions & 0 deletions examples/mysql/tests/mutation_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,3 +532,91 @@ async fn test_update_mutation() {
"#,
);
}

#[tokio::test]
async fn test_delete_mutation() {
let schema = get_schema().await;

assert_eq(
schema
.execute(
r#"
{
filmText(filters: { filmId: { gte: 998 } }, orderBy: { filmId: ASC }) {
nodes {
filmId
title
}
}
}
"#,
)
.await,
r#"
{
"filmText": {
"nodes": [
{
"filmId": 998,
"title": "ZHIVAGO CORE"
},
{
"filmId": 999,
"title": "ZOOLANDER FICTION"
},
{
"filmId": 1000,
"title": "ZORRO ARK"
}
]
}
}
"#,
);

assert_eq(
schema
.execute(
r#"
mutation {
filmTextDelete(filter: { filmId: { gte: 999 } })
}
"#,
)
.await,
r#"
{
"filmTextDelete": 2
}
"#,
);

assert_eq(
schema
.execute(
r#"
{
filmText(filters: { filmId: { gte: 998 } }, orderBy: { filmId: ASC }) {
nodes {
filmId
title
}
}
}
"#,
)
.await,
r#"
{
"filmText": {
"nodes": [
{
"filmId": 998,
"title": "ZHIVAGO CORE"
}
]
}
}
"#,
);
}
160 changes: 144 additions & 16 deletions examples/postgres/tests/mutation_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ async fn test_create_batch_mutation() {
.execute(
r#"
{
language {
language(filters: { languageId: { lte: 8 } }, orderBy: { languageId: ASC }) {
nodes {
languageId
name
Expand Down Expand Up @@ -269,11 +269,10 @@ async fn test_create_batch_mutation() {
mutation {
languageCreateBatch(
data: [
{ languageId: 7, name: "Swedish", lastUpdate: "2030-01-12 21:50:05" }
{ languageId: 8, name: "Danish", lastUpdate: "2030-01-12 21:50:05" }
{ languageId: 1, name: "Swedish", lastUpdate: "2030-01-12 21:50:05" }
{ languageId: 1, name: "Danish", lastUpdate: "2030-01-12 21:50:05" }
]
) {
languageId
name
}
}
Expand All @@ -284,11 +283,9 @@ async fn test_create_batch_mutation() {
{
"languageCreateBatch": [
{
"languageId": 7,
"name": "Swedish "
},
{
"languageId": 8,
"name": "Danish "
}
]
Expand All @@ -301,9 +298,8 @@ async fn test_create_batch_mutation() {
.execute(
r#"
{
language {
language(filters: { languageId: { lte: 8 } }, orderBy: { languageId: ASC }) {
nodes {
languageId
name
}
}
Expand All @@ -317,35 +313,27 @@ async fn test_create_batch_mutation() {
"language": {
"nodes": [
{
"languageId": 1,
"name": "English "
},
{
"languageId": 2,
"name": "Italian "
},
{
"languageId": 3,
"name": "Japanese "
},
{
"languageId": 4,
"name": "Mandarin "
},
{
"languageId": 5,
"name": "French "
},
{
"languageId": 6,
"name": "German "
},
{
"languageId": 7,
"name": "Swedish "
},
{
"languageId": 8,
"name": "Danish "
}
]
Expand Down Expand Up @@ -501,3 +489,143 @@ async fn test_update_mutation() {
"#,
);
}

#[tokio::test]
async fn test_delete_mutation() {
let schema = get_schema().await;

assert_eq(
schema
.execute(
r#"
{
language(filters: { languageId: { gte: 9 } }, orderBy: { languageId: ASC }) {
nodes {
languageId
}
}
}
"#,
)
.await,
r#"
{
"language": {
"nodes": []
}
}
"#,
);

assert_eq(
schema
.execute(
r#"
mutation {
languageCreateBatch(
data: [
{ languageId: 9, name: "9", lastUpdate: "2030-01-12 21:50:05" }
{ languageId: 10, name: "10", lastUpdate: "2030-01-12 21:50:05" }
{ languageId: 11, name: "11", lastUpdate: "2030-01-12 21:50:05" }
]
) {
languageId
}
}
"#,
)
.await,
r#"
{
"languageCreateBatch": [
{
"languageId": 9
},
{
"languageId": 10
},
{
"languageId": 11
}
]
}
"#,
);

assert_eq(
schema
.execute(
r#"
{
language(filters: { languageId: { gte: 9 } }, orderBy: { languageId: ASC }) {
nodes {
languageId
}
}
}
"#,
)
.await,
r#"
{
"language": {
"nodes": [
{
"languageId": 9
},
{
"languageId": 10
},
{
"languageId": 11
}
]
}
}
"#,
);

assert_eq(
schema
.execute(
r#"
mutation {
languageDelete(filter: { languageId: { gte: 10 } })
}
"#,
)
.await,
r#"
{
"languageDelete": 2
}
"#,
);

assert_eq(
schema
.execute(
r#"
{
language(filters: { languageId: { gte: 9 } }, orderBy: { languageId: ASC }) {
nodes {
languageId
}
}
}
"#,
)
.await,
r#"
{
"language": {
"nodes": [
{
"languageId": 9
}
]
}
}
"#,
);
}
Loading

0 comments on commit 43aff8e

Please sign in to comment.