Skip to content

Commit

Permalink
work on alg
Browse files Browse the repository at this point in the history
  • Loading branch information
ValdemarGr committed Sep 2, 2023
1 parent 7cacc82 commit 0d6362b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,13 @@ class MySchema(pool: Resource[IO, Session[IO]]) {
"id" -> query(_.selId),
"age" -> query(_.selAge),
"height" -> query(_.selHeight),
"pets" -> queryAndThen[IO, Lambda[X => X], EntityTable2, UUID, List[QueryResult[PetTable]]](_.selId)(
"pets" -> cont { e =>
for {
pe <- petEntityTable.join[List](pe => sql"${pe.entityId} = ${e.id}".apply(Void))
p <- petTable.join(p => sql"${p.id} = ${pe.petId}".apply(Void))
} yield p
}
/*"pets" -> queryAndThen[IO, Lambda[X => X], EntityTable2, UUID, List[QueryResult[PetTable]]](_.selId)(
_.andThen(
resolveQuery(
EmptyableArg.Empty,
Expand All @@ -154,7 +160,7 @@ class MySchema(pool: Resource[IO, Session[IO]]) {
SkunkRunQuery(pool)
)
)
)
)*/
)

implicit lazy val entity: Type[IO, QueryResult[EntityTable]] = tpe[IO, QueryResult[EntityTable]](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ trait QueryAlgebra {
val decoder = (sel.decoder, done.dec).tupled
val qc2 = Interpreter.QueryContent(sel.cols ++ qc.selections, qc.joins)
val frag = Interpreter.renderQuery(qc2)
println(s"running ${frag.asInstanceOf[skunk.AppliedFragment].fragment}")
println(s"running:\n${frag.asInstanceOf[skunk.AppliedFragment].fragment.sql}\n")
val result = runQuery(frag, decoder)
result
.map{ xs => println(s"got ${xs.size} results"); xs }
.map(_.groupMap { case (k, _) => k } { case (_, v) => v })
.map(_.fmap(done.reassoc))
}
Expand Down
27 changes: 25 additions & 2 deletions modules/relational/src/main/scala/gql/relational/QueryDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import gql.Application
import natchez.TraceValue
import natchez.Kernel
import natchez.Span
import java.net.URI
import cats.arrow.FunctionK

trait QueryDsl extends QueryAlgebra {
Expand All @@ -39,12 +38,25 @@ trait QueryDsl extends QueryAlgebra {
tpe: => Out[F, G[B]]
): Field[F, QueryResult[A], G[B]] =
queryFull(EmptyableArg.Lift(a))((a, c) => f(a, c), Resolver.id[F, G[B]])(tpe)

/*
def queryAndThen[F[_], G[_], A, B, D](f: A => Query[G, Query.Select[B]])(g: Resolver[F, G[B], G[B]] => Resolver[F, G[B], D])(implicit
tpe: => Out[F, D]
): Field[F, QueryResult[A], D] =
queryFull(EmptyableArg.Empty)((a, _) => f(a), g(Resolver.id[F, G[B]]))(tpe)
def queryAndThen[F[_], G[_], A, B, C, D](
a: Arg[C]
)(f: (A, C) => Query[G, Query.Select[B]])(g: Resolver[F, G[B], G[B]] => Resolver[F, G[B], D])(implicit
tpe: => Out[F, D]
): Field[F, QueryResult[A], D] =
queryFull(EmptyableArg.Lift(a))((a, c) => f(a, c), g(Resolver.id[F, G[B]]))(tpe)*/

def queryAndThen[G[_], A, B](f: A => Query[G, Query.Select[B]]): PartiallyAppliedQueryAndThen[G, A, Unit, B, A] =
new PartiallyAppliedQueryAndThen[G, A, Unit, B, A](EmptyableArg.Empty, f, (a, _) => a)

def queryAndThen[G[_], A, B, C](a: Arg[C])(f: (A, C) => Query[G, Query.Select[B]]): PartiallyAppliedQueryAndThen[G, A, C, B, A] =
new PartiallyAppliedQueryAndThen[G, A, C, B, (A, C)](EmptyableArg.Lift(a), f, (a, c) => (a, c))

def queryAndThen[F[_], G[_], A, B, C, D](
a: Arg[C]
)(f: (A, C) => Query[G, Query.Select[B]])(g: Resolver[F, G[B], G[B]] => Resolver[F, G[B], D])(implicit
Expand Down Expand Up @@ -108,4 +120,15 @@ trait QueryDsl extends QueryAlgebra {
})(tpe)
.addAttributes(tfa)
}

final class PartiallyAppliedQueryAndThen[G[_], A, B, C, D](
ea: EmptyableArg[B],
g: D => Query[G, Query.Select[C]],
f: (A, B) => D
) {
def apply[F[_], E](h: Resolver[F, G[C], G[C]] => Resolver[F, G[C], E])(implicit
tpe: => Out[F, E]
): Field[F, QueryResult[A], E] =
queryFull[F, G, A, C, B, E](ea)((a, b) => g(f(a, b)), h(Resolver.id[F, G[C]]))(tpe)
}
}

0 comments on commit 0d6362b

Please sign in to comment.