-
Notifications
You must be signed in to change notification settings - Fork 3
/
ast.scala
338 lines (270 loc) · 12.5 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
package gql
import cats.implicits._
import io.circe._
import cats._
import cats.data._
import gql.resolver._
import java.util.UUID
object ast extends AstImplicits.Implicits {
sealed trait Out[F[_], A] {
def mapK[G[_]: Functor](fk: F ~> G): Out[G, A]
}
sealed trait In[A]
sealed trait InLeaf[A] extends In[A]
sealed trait Toplevel[+A] {
def name: String
def description: Option[String]
}
sealed trait OutToplevel[F[_], A] extends Out[F, A] with Toplevel[A] {
override def mapK[G[_]: Functor](fk: F ~> G): OutToplevel[G, A]
}
sealed trait InToplevel[A] extends In[A] with Toplevel[A]
sealed trait Selectable[F[_], A] extends OutToplevel[F, A] {
def fieldsList: List[(String, Field[F, A, ?, ?])]
def fieldMap: Map[String, Field[F, A, ?, ?]]
override def mapK[G[_]: Functor](fk: F ~> G): Selectable[G, A]
}
sealed trait ObjectLike[F[_], A] extends Selectable[F, A] {
def implementsMap: Map[String, Implementation[F, 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 implementsMap = implementations.map(i => i.implementation.value.name -> i).toMap
def mapK[G[_]: Functor](fk: F ~> G): Type[G, A] =
Type(name, fields.map { case (k, v) => k -> v.mapK(fk) }, implementations.map(_.mapK(fk)), description)
}
final case class Input[A](
name: String,
fields: NonEmptyArg[A],
description: Option[String] = None
) extends InToplevel[A] {
def document(description: String): Input[A] = copy(description = Some(description))
}
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
def mapK[G[_]: Functor](fk: F ~> G): Union[G, A] =
Union(
name,
types.map(_.mapK(fk)),
description
)
}
final case class Implementation[F[_], A, B](implementation: Eval[Interface[F, B]])(implicit
val specify: B => Option[A]
) {
def mapK[G[_]: Functor](fk: F ~> G): Implementation[G, A, B] =
Implementation(implementation.map(_.mapK(fk)))
}
final case class Interface[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): Interface[F, A] = copy(description = Some(description))
override def mapK[G[_]: Functor](fk: F ~> G): Interface[G, A] =
copy[G, A](
implementations = implementations.map(_.mapK(fk)),
fields = fields.map { case (k, v) => k -> v.mapK(fk) }
)
lazy val fieldsList = fields.toList
lazy val fieldMap = fields.toNem.toSortedMap.toMap
lazy val implementsMap = implementations.map(i => i.implementation.value.name -> i).toMap
}
final case class Scalar[F[_], A](
name: String,
encoder: A => Value,
decoder: Value => Either[String, A],
description: Option[String] = None
) extends OutToplevel[F, A]
with InLeaf[A]
with InToplevel[A] {
def document(description: String): Scalar[F, A] = copy(description = Some(description))
override def mapK[G[_]: Functor](fk: F ~> G): Scalar[G, A] =
Scalar(name, encoder, decoder, description)
def eimap[B](f: A => Either[String, B])(g: B => A): Scalar[F, B] =
Scalar(name, encoder.compose(g), decoder.andThen(_.flatMap(f)), description)
def rename(newName: String): Scalar[F, 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[F[_], A](
name: String,
mappings: NonEmptyList[(String, EnumValue[A])],
description: Option[String] = None
) extends OutToplevel[F, A]
with InLeaf[A]
with InToplevel[A] {
def document(description: String): Enum[F, A] = copy(description = Some(description))
override def mapK[G[_]: Functor](fk: F ~> G): Enum[G, A] =
Enum(name, mappings, 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
}
final case class Field[F[_], -I, T, A](
args: Arg[A],
resolve: Resolver[F, (I, A), T],
output: Eval[Out[F, T]],
description: Option[String] = None
) {
def document(description: String): Field[F, I, T, A] = copy(description = Some(description))
type A0 = A
def mapK[G[_]: Functor](fk: F ~> G): Field[G, I, T, A] =
Field[G, I, T, A](
args,
resolve.mapK(fk),
output.map(_.mapK(fk)),
description
)
def contramap[B](g: B => I): Field[F, B, T, A] =
Field(
args,
resolve.contramap[(B, A)] { case (b, a) => (g(b), a) },
output,
description
)
}
final case class Variant[F[_], A, B](tpe: Eval[Type[F, B]])(implicit val specify: A => Option[B]) {
def mapK[G[_]: Functor](fk: F ~> G): Variant[G, A, B] =
Variant(tpe.map(_.mapK(fk)))
def contramap[C](g: C => A): Variant[F, C, B] =
Variant[F, C, B](tpe)(c => specify(g(c)))
}
final case class OutOpt[F[_], A](of: Out[F, A]) extends Out[F, Option[A]] {
def mapK[G[_]: Functor](fk: F ~> G): OutOpt[G, A] = OutOpt(of.mapK(fk))
}
final case class OutArr[F[_], A, C](of: Out[F, A], toSeq: C => Seq[A]) extends Out[F, C] {
def mapK[G[_]: Functor](fk: F ~> G): OutArr[G, A, C] = OutArr(of.mapK(fk), toSeq)
def contramap[B](f: B => C): OutArr[F, A, B] = OutArr(of, f.andThen(toSeq))
}
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)
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[F[_], A](name: String)(implicit enc: Encoder[A], dec: Decoder[A]): Scalar[F, A] =
Scalar(
name,
a => Value.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 def invariantForScalar[F[_]]: Invariant[Scalar[F, *]] = new Invariant[Scalar[F, *]] {
override def imap[A, B](fa: Scalar[F, A])(f: A => B)(g: B => A): Scalar[F, 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 {
implicit def idTpe[F[_], A](implicit s: Scalar[F, A]): Scalar[F, 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
)
}
}
object AstImplicits {
import ast._
trait Implicits extends LowPriorityImplicits {
implicit def stringScalar[F[_]]: Scalar[F, String] = Scalar
.fromCirce[F, String]("String")
.document("The `String` is a UTF-8 character sequence usually representing human-readable text.")
implicit def intScalar[F[_]]: Scalar[F, Int] = Scalar
.fromCirce[F, 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 def longScalar[F[_]]: Scalar[F, Long] = Scalar
.fromCirce[F, 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 def floatScalar[F[_]]: Scalar[F, Float] = Scalar
.fromCirce[F, 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 def doubleScalar[F[_]]: Scalar[F, Double] = Scalar
.fromCirce[F, 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 def bigIntScalar[F[_]]: Scalar[F, BigInt] = Scalar
.fromCirce[F, BigInt]("BigInt")
.document(
"The `BigInt` scalar type represents non-fractional signed whole numeric values. BigInt can represent values of arbitrary size."
)
implicit def bigDecimalScalar[F[_]]: Scalar[F, BigDecimal] = Scalar
.fromCirce[F, 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 def booleanScalar[F[_]]: Scalar[F, Boolean] = Scalar
.fromCirce[F, Boolean]("Boolean")
.document("The `Boolean` scalar type represents `true` or `false`.")
implicit def uuidScalar[F[_]]: Scalar[F, UUID] = Scalar
.fromCirce[F, 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]): Out[F, Option[A]] = OutOpt(tpe)
}
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("empty array"))
implicit def gqlInForNonEmptyVector[A](implicit tpe: In[A]): In[NonEmptyVector[A]] =
InArr[A, NonEmptyVector[A]](tpe, _.toVector.toNev.toRight("empty array"))
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("empty array"))
implicit def gqlOutArrForSeqLike[F[_], A, G[x] <: Seq[x]](implicit tpe: Out[F, A]): Out[F, G[A]] =
OutArr(tpe, identity)
implicit def gqlOutArrForNel[F[_], A](implicit tpe: Out[F, A]): Out[F, NonEmptyList[A]] =
OutArr(tpe, _.toList)
implicit def gqlOutArrForNev[F[_], A](implicit tpe: Out[F, A]): Out[F, NonEmptyVector[A]] =
OutArr(tpe, _.toVector)
implicit def gqlOutArrForNec[F[_], A](implicit tpe: Out[F, A]): Out[F, NonEmptyChain[A]] =
OutArr(tpe, _.toList)
implicit def gqlOutArrForChain[F[_], A](implicit tpe: Out[F, A]): Out[F, Chain[A]] =
OutArr(tpe, _.toList)
}
}