Skip to content

Commit 6cc1588

Browse files
committed
reestablish order after using find to fetch the items the aggregationquery returned in the correct order
1 parent ab8153a commit 6cc1588

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

server/connectors/api-connector-mongo/src/main/scala/com/prisma/api/connector/mongo/database/AggregationQueryBuilder.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ trait AggregationQueryBuilder extends FilterConditionBuilder with ProjectionBuil
2626
selectedFields: SelectedFields,
2727
rowValueOpt: Option[GCValue]): Future[Seq[Document]] = {
2828
aggregationQueryForId(database, model, queryArguments, rowValueOpt).flatMap { ids =>
29-
val inFilter = in("_id", ids.map(GCToBson(_)): _*)
30-
database.getCollection(model.dbName).find(inFilter).projection(projectSelected(selectedFields)).toFuture
29+
val bsonIds = ids.map(GCToBson(_))
30+
database.getCollection(model.dbName).find(in("_id", bsonIds: _*)).projection(projectSelected(selectedFields)).toFuture.map { seq =>
31+
bsonIds.map(id => seq.find(doc => doc.get("_id").get == id).get) //sort according to ids ordering
32+
}
3133
}
3234
}
3335

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.prisma.api.queries
2+
3+
import com.prisma.api.ApiSpecBase
4+
import com.prisma.shared.schema_dsl.SchemaDsl
5+
import org.scalatest.{FlatSpec, Matchers}
6+
7+
class RelationFilterOrderingSpec extends FlatSpec with Matchers with ApiSpecBase {
8+
9+
"Using relational filters" should "return items in the specified order" in {
10+
11+
val project = SchemaDsl.fromStringV11() {
12+
"""type Blog {
13+
| id: ID! @id
14+
| title: String!
15+
| score: Int!
16+
| labels: [Label!]! @relation(name: "BlogLabels", link: INLINE)
17+
|}
18+
|type Label {
19+
| id: ID! @id
20+
| text: String! @unique
21+
|}"""
22+
}
23+
24+
database.setup(project)
25+
26+
server.query(s"""mutation {createLabel(data: {text: "x"}) {text }}""", project)
27+
28+
server.query(s"""mutation {createBlog(data: {title: "blog_1", score: 10,labels: {connect: {text: "x"}}}) {title}}""", project)
29+
server.query(s"""mutation {createBlog(data: {title: "blog_1", score: 20,labels: {connect: {text: "x"}}}) {title}}""", project)
30+
server.query(s"""mutation {createBlog(data: {title: "blog_1", score: 30,labels: {connect: {text: "x"}}}) {title}}""", project)
31+
32+
val res1 = server.query("""query {blogs(first: 2, orderBy: score_DESC) {title, score}}""", project)
33+
34+
res1.toString should be("""{"data":{"blogs":[{"title":"blog_1","score":30},{"title":"blog_1","score":20}]}}""")
35+
36+
val res2 = server.query("""query {blogs (first: 2, orderBy: score_DESC, where:{labels_some: {text: "x"}}) {title, score}}""", project)
37+
res2.toString should be("""{"data":{"blogs":[{"title":"blog_1","score":30},{"title":"blog_1","score":20}]}}""")
38+
39+
}
40+
}

0 commit comments

Comments
 (0)