-
Notifications
You must be signed in to change notification settings - Fork 3
/
Compiler.scala
134 lines (122 loc) · 4.8 KB
/
Compiler.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright 2022 Valdemar Grange
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gql
import cats._
import cats.implicits._
import io.circe._
import cats.effect._
import gql.parser.{QueryParser => P}
import gql.interpreter.Interpreter
import cats.data._
import gql.parser.QueryParser
sealed trait CompilationError {
def asGraphQL: JsonObject
}
object CompilationError {
final case class Parse(error: gql.parser.ParseError) extends CompilationError {
lazy val asGraphQL: JsonObject = JsonObject("errors" -> Json.arr(Json.fromJsonObject(error.asGraphQL)))
}
final case class Preparation(error: NonEmptyChain[gql.PreparedQuery.PositionalError]) extends CompilationError {
lazy val asGraphQL: JsonObject = JsonObject("errors" -> Json.arr(error.map(x => Json.fromJsonObject(x.asGraphQL)).toList: _*))
}
}
sealed trait Application[F[_]] {
def mapK[G[_]](f: F ~> G): Application[G]
}
object Application {
final case class Query[F[_]](run: F[QueryResult]) extends Application[F] {
override def mapK[G[_]](f: F ~> G): Query[G] = Query(f(run))
}
final case class Mutation[F[_]](run: F[QueryResult]) extends Application[F] {
override def mapK[G[_]](f: F ~> G): Mutation[G] = Mutation(f(run))
}
final case class Subscription[F[_]](run: fs2.Stream[F, QueryResult]) extends Application[F] {
override def mapK[G[_]](f: F ~> G): Subscription[G] = Subscription(run.translate(f))
}
}
final case class CompilerParameters(
query: String,
variables: Option[Map[String, Json]],
operationName: Option[String]
)
object Compiler { outer =>
type Outcome[F[_]] = Either[CompilationError, Application[F]]
final class PartiallyAppliedCompiler[F[_]](val F: Async[F]) extends AnyVal {
implicit def F0: Async[F] = F
def compile[Q, M, S](
schema: Schema[F, Q, M, S],
query: String,
variables: Map[String, Json] = Map.empty,
operationName: Option[String] = None,
queryInput: F[Q] = F.unit,
mutationInput: F[M] = F.unit,
subscriptionInput: F[S] = F.unit
) = compileWith(schema, CompilerParameters(query, Some(variables), operationName), queryInput, mutationInput, subscriptionInput)
def compileWith[Q, M, S](
schema: Schema[F, Q, M, S],
cp: CompilerParameters,
queryInput: F[Q] = F.unit,
mutationInput: F[M] = F.unit,
subscriptionInput: F[S] = F.unit
): Outcome[F] =
parsePrep(schema, cp)
.map { case (ot, pfs) => compilePrepared(schema, ot, pfs, queryInput, mutationInput, subscriptionInput) }
def parsePrep(
schema: Schema[F, ?, ?, ?],
cp: CompilerParameters
): Either[CompilationError, (QueryParser.OperationType, NonEmptyList[PreparedQuery.PreparedField[F, Any]])] =
gql.parser.parse(cp.query) match {
case Left(pe) => Left(CompilationError.Parse(pe))
case Right(q) =>
PreparedQuery.prepare(q, schema, cp.variables.getOrElse(Map.empty), cp.operationName) match {
case Left(pe) => Left(CompilationError.Preparation(pe))
case Right(x) => Right(x)
}
}
def compilePrepared[Q, M, S](
schema: Schema[F, Q, M, S],
operationType: P.OperationType,
ps: NonEmptyList[PreparedQuery.PreparedField[F, Any]],
queryInput: F[Q] = F.unit,
mutationInput: F[M] = F.unit,
subscriptionInput: F[S] = F.unit
) = {
implicit val s = schema.statistics
implicit val p = schema.planner
operationType match {
case P.OperationType.Query =>
Application.Query {
queryInput.flatMap { qi =>
Interpreter.runSync(qi, ps, schema.state).map { case (e, d) => QueryResult(e, d) }
}
}
case P.OperationType.Mutation =>
Application.Mutation {
mutationInput.flatMap { mi =>
Interpreter.runSync(mi, ps, schema.state).map { case (e, d) => QueryResult(e, d) }
}
}
case P.OperationType.Subscription =>
Application.Subscription {
fs2.Stream.eval(subscriptionInput).flatMap { si =>
Interpreter.runStreamed(si, ps, schema.state).map { case (e, d) => QueryResult(e, d) }
}
}
}
}
}
def apply[F[_]](implicit F: Async[F]): PartiallyAppliedCompiler[F] = new PartiallyAppliedCompiler[F](F)
}