-
Notifications
You must be signed in to change notification settings - Fork 3
/
ast.scala
354 lines (281 loc) · 13.3 KB
/
ast.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* Copyright 2023 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.implicits._
import io.circe._
import cats._
import cats.data._
import gql.resolver._
import java.util.UUID
import gql.parser.{Value => V, Const}
/** A tree-like structure representing a GraphQL schema, akin to most schema builders.
*/
object ast extends AstImplicits.Implicits {
sealed trait Out[+F[_], A]
sealed trait In[A]
sealed trait Toplevel[+F[_], +A] {
def name: String
def description: Option[String]
}
sealed trait OutToplevel[+F[_], A] extends Out[F, A] with Toplevel[F, A]
sealed trait InToplevel[A] extends In[A] with Toplevel[fs2.Pure, A]
sealed trait Selectable[+F[_], A] extends OutToplevel[F, A] {
def abstractFields: List[(String, AbstractField[F, ?])]
def abstractFieldMap: Map[String, AbstractField[F, ?]]
}
sealed trait ObjectLike[+F[_], A] extends Selectable[F, A] {
def implementsMap: Map[String, Eval[Interface[F, ?]]]
def abstractFieldsNel: NonEmptyList[(String, AbstractField[F, ?])]
}
final case class Implementation[+F[_], A, B](implementation: Eval[Interface[F, B]])(implicit
val specify: B => Option[A]
)
final case class Type[+F[_], A](
name: String,
fields: NonEmptyList[(String, Field[F, A, ?])],
implementations: List[Implementation[F, A, ?]],
description: Option[String] = None
) extends ObjectLike[F, A] {
def document(description: String): Type[F, A] = copy(description = Some(description))
lazy val fieldsList: List[(String, Field[F, A, ?])] = fields.toList
lazy val fieldMap = fields.toNem.toSortedMap.toMap
lazy val concreteFields = fieldsList
lazy val concreteFieldsMap = fieldMap
lazy val implementsMap = implementations.map(i => i.implementation.value.name -> i.implementation).toMap
lazy val abstractFieldsNel = fields.map { case (k, v) => k -> v.asAbstract }
lazy val abstractFields: List[(String, AbstractField[F, ?])] = abstractFieldsNel.toList
lazy val abstractFieldMap: Map[String, AbstractField[F, ?]] = abstractFields.toMap
}
final case class Input[A](
name: String,
fields: Arg[A],
description: Option[String] = None
) extends InToplevel[A] {
def document(description: String): Input[A] = copy(description = Some(description))
}
final case class Variant[+F[_], A, B](tpe: Eval[Type[F, B]])(implicit val specify: A => Option[B]) {
def contramap[C](g: C => A): Variant[F, C, B] =
Variant[F, C, B](tpe)(c => specify(g(c)))
}
final case class Union[+F[_], A](
name: String,
types: NonEmptyList[Variant[F, A, ?]],
description: Option[String] = None
) extends Selectable[F, A] {
def document(description: String): Union[F, A] = copy(description = Some(description))
def contramap[B](f: B => A): Union[F, B] =
Union(name, types.map(_.contramap(f)), description)
lazy val instanceMap = types.map(i => i.tpe.value.name -> i).toList.toMap
lazy val fieldMap = Map.empty
lazy val fieldsList: List[(String, Field[F, A, ?])] = Nil
lazy val abstractFields = Nil
lazy val abstractFieldMap = Map.empty
}
final case class Interface[+F[_], A](
name: String,
fields: NonEmptyList[(String, AnyField[F, A, ?])],
implementations: List[Eval[Interface[F, ?]]],
description: Option[String] = None
) extends ObjectLike[F, A] {
def document(description: String): Interface[F, A] = copy(description = Some(description))
lazy val abstractFieldsNel = fields.map { case (k, v) => k -> v.asAbstract }
lazy val abstractFields = abstractFieldsNel.toList
lazy val abstractFieldMap = abstractFields.toList.toMap
lazy val implementsMap = implementations.map(i => i.value.name -> i).toMap
}
final case class Scalar[A](
name: String,
encoder: A => V[Const, Unit],
decoder: V[Const, Unit] => Either[String, A],
description: Option[String] = None
) extends OutToplevel[fs2.Pure, A]
with InToplevel[A] {
def document(description: String): Scalar[A] = copy(description = Some(description))
def eimap[B](f: A => Either[String, B])(g: B => A): Scalar[B] =
Scalar(name, encoder.compose(g), decoder.andThen(_.flatMap(f)), description)
def rename(newName: String): Scalar[A] = copy(name = newName)
}
final case class EnumValue[A](
value: A,
description: Option[String] = None
) {
def document(description: String): EnumValue[A] = copy(description = Some(description))
}
final case class Enum[A](
name: String,
mappings: NonEmptyList[(String, EnumValue[? <: A])],
description: Option[String] = None
) extends OutToplevel[fs2.Pure, A]
with InToplevel[A] {
def document(description: String): Enum[A] = copy(description = Some(description))
lazy val kv = mappings.map { case (k, v) => k -> v.value }
lazy val m = kv.toNem
lazy val revm = kv.map(_.swap).toList.toMap
}
sealed trait AnyField[+F[_], -I, T] {
def output: Eval[Out[F, T]]
def asAbstract: AbstractField[F, T]
}
final case class Field[+F[_], -I, T](
resolve: Resolver[F, I, T],
output: Eval[Out[F, T]],
description: Option[String] = None
) extends AnyField[F, I, T] {
def document(description: String): Field[F, I, T] = copy(description = Some(description))
lazy val asAbstract: AbstractField[F, T] =
AbstractField(
NonEmptyChain
.fromChain {
def collectFields[G[_]](step: Step[G, ?, ?]): Chain[Arg[Any]] =
step match {
case Step.Alg.Argument(a) => Chain.one(a)
case Step.Alg.First(s) => collectFields(s)
case Step.Alg.Choose(l, r) => collectFields(l) ++ collectFields(r)
case Step.Alg.Compose(l, r) => collectFields(l) ++ collectFields(r)
case _ => Chain.empty
}
collectFields(resolve.underlying)
}
.map(_.nonEmptySequence),
output,
description
)
def compose[F2[x] >: F[x], I2](r: Resolver[F2, I2, I]): Field[F2, I2, T] =
Field(r.andThen(resolve), output, description)
def contramap[F2[x] >: F[x], I2](f: I2 => I): Field[F2, I2, T] =
compose[F2, I2](Resolver.lift[F2, I2](f))
}
// Field, but without any implementation
final case class AbstractField[+F[_], T](
arg: Option[Arg[?]],
output: Eval[Out[F, T]],
description: Option[String] = None
) extends AnyField[F, Any, T] {
def document(description: String): AbstractField[F, T] = copy(description = Some(description))
def asAbstract = this
}
final case class OutOpt[+F[_], A, B](of: Out[F, B], resolver: Resolver[F, A, B]) extends Out[F, Option[A]]
final case class OutArr[+F[_], A, C, B](of: Out[F, B], toSeq: C => Seq[A], resolver: Resolver[F, A, B]) extends Out[F, C] {
def contramap[D](f: D => C): OutArr[F, A, D, B] = OutArr(of, f.andThen(toSeq), resolver)
}
final case class InOpt[A](of: In[A]) extends In[Option[A]]
// This can be a bit hard to read
// For every element in an input array [I1, I2, ...] decode with of such that we have [A1, A2, ...],
// then map [A1, A2, ...] into C (which could be another datatype, for example a non-empty container)
final case class InArr[A, C](of: In[A], fromSeq: Seq[A] => Either[String, C]) extends In[C] {
def emap[B](f: C => Either[String, B]): InArr[A, B] =
InArr(of, fromSeq.andThen(_.flatMap(f)))
def map[B](f: C => B): InArr[A, B] =
InArr(of, fromSeq.andThen(_.map(f)))
}
object Scalar {
def fromCirce[A](name: String)(implicit enc: Encoder[A], dec: Decoder[A]): Scalar[A] = {
import io.circe.syntax._
Scalar(
name,
a => V.fromJson(enc(a)),
value =>
dec.decodeJson(value.asJson).leftMap { case df: io.circe.DecodingFailure =>
val maybeAt = if (df.history.size > 1) s" at ${io.circe.CursorOp.opsToPath(df.history)}" else ""
s"decoding failure for type `$name`$maybeAt with message ${df.message}"
}
)
}
implicit lazy val invariantForScalar: Invariant[Scalar] = new Invariant[Scalar] {
override def imap[A, B](fa: Scalar[A])(f: A => B)(g: B => A): Scalar[B] =
Scalar(fa.name, fa.encoder.compose(g), fa.decoder.andThen(_.map(f)), fa.description)
}
}
final case class ID[A](value: A) extends AnyVal
object ID extends IDLowPrio {
implicit def idTpe[A](implicit s: Scalar[A]): Scalar[ID[A]] =
s.imap(ID(_))(_.value)
.rename("ID")
.document(
"""|The `ID` scalar type represents a unique identifier, often used to refetch an object or as key for a cache.
|The ID type appears in a JSON response as a String; however, it is not intended to be human-readable.
|When expected as an input type, any string (such as `\"4\"`) or integer (such as `4`) input value will be accepted as an ID."""".stripMargin
)
}
trait IDLowPrio {
implicit def idIn[A](implicit s: Scalar[A]): In[ID[A]] = ID.idTpe[A]
}
}
object AstImplicits {
import ast._
trait Implicits extends LowPriorityImplicits {
implicit lazy val stringScalar: Scalar[String] = Scalar
.fromCirce[String]("String")
.document("The `String` is a UTF-8 character sequence usually representing human-readable text.")
implicit lazy val intScalar: Scalar[Int] = Scalar
.fromCirce[Int]("Int")
.document(
"The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1."
)
implicit lazy val longScalar: Scalar[Long] = Scalar
.fromCirce[Long]("Long")
.document(
"The `Long` scalar type represents non-fractional signed whole numeric values. Long can represent values between -(2^63) and 2^63 - 1."
)
implicit lazy val floatScalar: Scalar[Float] = Scalar
.fromCirce[Float]("Float")
.document(
"The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point)."
)
implicit lazy val doubleScalar: Scalar[Double] = Scalar
.fromCirce[Double]("Double")
.document(
"The `Double` scalar type represents signed double-precision fractional values as specified by [IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point)."
)
implicit lazy val bigIntScalar: Scalar[BigInt] = Scalar
.fromCirce[BigInt]("BigInt")
.document(
"The `BigInt` scalar type represents non-fractional signed whole numeric values. BigInt can represent values of arbitrary size."
)
implicit lazy val bigDecimalScalar: Scalar[BigDecimal] = Scalar
.fromCirce[BigDecimal]("BigDecimal")
.document(
"The `BigDecimal` scalar type represents signed double-precision fractional values as specified by [IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point)."
)
implicit lazy val booleanScalar: Scalar[Boolean] = Scalar
.fromCirce[Boolean]("Boolean")
.document("The `Boolean` scalar type represents `true` or `false`.")
implicit lazy val uuidScalar: Scalar[UUID] = Scalar
.fromCirce[UUID]("UUID")
.document(
"The `UUID` scalar type represents a UUID v4 as specified by [RFC 4122](https://tools.ietf.org/html/rfc4122)."
)
implicit def gqlInForOption[A](implicit tpe: In[A]): In[Option[A]] = InOpt(tpe)
implicit def gqlOutForOption[F[_], A](implicit tpe: Out[F, A]): OutOpt[F, A, A] =
OutOpt(tpe, Resolver.id)
}
trait LowPriorityImplicits {
implicit def gqlInForSeq[A](implicit tpe: In[A]): In[Seq[A]] = InArr[A, Seq[A]](tpe, _.asRight)
implicit def gqlInForList[A](implicit tpe: In[A]): In[List[A]] = InArr[A, List[A]](tpe, _.toList.asRight)
implicit def gqlInForVector[A](implicit tpe: In[A]): In[Vector[A]] = InArr[A, Vector[A]](tpe, _.toVector.asRight)
implicit def gqlInForSet[A](implicit tpe: In[A]): In[Set[A]] = InArr[A, Set[A]](tpe, _.toSet.asRight)
implicit def gqlInForNonEmptyList[A](implicit tpe: In[A]): In[NonEmptyList[A]] =
InArr[A, NonEmptyList[A]](tpe, _.toList.toNel.toRight("expected non-empty array, but array was empty"))
implicit def gqlInForNonEmptyVector[A](implicit tpe: In[A]): In[NonEmptyVector[A]] =
InArr[A, NonEmptyVector[A]](tpe, _.toVector.toNev.toRight("expected non-empty array, but array was empty"))
implicit def gqlInForChain[A](implicit tpe: In[A]): In[Chain[A]] =
InArr[A, Chain[A]](tpe, xs => Chain.fromSeq(xs).asRight)
implicit def gqlInForNonEmptyChain[A](implicit tpe: In[A]): In[NonEmptyChain[A]] =
InArr[A, NonEmptyChain[A]](tpe, xs => NonEmptyChain.fromSeq(xs).toRight("expected non-empty array, but array was empty"))
implicit def gqlOutArrForTraversable[F[_], A, G[_]: Traverse](implicit tpe: Out[F, A]): OutArr[F, A, G[A], A] =
OutArr(tpe, _.toList, Resolver.id)
}
}