-
Notifications
You must be signed in to change notification settings - Fork 3
/
dsl.scala
277 lines (207 loc) · 11.5 KB
/
dsl.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
/*
* 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 gql.ast._
import gql.resolver._
import cats.data._
import scala.reflect.ClassTag
object dsl {
def tpe[F[_], A](
name: String,
hd: (String, Field[F, A, ?, ?]),
tl: (String, Field[F, A, ?, ?])*
) = Type[F, A](name, NonEmptyList(hd, tl.toList), Nil)
def input[A](
name: String,
fields: NonEmptyArg[A]
): Input[A] = Input(name, fields)
def arg[A](name: String)(implicit tpe: => In[A]): NonEmptyArg[A] =
NonEmptyArg.one[A](ArgValue(name, Eval.later(tpe), None, None))
def arg[A](name: String, description: String)(implicit tpe: => In[A]): NonEmptyArg[A] =
NonEmptyArg.one[A](ArgValue(name, Eval.later(tpe), None, Some(description)))
def arg[A](name: String, default: Value)(implicit tpe: => In[A]): NonEmptyArg[A] =
NonEmptyArg.one[A](ArgValue(name, Eval.later(tpe), Some(default), None))
def arg[A](name: String, default: Value, description: String)(implicit tpe: => In[A]): NonEmptyArg[A] =
NonEmptyArg.one[A](ArgValue(name, Eval.later(tpe), Some(default), Some(description)))
def cache[F[_]: Functor, I, O](resolver: Resolver[F, I, O])(get: I => F[Option[O]]): CacheResolver[F, I, I, O] =
CacheResolver(i => get(i).map(_.toRight(i)), resolver)
def cacheFull[F[_], I, I2, O](resolver: Resolver[F, I2, O])(get: I => F[Either[I2, O]]): CacheResolver[F, I, I2, O] =
CacheResolver(get, resolver)
object value {
def scalar[F[_], A](value: A)(implicit tpe: => Scalar[F, A]) =
tpe.encoder(value)
def fromEnum[F[_], A](value: A)(implicit tpe: => Enum[F, A]) =
tpe.revm.get(value).map(enumValue)
def enumValue(value: String) = Value.EnumValue(value)
def arr(xs: Value*) = Value.ArrayValue(xs.toVector)
def obj(xs: (String, Value)*) = Value.ObjectValue(xs.toMap)
def nullValue = Value.NullValue
}
final class PartiallyAppliedField[I](val dummy: Boolean = false) extends AnyVal {
def apply[F[_], T, A](arg: Arg[A])(resolver: Resolver[F, (I, A), T])(implicit
tpe: => Out[F, T]
): Field[F, I, T, A] =
full.field[F, I, T, A](arg)(resolver)(tpe)
def apply[F[_], T](resolver: Resolver[F, I, T])(implicit tpe: => Out[F, T]): Field[F, I, T, Unit] =
full.field[F, I, T](resolver)(tpe)
}
def field[I] = new PartiallyAppliedField[I]
final class PartiallyAppliedStream[I](val dummy: Boolean = false) extends AnyVal {
def apply[F[_], T](f: I => fs2.Stream[F, T]): StreamResolver[F, I, T] =
full.stream[F, I, T](f)
}
def stream[I] = new PartiallyAppliedStream[I]
final class PartiallyAppliedStreamFallible[I](val dummy: Boolean = false) extends AnyVal {
def apply[F[_], T](f: I => fs2.Stream[F, IorNec[String, T]]): StreamResolver[F, I, T] =
full.streamFallible[F, I, T](f)
}
def streamFallible[I] = new PartiallyAppliedStreamFallible[I]
final class PartiallyAppliedFallible[I](val dummy: Boolean = false) extends AnyVal {
def apply[F[_], T, A](
arg: Arg[A]
)(resolver: (I, A) => F[Ior[String, T]])(implicit tpe: => Out[F, T]): Field[F, I, T, A] =
full.fallible[F, I, T, A](arg)(resolver)(tpe)
def apply[F[_], T](resolver: I => F[Ior[String, T]])(implicit tpe: => Out[F, T]): Field[F, I, T, Unit] =
full.fallible[F, I, T](resolver)(tpe)
}
def fallible[I] = new PartiallyAppliedFallible[I]
final class PartiallyAppliedEff[I](val dummy: Boolean = false) extends AnyVal {
def apply[F[_], T, A](arg: Arg[A])(resolver: (I, A) => F[T])(implicit tpe: => Out[F, T]): Field[F, I, T, A] =
full.eff[F, I, T, A](arg)(resolver)(tpe)
def apply[F[_], T](resolver: I => F[T])(implicit tpe: => Out[F, T]): Field[F, I, T, Unit] =
full.eff[F, I, T](resolver)(tpe)
}
def eff[I] = new PartiallyAppliedEff[I]
final class PartiallyAppliedPure[F[_], I](val dummy: Boolean = false) extends AnyVal {
def apply[T, A](arg: Arg[A])(resolver: (I, A) => Id[T])(implicit tpe: => Out[F, T]): Field[F, I, T, A] =
full.pure[F, I, T, A](arg)(resolver)(tpe)
def apply[T](resolver: I => Id[T])(implicit tpe: => Out[F, T]): Field[F, I, T, Unit] =
full.pure[F, I, T](resolver)(tpe)
}
def pure[F[_], I] = new PartiallyAppliedPure[F, I]
object full {
def field[F[_], I, T, A](arg: Arg[A])(resolver: Resolver[F, (I, A), T])(implicit
tpe: => Out[F, T]
): Field[F, I, T, A] =
Field[F, I, T, A](arg, resolver, Eval.later(tpe))
def field[F[_], I, T](resolver: Resolver[F, I, T])(implicit tpe: => Out[F, T]): Field[F, I, T, Unit] =
Field[F, I, T, Unit](Applicative[Arg].unit, resolver.contramap[(I, Unit)] { case (i, _) => i }, Eval.later(tpe))
def eff[F[_], I, T, A](arg: Arg[A])(resolver: (I, A) => F[T])(implicit tpe: => Out[F, T]): Field[F, I, T, A] =
field(arg)(EffectResolver[F, (I, A), T] { case (i, a) => resolver(i, a) })(tpe)
def eff[F[_], I, T](resolver: I => F[T])(implicit tpe: => Out[F, T]): Field[F, I, T, Unit] =
eff[F, I, T, Unit](Applicative[Arg].unit)((i, _) => resolver(i))(tpe)
// Not sure if this is a compiler bug or something since all type parameters except I are invariant?
def pure[F[_], I, T, A](arg: Arg[A])(resolver: (I, A) => Id[T])(implicit tpe: => Out[F, T]): Field[F, I, T, A] =
field(arg)(PureResolver[F, (I, A), T] { case (i, a) => resolver(i, a) })(tpe)
def pure[F[_], I, T](resolver: I => Id[T])(implicit tpe: => Out[F, T]): Field[F, I, T, Unit] =
field(PureResolver[F, I, T](resolver))(tpe)
def fallible[F[_], I, T, A](
arg: Arg[A]
)(resolver: (I, A) => F[Ior[String, T]])(implicit tpe: => Out[F, T]): Field[F, I, T, A] =
field(arg)(FallibleResolver[F, (I, A), T] { case (i, a) => resolver(i, a) })(tpe)
def fallible[F[_], I, T](resolver: I => F[Ior[String, T]])(implicit tpe: => Out[F, T]): Field[F, I, T, Unit] =
fallible[F, I, T, Unit](Applicative[Arg].unit)((i, _) => resolver(i))(tpe)
def stream[F[_], I, T](f: I => fs2.Stream[F, T]): StreamResolver[F, I, T] =
streamFallible[F, I, T](i => f(i).map(_.rightIor))
def streamFallible[F[_], I, T](f: I => fs2.Stream[F, IorNec[String, T]]): StreamResolver[F, I, T] =
StreamResolver(f)
}
def enumVal[A](value: A): EnumValue[A] =
EnumValue(value)
def enumType[F[_], A](name: String, hd: (String, EnumValue[? <: A]), tl: (String, EnumValue[? <: A])*) =
Enum[F, A](name, NonEmptyList(hd, tl.toList))
def interface[F[_], A](
name: String,
hd: (String, Field[F, A, ?, ?]),
tl: (String, Field[F, A, ?, ?])*
) = Interface[F, A](name, NonEmptyList(hd, tl.toList), Nil)
def union[F[_], A](name: String) = PartiallyAppliedUnion0[F, A](name)
def optType[F[_], A, B](resolver: Resolver[F, A, B])(implicit tpe: => Out[F, B]): Out[F, Option[A]] =
OutOpt(tpe, resolver)
def optType[A](implicit tpe: => In[A]): In[Option[A]] = InOpt(tpe)
def arrType[F[_], A, C, B](toSeq: C => Seq[A])(resolver: Resolver[F, A, B])(implicit
tpe: => Out[F, B]
): OutArr[F, A, C, B] =
OutArr(tpe, toSeq, resolver)
def arrType[F[_], A, G[x] <: Seq[x], B](resolver: Resolver[F, A, B])(implicit
tpe: => Out[F, B]
): OutArr[F, A, G[A], B] =
OutArr(tpe, identity, resolver)
def arrType[A](implicit tpe: => In[A]): In[Seq[A]] = InArr[A, Seq[A]](tpe, _.asRight)
implicit class ResolverSyntax[F[_], I, O](val resolver: Resolver[F, I, O]) extends AnyVal {
def andThen[O2](next: Resolver[F, O, O2]): Resolver[F, I, O2] =
CompositionResolver(resolver, next)
def streamMap[O2](f: O => fs2.Stream[F, IorNec[String, O2]]): Resolver[F, I, O2] =
resolver.andThen(StreamResolver(f))
def fallibleMap[O2](f: O => F[Ior[String, O2]]): Resolver[F, I, O2] =
resolver.andThen(FallibleResolver[F, O, O2](f))
def evalMap[O2](f: O => F[O2]): Resolver[F, I, O2] =
resolver.andThen(EffectResolver[F, O, O2](f))
def map[O2](f: O => O2): Resolver[F, I, O2] =
resolver.andThen(PureResolver[F, O, O2](f))
def mapBoth[O2](f: (I, O) => O2)(implicit F: Functor[F]): Resolver[F, I, O2] =
resolver.mapWithInput[I, O2] { case (i, o) => f(i, o) }
}
implicit class FieldSyntax[F[_], I, T, A](val field: Field[F, I, T, A]) extends AnyVal {
def compose[I2](g: Resolver[F, I2, I])(implicit F: Functor[F]): Field[F, I2, T, A] =
Field(
field.args,
g.contramap[(I2, A)] { case (b, _) => b }.mapBoth { case ((_, a), i) => (i, a) }.andThen(field.resolve),
field.output,
field.description
)
}
implicit class BatchResolverSyntax[F[_], K, V](val batchResolver: BatchResolver[F, Set[K], Map[K, V]]) extends AnyVal {
def one(implicit F: Functor[F]): Resolver[F, K, Option[V]] =
batchResolver.contramap[K](Set(_)).mapBoth { case (k, m) => m.get(k) }
}
implicit class TypeSyntax[F[_], A](val tpe: Type[F, A]) extends AnyVal {
def implements[B](pf: PartialFunction[B, A])(implicit interface: => Interface[F, B]): Type[F, A] =
tpe.copy(implementations = Implementation(Eval.later(interface))(pf.lift) :: tpe.implementations)
def subtypeOf[B](implicit ev: A <:< B, tag: ClassTag[A], interface: => Interface[F, B]): Type[F, A] =
implements[B] { case a: A => a }(interface)
def addFields(xs: (String, Field[F, A, ?, ?])*) =
tpe.copy(fields = tpe.fields concat xs.toList)
}
implicit class InterfaceSyntax[F[_], A](val tpe: Interface[F, A]) extends AnyVal {
def implements[B](pf: PartialFunction[B, A])(implicit interface: => Interface[F, B]): Interface[F, A] =
tpe.copy(implementations = Implementation(Eval.later(interface))(pf.lift) :: tpe.implementations)
def subtypeOf[B](implicit ev: A <:< B, tag: ClassTag[A], interface: => Interface[F, B]): Interface[F, A] =
implements[B] { case a: A => a }(interface)
def addFields(xs: (String, Field[F, A, ?, ?])*) =
tpe.copy(fields = tpe.fields concat xs.toList)
}
implicit class UnionSyntax[F[_], A](val tpe: Union[F, A]) extends AnyVal {
def variant[B](pf: PartialFunction[A, B])(implicit innerTpe: => Type[F, B]): Union[F, A] =
tpe.copy(types = Variant[F, A, B](Eval.later(innerTpe))(pf.lift) :: tpe.types)
def subtype[B: ClassTag](implicit ev: B <:< A, innerTpe: => Type[F, B]): Union[F, A] =
variant[B] { case a: B => a }(innerTpe)
}
final case class PartiallyAppliedUnion1[F[_], A](name: String, hd: Variant[F, A, ?]) {
def variant[B](pf: PartialFunction[A, B])(implicit innerTpe: => Type[F, B]): Union[F, A] =
Union[F, A](name, NonEmptyList.of(hd, Variant[F, A, B](Eval.later(innerTpe))(pf.lift)), None)
def subtype[B: ClassTag](implicit ev: B <:< A, innerTpe: => Type[F, B]): Union[F, A] =
variant[B] { case a: B => a }(innerTpe)
}
final case class PartiallyAppliedUnion0[F[_], A](name: String) extends AnyVal {
def variant[B](pf: PartialFunction[A, B])(implicit innerTpe: => Type[F, B]): PartiallyAppliedUnion1[F, A] =
PartiallyAppliedUnion1[F, A](name, Variant[F, A, B](Eval.later(innerTpe))(pf.lift))
def subtype[B: ClassTag](implicit ev: B <:< A, innerTpe: => Type[F, B]): PartiallyAppliedUnion1[F, A] =
variant[B] { case a: B => a }(innerTpe)
}
}