Skip to content

Commit

Permalink
✅ Add test for optional stages in GraphQL field to aggregation mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
ujibang committed Oct 27, 2023
1 parent 2a2cc39 commit fcc6d09
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "field-to-aggregation",
"enabled": true
},
"schema": "enum Category { frontend, backend } type Stat {_id: String posts: Int} type User { _id: ObjectId name: String email: String posts: [Post] postsByCategoryWithDataLoader: [BsonDocument] postsGroupedByCategory: [BsonDocument] postsByCategory(category: Category): [Post] emptyStage: [BsonDocument] malformedStage: [BsonDocument] } type Post { _id: ObjectId title: String body: String category: Category created_at: DateTime author: User } type Query { userByEmail(email: String): User postsGroupedByCategory(category: Category): [Post] countPostsByCategory: [Stat] }",
"schema": "enum Category { frontend, backend } type Stat {_id: String posts: Int} type User { _id: ObjectId name: String email: String posts: [Post] postsByCategoryWithDataLoader: [BsonDocument] postsGroupedByCategory: [BsonDocument] postsByCategoryAggr(category: Category): [Post] postsByCategoryQuery(category: Category): [Post] emptyStage: [BsonDocument] malformedStage: [BsonDocument] } type Post { _id: ObjectId title: String body: String category: Category created_at: DateTime author: User flag: Boolean } type Query { userByEmail(email: String): User postsGroupedByCategory(category: Category): [Post] countPostsByCategory: [Stat] }",
"mappings": {
"User": {
"postsByCategoryWithDataLoader": {
Expand Down Expand Up @@ -54,20 +54,22 @@
}
]
},
"postsByCategory": {
"postsByCategoryAggr": {
"db": "test-graphql",
"collection": "posts-fta",
"stages": [
{
"$match": {
"author": {
"$fk": "_id"
},
"category": { "$arg": [ "category", "backend" ] }
}
}
{ "$match": { "author": { "$fk": "_id" }, "category": { "$arg": [ "category", "backend" ] } } },
{ "$ifarg": [ "category", { "$addFields": { "flag": true} }, { "$addFields": { "flag": false } } ] }
]
},
"postsByCategoryQuery": {
"db": "test-graphql",
"collection": "posts-fta",
"find": {
"author": { "$fk": "_id" },
"category": { "$arg": [ "category", "backend" ] }
}
},
"emptyStage": {
"db": "test-graphql",
"collection": "posts-fta",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,18 @@ Scenario: Get a list of posts by category and for each one return the author nam
When method POST
Then status 200

Scenario: Get users with all posts in backend category
Scenario: Get users with all posts in backend category. Uses arg with default value and optional stage.
* text query =
"""
{
userByEmail(email: "[email protected]"){
name
email
postsByCategory(category: backend) {
postsByCategoryAggr(category: backend) {
body
flag
}
postsByCategoryQuery(category: backend) {
body
}
}
Expand All @@ -128,16 +132,22 @@ Scenario: Get users with all posts in backend category
And request query
When method POST
Then status 200
And match $.data.userByEmail.postsByCategory.length() == 2
And match $.data.userByEmail.postsByCategoryAggr.length() == 2
And match $.data.userByEmail.postsByCategoryAggr[*].flag == [true,true]
And match $.data.userByEmail.postsByCategoryQuery.length() == 2

Scenario: Get users with all posts in frontend category
Scenario: Get users with all posts in frontend category. Uses arg with default value and optional stage.
* text query =
"""
{
userByEmail(email: "[email protected]"){
name
email
postsByCategory(category: frontend) {
postsByCategoryAggr(category: frontend) {
body
flag
}
postsByCategoryQuery(category: frontend) {
body
}
}
Expand All @@ -149,16 +159,22 @@ Scenario: Get users with all posts in frontend category
And request query
When method POST
Then status 200
And match $.data.userByEmail.postsByCategory.length() == 1
And match $.data.userByEmail.postsByCategoryAggr.length() == 1
And match $.data.userByEmail.postsByCategoryAggr[*].flag == [true]
And match $.data.userByEmail.postsByCategoryQuery.length() == 1

Scenario: Get users with all posts in the default category (backend). this uses arg with default value
Scenario: Get users with all posts in the default category (backend). Uses arg with default value and optional stage.
* text query =
"""
{
userByEmail(email: "[email protected]"){
name
email
postsByCategory {
postsByCategoryAggr {
body
flag
}
postsByCategoryQuery {
body
}
}
Expand All @@ -170,7 +186,9 @@ Scenario: Get users with all posts in the default category (backend). this uses
And request query
When method POST
Then status 200
And match $.data.userByEmail.postsByCategory.length() == 2
And match $.data.userByEmail.postsByCategoryAggr.length() == 2
And match $.data.userByEmail.postsByCategoryAggr[*].flag == [false, false]
And match $.data.userByEmail.postsByCategoryQuery.length() == 2

Scenario: Map query field to aggregation to count the number of posts by category
* text query =
Expand Down

0 comments on commit fcc6d09

Please sign in to comment.