Skip to content

Commit

Permalink
wok
Browse files Browse the repository at this point in the history
  • Loading branch information
ValdemarGr committed Aug 18, 2023
1 parent 0b21515 commit d41fdf4
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 54 deletions.
4 changes: 2 additions & 2 deletions modules/core/src/main/scala/gql/dsl/FieldDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ trait FieldDslFull {

def lift[I] = new FieldDsl.PartiallyAppliedLift[I]

def build[F[_], I] = new FieldBuilder[F, I]
def build[F[_], I] = new FieldBuilder[F, I] {}

def builder[F[_], I] = new FieldDsl.PartiallyAppliedFieldBuilder[F, I]

Expand Down Expand Up @@ -149,7 +149,7 @@ object FieldDsl extends FieldDslFull {
}
}

final class FieldBuilder[F[_], I](private val dummy: Boolean = false) extends AnyVal {
trait FieldBuilder[F[_], I] {
def tpe(
name: String,
hd: (String, Field[F, I, ?]),
Expand Down
78 changes: 64 additions & 14 deletions modules/relational/src/main/scala/gql/relational/ExampleImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import gql.Arg
import gql.EmptyableArg
import skunk.codec.all._
import cats.data._
import gql.resolver.Resolver

object SkunkSchema extends QueryAlgebra with QueryDsl {
type Frag = skunk.AppliedFragment
Expand All @@ -26,23 +27,23 @@ object SkunkSchema extends QueryAlgebra with QueryDsl {
type Decoder[A] = skunk.Decoder[A]
def optDecoder[A](d: Decoder[A]): Decoder[Option[A]] = d.opt

def runQuery[F[_]: Monad, G[_], I, B, ArgType](ses: Session[F], toplevelArg: EmptyableArg[ArgType], q: (I, ArgType) => Query[G, B]) = {
def runQuery[F[_]: MonadCancelThrow, G[_], I, B, ArgType](pool: Resource[F, Session[F]], toplevelArg: EmptyableArg[ArgType], q: (I, ArgType) => Query[G, B]) = {
resolveQuery[F, G, I, B, ArgType](toplevelArg, q).evalMap { case (qc, d: Interpreter.Done[G, a, QueryResult[B]]) =>
val af = Interpreter.renderQuery(qc)
println(af.fragment.sql)
val out: F[List[a]] = ses.execute(af.fragment.query(d.dec))(af.argument)
val out: F[List[a]] = pool.use(_.execute(af.fragment.query(d.dec))(af.argument))

out.map{ x => println(x.size);x}.map(xs => d.reassoc(xs).toIor)
}.rethrow
}

def runField[F[_]: Monad, G[_], I, B, ArgType](ses: Session[F], arg: Arg[ArgType])(q: (I, ArgType) => Query[G, B])(implicit
def runField[F[_]: MonadCancelThrow, G[_], I, B, ArgType](pool: Resource[F, Session[F]], arg: Arg[ArgType])(q: (I, ArgType) => Query[G, B])(implicit
tpe: => Out[F, G[QueryResult[B]]]
) = Field(runQuery(ses, EmptyableArg.Lift(arg), q), Eval.later(tpe))
) = Field(runQuery(pool, EmptyableArg.Lift(arg), q), Eval.later(tpe))

def runField[F[_]: Monad, G[_], I, B](ses: Session[F])(q: I => Query[G, B])(implicit
def runField[F[_]: MonadCancelThrow, G[_], I, B](pool: Resource[F, Session[F]])(q: I => Query[G, B])(implicit
tpe: => Out[F, G[QueryResult[B]]]
) = Field(runQuery[F, G, I, B, Unit](ses, EmptyableArg.Empty, (i, _) => q(i)), Eval.later(tpe))
) = Field(runQuery[F, G, I, B, Unit](pool, EmptyableArg.Empty, (i, _) => q(i)), Eval.later(tpe))

trait SkunkTable[A] extends Table[A] {
def aliased(x: Fragment[Void]): Fragment[Void] =
Expand All @@ -55,7 +56,7 @@ object SkunkSchema extends QueryAlgebra with QueryDsl {
}
}

object MySchema {
class MySchema(pool: Resource[IO, Session[IO]]) {
import SkunkSchema._

case class EntityTable(alias: String) extends SkunkTable[UUID] {
Expand All @@ -80,10 +81,10 @@ object MySchema {
}
val contractTable = table(ContractTable)

case class ContractEntityTable(alias: String) extends SkunkTable[(UUID, UUID)] {
case class ContractEntityTable(alias: String) extends SkunkTable[UUID] {
def table = void"contract_entity"
def groupingKey = void"contract_id, entity_id"
def groupingKeyDecoder: Decoder[(UUID, UUID)] = (uuid ~ uuid)
def groupingKey = void"contract_id"
def groupingKeyDecoder: Decoder[UUID] = uuid

val (contractId, selContractId) = sel("contract_id", uuid)
val (entityId, selEntityId) = sel("entity_id", uuid)
Expand All @@ -103,7 +104,15 @@ object MySchema {
}
val entityTable2 = table(EntityTable2)

implicit lazy val entity: Type[IO, QueryResult[EntityTable2]] = tpe[IO, QueryResult[EntityTable2]](
implicit lazy val entity2: Type[IO, QueryResult[EntityTable2]] = tpe[IO, QueryResult[EntityTable2]](
"Entity",
"name" -> query(_.selName),
"id" -> query(_.selId),
"age" -> query(_.selAge),
"height" -> query(_.selHeight)
)

implicit lazy val entity: Type[IO, QueryResult[EntityTable]] = tpe[IO, QueryResult[EntityTable]](
"Entity",
"name" -> query(_.selName),
"id" -> query(_.selId),
Expand All @@ -128,9 +137,50 @@ object MySchema {
*/
implicit lazy val contract: Type[IO, QueryResult[ContractTable]] = tpe[IO, QueryResult[ContractTable]](
"Contract",
"name" -> query(_.selName),
"name" -> query(c => (c.selName, c.selId).mapN(_ + _.toString())),
"id" -> query(_.selId),
"entities" -> cont(
"fastEntities" -> queryAndThen[IO, Lambda[X => X], ContractTable, UUID, List[QueryResult[EntityTable2]]]{ c =>
//contractEntityTable.join[List](cet => sql"${c.id} = ${cet.contractId}".apply(Void)).map(_.selEntityId)
c.selId
} { (xs: Resolver[IO, UUID, UUID]) =>
val res = resolveQuery[IO, List, UUID, EntityTable2, Unit](EmptyableArg.Empty, { (i, _) =>
entityTable2.join[List](e => sql"${e.contractId} = ${uuid}".apply(i))
})
type K = (UUID, Interpreter.QueryContent, Interpreter.Done[List, _, QueryResult[EntityTable2]])
type V = List[QueryResult[EntityTable2]]
val ilb = Resolver.inlineBatch[IO, (UUID, Interpreter.QueryContent, Interpreter.Done[List, _, QueryResult[EntityTable2]]), V]{ (ys: Set[K]) =>
ys.toList.headOption.traverse{ case (_, qc, d: Interpreter.Done[List, a, QueryResult[EntityTable2]]) =>
val af = Interpreter.renderQuery(qc.copy(selections = Chain(void"t1.contract_id") ++ qc.selections))

val done2 = Interpreter.Done[Lambda[X => Map[UUID, List[X]]], (UUID, a), QueryResult[EntityTable2]](
uuid ~ d.dec,
xs =>
xs.groupMap{ case (k, _) => k }{ case (_, v) => v }.toList.traverse{ case (k, vs) =>
d.reassoc(vs) tupleLeft k
}.map(_.toMap)
)

println(af.fragment.sql)
val out = pool.use(_.execute(af.fragment.query(done2.dec))(af.argument))

out
.map{ x => println(x.size);x}.map(xs => done2.reassoc(xs).toIor)
.map{ x =>
x.toEither.toOption.get.map{ case (k, v) => (k, qc, d) -> v }
}
}.map(_.get.asInstanceOf[Map[K, V]])
}

val prepped = res.tupleIn.map{ case ((qc, d), id) =>
Set((id, qc, d).asInstanceOf[K]): Set[K]
}

val combined = prepped.andThen(ilb).map(_.map{ case ((k, _, _), vs) => k -> vs })
.tupleIn.map{ case (outs, id) => println(outs); outs(id) }

combined
},/*
"fastEntities" -> cont(
arg[Option[List[String]]]("entityNames")
) { (c, ens) =>
entityTable2.join[List] { e =>
Expand All @@ -146,7 +196,7 @@ object MySchema {
// sql"${e.id} = ${cet.entityId}${extra.fragment} and ${c.parent.parent.pk} is not null".apply(extra.argument)
// }
// } yield e
}
}*/
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ trait QueryAlgebra {
ys |+| nl |+| stringToFrag("where ") |+| hd.pred
}

stringToFrag("select ") |+| selections |+| nl |+| suffix
stringToFrag("select ") |+| selections |+| suffix
}

case class Done[G[_], A, B](
Expand Down
76 changes: 39 additions & 37 deletions modules/relational/src/main/scala/gql/relational/a.scala
Original file line number Diff line number Diff line change
Expand Up @@ -622,74 +622,76 @@ object Test7 {
override def traceId: IO[Option[String]] = IO.pure(None)
override def traceUri: IO[Option[URI]] = IO.pure(None)
}
Session
.single[IO](
host = "127.0.0.1",
user = "postgres",
database = "postgres",
password = "1234".some
val pool =
Session
.single[IO](
host = "127.0.0.1",
user = "postgres",
database = "postgres",
password = "1234".some
)

val xaPool = pool.flatMap(ses => ses.transaction as ses)

import gql.relational.MySchema
import gql.relational.SkunkSchema
val ms = new MySchema(xaPool)
import ms._
val ss = SchemaShape.unit[IO](
fields[IO, Unit](
"name" -> lift(_ => "edlav"),
"contract" -> SkunkSchema.runField(xaPool, arg[UUID]("contractId"))((_: Unit, a: UUID) =>
ms.contractTable.join[Option](c => sql"${c.id} = ${uuid}".apply(a))
)(implicitly, gql.ast.gqlOutForOption(ms.contract))
)
.use { ses =>
ses.transaction.surround {
import gql.relational.MySchema
import gql.relational.SkunkSchema
val ss = SchemaShape.unit[IO](
fields[IO, Unit](
"name" -> lift(_ => "edlav"),
"contract" -> SkunkSchema.runField(ses, arg[UUID]("contractId"))(
(_: Unit, a: UUID) => MySchema.contractTable.join[Option](c => sql"${c.id} = ${uuid}".apply(a))
)
)
)
)

Schema.simple(ss).flatMap { schema =>
gql
.Compiler[IO]
.compile(
schema,
"""
Schema.simple(ss).flatMap { schema =>
gql
.Compiler[IO]
.compile(
schema,
"""
query {
contract(contractId: "1ff0ca77-c13f-4af8-9166-72373f309247") {
name
id
entities(entityNames: ["Jane"]) {
fastEntities {
name
age
}
entities2: entities(entityNames: ["Jane"]) {
fe2: fastEntities {
name
age
}
entities3: entities(entityNames: ["Jane"]) {
fe3: fastEntities {
name
age
}
entities4: entities(entityNames: ["Jane"]) {
fe4: fastEntities {
name
age
}
entities5: entities(entityNames: ["Jane"]) {
fe5: fastEntities {
name
age
}
entities6: entities(entityNames: ["Jane"]) {
fe6: fastEntities {
name
age
}
entities7: entities(entityNames: ["Jane"]) {
fe7: fastEntities {
name
age
}
}
}
"""
) match {
case Right(Application.Query(run)) => run.flatMap(IO.println)
case x => IO.println(x)
}
}
}
) match {
case Right(Application.Query(run)) => run.flatMap(IO.println)
case x => IO.println(x)
}
}
}

case class EntityTable(alias: Fragment[Void]) extends Table[UUID] {
Expand Down
5 changes: 5 additions & 0 deletions setup-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
docker stop gql-postgres || true && docker rm gql-postgres || true
docker run --name gql-postgres --rm -d -p 5432:5432 -e POSTGRES_PASSWORD=1234 postgres
sleep 1
cat db.sql | docker exec -i gql-postgres psql -Upostgres

0 comments on commit d41fdf4

Please sign in to comment.