Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
ValdemarGr committed Mar 26, 2024
1 parent a7897e4 commit 0feb9ad
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 28 deletions.
16 changes: 8 additions & 8 deletions modules/core/src/main/scala/gql/SchemaShape.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ object SchemaShape {
type Implementations[F[_]] = Map[String, Map[String, InterfaceImpl[F, ?]]]

final case class DiscoveryState[F[_]](
toplevels: Map[String, Toplevel[F, ?]],
toplevels: Map[String, Toplevel[F]],
implementations: Implementations[F],
positions: Map[String, List[QueryPosition[F, ?]]]
) {
lazy val inputs: Map[String, InToplevel[?]] = toplevels.collect { case (k, v: InToplevel[?]) => k -> v }

lazy val outputs: Map[String, OutToplevel[F, ?]] = toplevels.collect { case (k, v: OutToplevel[F, ?]) => k -> v }

def addToplevel(name: String, tl: Toplevel[F, ?]): DiscoveryState[F] =
def addToplevel(name: String, tl: Toplevel[F]): DiscoveryState[F] =
copy(toplevels = toplevels + (name -> tl))

def addImplementation(name: String, impl: Map[String, InterfaceImpl[F, ?]]): DiscoveryState[F] =
Expand Down Expand Up @@ -175,7 +175,7 @@ object SchemaShape {

def runPf(vn: VisitNode[F]) = pf.lift(vn).getOrElse[G[A] => G[A]](ga => ga)

def nextIfNotSeen(tl: Toplevel[F, ?])(ha: => H[A]): H[A] =
def nextIfNotSeen(tl: Toplevel[F])(ha: => H[A]): H[A] =
L.ask[Set[String]].flatMap { seen =>
if (seen.contains(tl.name)) H.pure(M.empty)
else L.local(ha)(_ + tl.name)
Expand Down Expand Up @@ -266,7 +266,7 @@ object SchemaShape {
val H = Monad[H]
implicit lazy val parForState: Parallel[H] = Parallel.identity[H]

def nextIfNotSeen(tl: Toplevel[F, ?])(ha: => H[A]): H[A] =
def nextIfNotSeen(tl: Toplevel[F])(ha: => H[A]): H[A] =
S.get.flatMap { seen =>
if (seen.contains(tl.name)) H.pure(A.empty)
else S.modify(_ + tl.name) >> ha
Expand Down Expand Up @@ -377,7 +377,7 @@ object SchemaShape {
o + Doc.hardLine
}

def renderModifierStack[G[_]](ms: ModifierStack[Toplevel[G, ?]]) =
def renderModifierStack[G[_]](ms: ModifierStack[Toplevel[G]]) =
ms.modifiers.foldLeft(Doc.text(ms.inner.name)) {
case (accum, Modifier.List) => accum.tightBracketBy(Doc.char('['), Doc.char(']'))
case (accum, Modifier.NonNull) => accum + Doc.char('!')
Expand Down Expand Up @@ -541,18 +541,18 @@ object SchemaShape {
)

sealed trait TypeInfo extends Product with Serializable {
def asToplevel: Option[Toplevel[F, ?]]
def asToplevel: Option[Toplevel[F]]
def next: Option[TypeInfo]
}
sealed trait InnerTypeInfo extends TypeInfo {
def next: Option[TypeInfo] = None
}
object TypeInfo {
final case class OutInfo(t: OutToplevel[F, ?]) extends InnerTypeInfo {
def asToplevel: Option[Toplevel[F, ?]] = Some(t)
def asToplevel: Option[Toplevel[F]] = Some(t)
}
final case class InInfo(t: InToplevel[?]) extends InnerTypeInfo {
def asToplevel: Option[Toplevel[F, ?]] = Some(t)
def asToplevel: Option[Toplevel[F]] = Some(t)
}

final case class NEModifierStack(modifiers: NonEmptyList[Modifier], inner: InnerTypeInfo) extends TypeInfo {
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/gql/SchemaState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import gql.resolver.Step.BatchKey
final case class SchemaState[F[_]](
nextId: Int,
batchFunctions: Map[BatchKey[?, ?], SchemaState.BatchFunction[F, ?, ?]],
positions: List[Position[F, ?]]
positions: List[QueryPosition[F, ?]]
)

object SchemaState {
Expand Down
6 changes: 3 additions & 3 deletions modules/core/src/main/scala/gql/ast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ object ast extends AstImplicits.Implicits {

sealed trait In[A]

sealed trait Toplevel[+F[_], +A] {
sealed trait Toplevel[+F[_]] {
def name: String
def description: Option[String]
}

sealed trait OutToplevel[+F[_], A] extends Out[F, A] with Toplevel[F, A]
sealed trait OutToplevel[+F[_], A] extends Out[F, A] with Toplevel[F]

sealed trait InToplevel[A] extends In[A] with Toplevel[fs2.Pure, A]
sealed trait InToplevel[A] extends In[A] with Toplevel[fs2.Pure]

sealed trait Selectable[+F[_], A] extends OutToplevel[F, A] {
def anyFields: List[(String, AnyField[F, ?, ?])]
Expand Down
14 changes: 1 addition & 13 deletions modules/core/src/main/scala/gql/dsl/DirectiveDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,10 @@ trait DirectiveDsl[F[_]] {
handler: Position.QueryHandler[QA.InlineFragment, A]
): State[SchemaState[F], Position.InlineFragmentSpread[A]] =
DirectiveDsl.onInlineFragmentSpread(directive, handler)

def onEnum[A](
directive: Directive[A],
handler: Position.PureHandler[A, ast.Enum]
): State[SchemaState[F], Position.Schema.Enum[A]] =
DirectiveDsl.onEnum(directive, handler)
}

trait DirectiveDslFull {
protected def addPosition[F[_], A, Pos <: Position[F, A]](pos: Pos): State[SchemaState[F], Pos] =
protected def addPosition[F[_], A, Pos <: QueryPosition[F, A]](pos: Pos): State[SchemaState[F], Pos] =
State(s => (s.copy(positions = pos :: s.positions), pos))

def directive(name: String): Directive[Unit] =
Expand All @@ -72,12 +66,6 @@ trait DirectiveDslFull {
handler: Position.QueryHandler[QA.InlineFragment, A]
): State[SchemaState[F], Position.InlineFragmentSpread[A]] =
addPosition[F, A, Position.InlineFragmentSpread[A]](Position.InlineFragmentSpread(directive, handler))

def onEnum[F[_], A](
directive: Directive[A],
handler: Position.PureHandler[A, ast.Enum]
): State[SchemaState[F], Position.Schema.Enum[A]] =
addPosition[F, A, Position.Schema.Enum[A]](Position.Schema(directive, handler))
}

object DirectiveDsl extends DirectiveDslFull {
Expand Down
29 changes: 26 additions & 3 deletions modules/core/src/main/scala/gql/preparation/DirectiveAlg.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import cats._
import cats.implicits._
import gql._
import scala.reflect.ClassTag
import gql.ast._

class DirectiveAlg[F[_], C](
positions: Map[String, List[Position[F, ?]]],
Expand Down Expand Up @@ -52,7 +53,7 @@ class DirectiveAlg[F[_], C](
directives.map(_.nel.toList).getOrElse(Nil).parTraverse { d =>
getDirective[P](d.name, context)(pf).flatMap { p =>
// rigid type variable inference help
def go[A](p: P[A]): G[ParsedDirective[A,P]] =
def go[A](p: P[A]): G[ParsedDirective[A, P]] =
parseArg(
p,
d.arguments.map(_.nel.toList).getOrElse(Nil).map(a => a.name -> a.value.map(List(_))),
Expand All @@ -63,6 +64,16 @@ class DirectiveAlg[F[_], C](
}
}

def parseSchema[P[x] <: SchemaPosition[F, x]](
sd: SchemaDirective[F, P],
ctx: List[C]
): G[ParsedDirective[?, P]] = {
parseArg(sd.position, sd.args.map{ case (k, v) => k -> v.map(_ => List.empty[C]) }, ctx).map{ a =>
// unfortunate
ParsedDirective(sd.position.asInstanceOf[P[Any]], a)
}
}

def parseProvidedSubtype[P[x] <: Position[F, x]](
directives: Option[QueryAst.Directives[C, AnyValue]],
context: List[C]
Expand All @@ -76,13 +87,25 @@ class DirectiveAlg[F[_], C](

def parseSchemaDirective[P[x] <: SchemaPosition[F, x]](sd: ast.SchemaDirective[F, P], context: List[C]): G[ParsedDirective[?, P]] = {
// rigid type variable inference help
def go[A](p: P[A]): G[ParsedDirective[A, P]] =
parseArg(p, sd.args.map{ case (k, v) => k -> v.map(_ => List.empty[C])}, context)
def go[A](p: P[A]): G[ParsedDirective[A, P]] =
parseArg(p, sd.args.map { case (k, v) => k -> v.map(_ => List.empty[C]) }, context)
.map(ParsedDirective(p, _))

go(sd.position: P[?]).widen[ParsedDirective[?, P]]
}

def field[I, O](fi: MergedFieldInfo[F, C], field: Field[F, I, O]): G[List[(Field[F, I, _], MergedFieldInfo[F, C])]] =
(
parseProvidedSubtype[Position.Field[F, *]](fi.directives, List(fi.caret))
).flatMap{ xs =>
val ys = /*field.directives.map(sd => ) ++*/ xs
ys.foldLeftM[G, List[(Field[F, I, ?], MergedFieldInfo[F, C])]](List((field, fi))) { case (xs, prov) =>
xs.parFlatTraverse { case (f: Field[F, I, ?], fi) =>
G.raiseEither(prov.p.handler(prov.a, f, fi), List(fi.caret))
}
}
}

def foldDirectives[P[x] <: Position[F, x]]: DirectiveAlg.PartiallyAppliedFold[F, C, P] =
new DirectiveAlg.PartiallyAppliedFold[F, C, P] {
override def apply[H[_]: Traverse, A](directives: Option[QueryAst.Directives[C, AnyValue]], context: List[C])(base: A)(
Expand Down

0 comments on commit 0feb9ad

Please sign in to comment.