Skip to content

Commit

Permalink
✅ Add test for arg with default value in GraphQL field to aggregation…
Browse files Browse the repository at this point in the history
… mapping
  • Loading branch information
ujibang committed Oct 27, 2023
1 parent 038c486 commit 2a2cc39
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "field-to-aggregation",
"enabled": true
},
"schema": "type Stat {_id: String posts: Int} type User { _id: ObjectId name: String email: String posts: [Post] postsByCategoryWithDataLoader: [BsonDocument] postsByCategory: [BsonDocument] emptyStage: [BsonDocument] malformedStage: [BsonDocument] } type Post { _id: ObjectId title: String body: String category: String created_at: DateTime author: User } type Query { userByEmail(email: String): User postsByCategory(category: String): [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] 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] }",
"mappings": {
"User": {
"postsByCategoryWithDataLoader": {
Expand Down Expand Up @@ -33,7 +33,7 @@
"caching": true
}
},
"postsByCategory": {
"postsGroupedByCategory": {
"db": "test-graphql",
"collection": "posts-fta",
"stages": [
Expand All @@ -54,6 +54,20 @@
}
]
},
"postsByCategory": {
"db": "test-graphql",
"collection": "posts-fta",
"stages": [
{
"$match": {
"author": {
"$fk": "_id"
},
"category": { "$arg": [ "category", "backend" ] }
}
}
]
},
"emptyStage": {
"db": "test-graphql",
"collection": "posts-fta",
Expand Down Expand Up @@ -92,7 +106,7 @@
}
}
},
"postsByCategory": {
"postsGroupedByCategory": {
"db": "test-graphql",
"collection": "posts-fta",
"find": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Scenario: Should return an array with two objects
userByEmail(email: "[email protected]"){
name
email
postsByCategory
postsGroupedByCategory
}
}
"""
Expand All @@ -37,7 +37,7 @@ Scenario: Should return an array with two objects
And request query
When method POST
Then status 200
And match $..postsByCategory.length() == 2
And match $..postsGroupedByCategory.length() == 2


Scenario: Should return an empty array for emptyStage field
Expand Down Expand Up @@ -94,7 +94,7 @@ Scenario: Get a list of posts by category and for each one return the author nam
* text query =
"""
{
postsByCategory(category: "backend") {
postsGroupedByCategory(category: backend) {
author {
name,
postsByCategoryWithDataLoader
Expand All @@ -109,8 +109,70 @@ 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
* text query =
"""
{
userByEmail(email: "[email protected]"){
name
email
postsByCategory(category: backend) {
body
}
}
}
"""

Given header Content-Type = contTypeGraphQL
And header Authorization = admin
And request query
When method POST
Then status 200
And match $.data.userByEmail.postsByCategory.length() == 2

Scenario: Get users with all posts in frontend category
* text query =
"""
{
userByEmail(email: "[email protected]"){
name
email
postsByCategory(category: frontend) {
body
}
}
}
"""

Given header Content-Type = contTypeGraphQL
And header Authorization = admin
And request query
When method POST
Then status 200
And match $.data.userByEmail.postsByCategory.length() == 1

Scenario: Get users with all posts in the default category (backend). this uses arg with default value
* text query =
"""
{
userByEmail(email: "[email protected]"){
name
email
postsByCategory {
body
}
}
}
"""

Given header Content-Type = contTypeGraphQL
And header Authorization = admin
And request query
When method POST
Then status 200
And match $.data.userByEmail.postsByCategory.length() == 2

Scenario: Map query field to aggregation to count the number of posts by category
Scenario: Map query field to aggregation to count the number of posts by category
* text query =
"""
{
Expand All @@ -125,4 +187,4 @@ Scenario: Map query field to aggregation to count the number of posts by categor
And header Authorization = admin
And request query
When method POST
Then status 200
Then status 200

0 comments on commit 2a2cc39

Please sign in to comment.