Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.

Commit c5d9ada

Browse files
authored
Merge pull request #4485 from prisma/AggregationWithoutCursor
Distinguish between counting with pagination and without
2 parents 27a5658 + c2e8ba1 commit c5d9ada

2 files changed

Lines changed: 90 additions & 5 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ trait NodeManyQueries extends FilterConditionBuilder with AggregationQueryBuilde
114114
}
115115
}
116116

117-
//Fixme this does not use all queryarguments
118117
def countFromModel(model: Model, queryArguments: QueryArguments) = SimpleMongoAction { database =>
119118
if (needsAggregation(queryArguments.filter)) {
120119
aggregationQueryForId(database, model, queryArguments).map { x =>
121120
x.length match {
122-
case 0 => 0
123-
case x => x - 1 // we always fetch one more item for the page info we need to subtract that
121+
case 0 => 0
122+
case z if queryArguments.first.isDefined || queryArguments.last.isDefined => z - 1 // we fetch one more item than asked for for the page info
123+
case z => z
124124
}
125125
}
126126
} else {

server/servers/api/src/test/scala/com/prisma/api/queries/MultiItemConnectionQuerySpec.scala

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class MultiItemConnectionQuerySpec extends FlatSpec with Matchers with ApiSpecBa
111111
.toString should equal("""{"data":{"todoesConnection":{"edges":[{"node":{"title":"Hello World!"}}]}}}""")
112112
}
113113

114-
"the connection query" should "work when using cursors when not on Mongo" taggedAs (IgnoreSQLite) in {
114+
"the connection query" should "work when using cursors" taggedAs (IgnoreSQLite) in {
115115
val datamodels = {
116116
val dm1 =
117117
"""type User {
@@ -122,14 +122,22 @@ class MultiItemConnectionQuerySpec extends FlatSpec with Matchers with ApiSpecBa
122122
}"""
123123

124124
val dm2 =
125+
"""type User {
126+
id: ID! @id
127+
name: String
128+
following: [User!]! @relation(name: "UserToFollow")
129+
followers: [User!]! @relation(name: "UserToFollow", link: INLINE)
130+
}"""
131+
132+
val dm3 =
125133
"""type User {
126134
| id: ID! @id
127135
| name: String
128136
| following: [User!]! @relation(name: "UserToFollow", link: TABLE)
129137
| followers: [User!]! @relation(name: "UserToFollow")
130138
|}"""
131139

132-
TestDataModels(mongo = Vector(dm1), sql = Vector(dm2))
140+
TestDataModels(mongo = Vector(dm1, dm2), sql = Vector(dm3))
133141
}
134142
datamodels.testV11 { project =>
135143
val a = server.query(s"""mutation{createUser(data:{name: "a", followers:{create:[{name:"b"}, {name:"c"}, {name:"x"}]}}){id}}""", project)
@@ -162,4 +170,81 @@ class MultiItemConnectionQuerySpec extends FlatSpec with Matchers with ApiSpecBa
162170
}
163171
}
164172

173+
"the connection query" should "work when not using cursors" taggedAs (IgnoreSQLite) in {
174+
val datamodels = {
175+
val dm1 =
176+
"""type User {
177+
| id: ID! @id
178+
| name: String
179+
| company: Company @relation(link: INLINE)
180+
|}
181+
|
182+
|type Company {
183+
| id: ID! @id
184+
| name: String,
185+
| members: [User!]!
186+
|}"""
187+
188+
val dm2 =
189+
"""type User {
190+
| id: ID! @id
191+
| name: String
192+
| company: Company
193+
|}
194+
|
195+
|type Company {
196+
| id: ID! @id
197+
| name: String,
198+
| members: [User!]! @relation(link: INLINE)
199+
|}"""
200+
201+
val dm3 =
202+
"""type User {
203+
| id: ID! @id
204+
| name: String
205+
| company: Company @relation(link: TABLE)
206+
|}
207+
|
208+
|type Company {
209+
| id: ID! @id
210+
| name: String,
211+
| members: [User!]!
212+
|}"""
213+
214+
TestDataModels(mongo = Vector(dm1, dm2), sql = Vector(dm3))
215+
}
216+
217+
datamodels.testV11 { project =>
218+
val a = server.query(s"""mutation{createUser(data:{name: "a", company:{create:{name:"b"}}}){id, company{id}}}""", project)
219+
val d = server.query(s"""mutation{createUser(data:{name: "d", company:{create:{name:"e"}}}){id, company{id}}}""", project)
220+
val g = server.query(s"""mutation{createUser(data:{name: "g", company:{create:{name:"h"}}}){id, company{id}}}""", project)
221+
val k = server.query(s"""mutation{createUser(data:{name: "k", company:{create:{name:"l"}}}){id, company{id}}}""", project)
222+
223+
val result = server.query(
224+
s"""{
225+
| usersConnection(where: {
226+
| company: {
227+
| id: "${a.pathAsString("data.createUser.company.id")}"
228+
| }
229+
| }) {
230+
| edges {
231+
| node {
232+
| name
233+
| company {
234+
| name
235+
| }
236+
| }
237+
| }
238+
| aggregate {
239+
| count
240+
| }
241+
| }
242+
|}""".stripMargin,
243+
project
244+
)
245+
246+
result.toString should be("""{"data":{"usersConnection":{"edges":[{"node":{"name":"a","company":{"name":"b"}}}],"aggregate":{"count":1}}}}""")
247+
}
248+
}
249+
165250
}

0 commit comments

Comments
 (0)