Skip to content

Commit

Permalink
make UUIDs work as cursor by converting cursor arguments to GCValues
Browse files Browse the repository at this point in the history
  • Loading branch information
mavilein committed Oct 22, 2018
1 parent 88e9815 commit c689be8
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ trait CursorConditionBuilder extends BuilderBase {

// Then, we select the comparison operation and construct the cursors. For instance, if we use ascending order, and we want
// to get the items before, we use the "<" comparator on the column that defines the order.
def cursorFor(cursor: String, cursorType: String): Condition = (cursorType, sortDirection.toLowerCase.trim) match {
def cursorFor(cursorType: String): Condition = (cursorType, sortDirection.toLowerCase.trim) match {
case ("before", "asc") => row(orderByFieldWithAlias, idFieldWithAlias).lessThan(selectQuery, stringDummy)
case ("before", "desc") => row(orderByFieldWithAlias, idFieldWithAlias).greaterThan(selectQuery, stringDummy)
case ("after", "asc") => row(orderByFieldWithAlias, idFieldWithAlias).greaterThan(selectQuery, stringDummy)
case ("after", "desc") => row(orderByFieldWithAlias, idFieldWithAlias).lessThan(selectQuery, stringDummy)
case _ => throw new IllegalArgumentException
}

val afterCursorFilter = after.map(cursorFor(_, "after")).getOrElse(noCondition())
val beforeCursorFilter = before.map(cursorFor(_, "before")).getOrElse(noCondition())
val afterCursorFilter = after.map(_ => cursorFor("after")).getOrElse(noCondition())
val beforeCursorFilter = before.map(_ => cursorFor("before")).getOrElse(noCondition())

afterCursorFilter.and(beforeCursorFilter)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ object SetParams extends SlickExtensions with LimitClauseBuilder {

def setCursor(pp: PositionedParameters, queryArguments: QueryArguments): Unit = {
queryArguments.after.foreach { value =>
pp.setString(value)
pp.setString(value)
pp.setGcValue(value)
pp.setGcValue(value)
}
queryArguments.before.foreach { value =>
pp.setString(value)
pp.setString(value)
pp.setGcValue(value)
pp.setGcValue(value)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.prisma.api.connector.mongo.database
import com.mongodb.client.model.Filters
import com.prisma.api.connector._
import com.prisma.api.connector.mongo.extensions.HackforTrue.hackForTrue
import com.prisma.gc_values.CuidGCValue
import org.mongodb.scala.bson.conversions

object CursorConditionBuilder {
Expand Down Expand Up @@ -31,8 +32,8 @@ object CursorConditionBuilder {
case _ => throw new IllegalArgumentException
}

val afterCursorCondition = after.map(cursorCondition(_, "after")).getOrElse(hackForTrue)
val beforeCursorCondition = before.map(cursorCondition(_, "before")).getOrElse(hackForTrue)
val afterCursorCondition = after.map(_.asInstanceOf[CuidGCValue].value).map(cursorCondition(_, "after")).getOrElse(hackForTrue)
val beforeCursorCondition = before.map(_.asInstanceOf[CuidGCValue].value).map(cursorCondition(_, "before")).getOrElse(hackForTrue)

Filters.and(afterCursorCondition, beforeCursorCondition)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ object ResolverResult {

case class QueryArguments(
skip: Option[Int],
after: Option[String],
after: Option[IdGCValue],
first: Option[Int],
before: Option[String],
before: Option[IdGCValue],
last: Option[Int],
filter: Option[Filter],
orderBy: Option[OrderBy]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.prisma.api.resolver.{IdBasedConnection, IdBasedConnectionDefinition}
import com.prisma.api.schema.CustomScalarTypes.{DateTimeType, JsonType, UUIDType}
import com.prisma.gc_values._
import com.prisma.shared.models
import com.prisma.shared.models.TypeIdentifier.{Cuid, UUID}
import com.prisma.shared.models.{Field => _, _}
import com.prisma.util.coolArgs.GCAnyConverter
import sangria.schema.{Field => SangriaField, _}
Expand Down Expand Up @@ -255,12 +256,20 @@ class ObjectTypeBuilder(
}

private def extractQueryArgumentsFromContext(model: Model, ctx: Context[_, Unit], isSubscriptionFilter: Boolean): Option[QueryArguments] = {
def convertCursorToGcValue(s: String) = {
model.idField_!.typeIdentifier match {
case TypeIdentifier.Cuid => CuidGCValue(s)
case TypeIdentifier.UUID => UuidGCValue.parse_!(s)
case TypeIdentifier.Int => IntGCValue(s.toInt)
case x => sys.error(s"This must not happen. $x is not a valid type identifier for an id field.")
}
}
val rawFilterOpt: Option[Map[String, Any]] = ctx.argOpt[Map[String, Any]]("where")
val filterOpt = rawFilterOpt.map(generateFilterElement(_, model, isSubscriptionFilter))
val skipOpt = ctx.argOpt[Int]("skip")
val orderByOpt = ctx.argOpt[OrderBy]("orderBy")
val afterOpt = ctx.argOpt[String](IdBasedConnection.Args.After.name)
val beforeOpt = ctx.argOpt[String](IdBasedConnection.Args.Before.name)
val afterOpt = ctx.argOpt[String](IdBasedConnection.Args.After.name).map(convertCursorToGcValue)
val beforeOpt = ctx.argOpt[String](IdBasedConnection.Args.Before.name).map(convertCursorToGcValue)
val firstOpt = ctx.argOpt[Int](IdBasedConnection.Args.First.name)
val lastOpt = ctx.argOpt[Int](IdBasedConnection.Args.Last.name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ trait NonEmbeddedPaginationSpec extends FlatSpec with Matchers with ApiSpecBase
val result1 = server.query(
"""
|{
| lists {
| lists(orderBy: createdAt_ASC) {
| name
| todos(first: 3, orderBy: createdAt_ASC){
| title
Expand Down

0 comments on commit c689be8

Please sign in to comment.