diff --git a/core/src/test/java/karate/graphql/field-to-aggregation/app-definition-field-to-aggregation.json b/core/src/test/java/karate/graphql/field-to-aggregation/app-definition-field-to-aggregation.json index bb74961260..3e2f61fdba 100644 --- a/core/src/test/java/karate/graphql/field-to-aggregation/app-definition-field-to-aggregation.json +++ b/core/src/test/java/karate/graphql/field-to-aggregation/app-definition-field-to-aggregation.json @@ -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": { @@ -33,7 +33,7 @@ "caching": true } }, - "postsByCategory": { + "postsGroupedByCategory": { "db": "test-graphql", "collection": "posts-fta", "stages": [ @@ -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", @@ -92,7 +106,7 @@ } } }, - "postsByCategory": { + "postsGroupedByCategory": { "db": "test-graphql", "collection": "posts-fta", "find": { diff --git a/core/src/test/java/karate/graphql/field-to-aggregation/field-to-aggregation.feature b/core/src/test/java/karate/graphql/field-to-aggregation/field-to-aggregation.feature index 76c4bb7cdd..c23219a6bf 100644 --- a/core/src/test/java/karate/graphql/field-to-aggregation/field-to-aggregation.feature +++ b/core/src/test/java/karate/graphql/field-to-aggregation/field-to-aggregation.feature @@ -27,7 +27,7 @@ Scenario: Should return an array with two objects userByEmail(email: "foo@example.com"){ name email - postsByCategory + postsGroupedByCategory } } """ @@ -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 @@ -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 @@ -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: "foo@example.com"){ + 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: "foo@example.com"){ + 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: "foo@example.com"){ + 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 = """ { @@ -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 \ No newline at end of file + Then status 200 \ No newline at end of file