-
Notifications
You must be signed in to change notification settings - Fork 3
/
FieldDsl.scala
173 lines (130 loc) · 6.07 KB
/
FieldDsl.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
/*
* 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.dsl
import gql.resolver._
import gql.ast._
import gql._
import cats.data._
import cats._
import gql.dsl.aliases._
trait FieldDsl[F[_]] {
def fields[A](hd: (String, Field[F, A, ?]), tl: (String, Field[F, A, ?])*): Fields[F, A] =
FieldDsl.fields(hd, tl: _*)
def eff[I] = FieldDsl.eff[I]
def lift[I] = FieldDsl.lift[I]
def build[I] = FieldDsl.builder[F, I]
def builder[I] = FieldDsl.builder[F, I]
def abst[T](implicit tpe: => Out[F, T]): AbstractField[F, T] =
FieldDsl.abst[F, T](tpe)
def abstWith[T, A](arg: Arg[A])(implicit tpe: => Out[F, T]): AbstractField[F, T] =
FieldDsl.abstWith[F, T, A](arg)(tpe)
def abstGroup(
hd: (String, AbstractField[F, ?]),
tl: (String, AbstractField[F, ?])*
): AbstractFields[F] =
FieldDsl.abstGroup(hd, tl: _*)
def optType[A, B](resolver: Resolver[F, A, B])(implicit tpe: => Out[F, B]): Out[F, Option[A]] =
FieldDsl.optType[F, A, B](resolver)(tpe)
def optType[A](implicit tpe: => In[A]): In[Option[A]] =
FieldDsl.optType[A](tpe)
def arrType[A, C, B](toSeq: C => Seq[A])(resolver: Resolver[F, A, B])(implicit
tpe: => Out[F, B]
): OutArr[F, A, C, B] =
FieldDsl.arrType[F, A, C, B](toSeq)(resolver)(tpe)
def arrType[A, G[x] <: Seq[x], B](resolver: Resolver[F, A, B])(implicit
tpe: => Out[F, B]
): OutArr[F, A, G[A], B] =
FieldDsl.arrType(resolver)(tpe)
implicit def fieldDslFullFieldsOps[A](fields: Fields[F, A]): FieldDsl.FieldsOps[F, A] =
FieldDsl.fieldDslFullFieldsOps[F, A](fields)
}
trait FieldDslFull {
def fields[F[_], A](hd: (String, Field[F, A, ?]), tl: (String, Field[F, A, ?])*): Fields[F, A] =
NonEmptyList[(String, Field[F, A, ?])](hd, tl.toList)
def eff[I] = new FieldDsl.PartiallyAppliedEff[I]
def lift[I] = new FieldDsl.PartiallyAppliedLift[I]
def build[F[_], I] = new FieldBuilder[F, I] {}
def builder[F[_], I] = new FieldDsl.PartiallyAppliedFieldBuilder[F, I]
def abst[F[_], T](implicit tpe: => Out[F, T]): AbstractField[F, T] =
AbstractField[F, T](None, Eval.later(tpe))
def abstWith[F[_], T, A](arg: Arg[A])(implicit tpe: => Out[F, T]): AbstractField[F, T] =
AbstractField[F, T](Some(arg), Eval.later(tpe))
def abstGroup[F[_]](
hd: (String, AbstractField[F, ?]),
tl: (String, AbstractField[F, ?])*
): AbstractFields[F] =
NonEmptyList(hd, tl.toList)
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)
implicit def fieldDslFullFieldsOps[F[_], A](fields: Fields[F, A]): FieldDsl.FieldsOps[F, A] =
new FieldDsl.FieldsOps[F, A](fields)
}
object FieldDsl extends FieldDslFull {
final class PartiallyAppliedEff[I](private val dummy: Boolean = false) extends AnyVal {
def apply[F[_], T, A](arg: Arg[A])(resolver: (A, I) => F[T])(implicit tpe: => Out[F, T]): Field[F, I, T] =
Field(Resolver.effect[F, (A, I)] { case (a, i) => resolver(a, i) }.contraArg(arg), Eval.later(tpe))
def apply[F[_], T](resolver: I => F[T])(implicit tpe: => Out[F, T]): Field[F, I, T] =
Field(Resolver.effect(resolver), Eval.later(tpe))
}
final class FieldsOps[F[_], A](private val fields: Fields[F, A]) extends AnyVal {
def addFields(xs: (String, Field[F, A, ?])*) =
fields concat xs.toList
def addFieldsNel(xs: NonEmptyList[(String, Field[F, A, ?])]) =
fields concat xs.toList
def mapValues[B](f: Field[F, A, ?] => Field[F, B, ?]) =
fields.map { case (k, v) => k -> f(v) }
def resolveBy[I2](f: Resolver[F, A, A] => Resolver[F, I2, A]): Fields[F, I2] =
mapValues(_.compose(f(Resolver.id)))
def compose[I2](resolver: Resolver[F, I2, A]): Fields[F, I2] =
resolveBy[I2](_.compose(resolver))
}
final class PartiallyAppliedFieldBuilder[F[_], I](private val dummy: Boolean = false) extends AnyVal {
def apply[A](f: FieldBuilder[F, I] => A): A = f(build[F, I])
}
final class PartiallyAppliedLift[I](private val dummy: Boolean = false) extends AnyVal {
def apply[F[_], T, A](arg: Arg[A])(resolver: (A, I) => Id[T])(implicit tpe: => Out[F, T]): Field[F, I, T] =
Field(Resolver.lift[F, (A, I)] { case (a, i) => resolver(a, i) }.contraArg(arg), Eval.later(tpe))
def apply[F[_], T](resolver: I => Id[T])(implicit tpe: => Out[F, T]): Field[F, I, T] =
Field(Resolver.lift[F, I](resolver), Eval.later(tpe))
}
}
trait FieldBuilder[F[_], I] {
def tpe(
name: String,
hd: (String, Field[F, I, ?]),
tl: (String, Field[F, I, ?])*
): Type[F, I] = TypeDsl.tpe[F, I](name, hd, tl: _*)
def fields(
hd: (String, Field[F, I, ?]),
tl: (String, Field[F, I, ?])*
): Fields[F, I] = FieldDsl.fields[F, I](hd, tl: _*)
def from[T](resolver: Resolver[F, I, T])(implicit tpe: => Out[F, T]): Field[F, I, T] =
Field[F, I, T](resolver, Eval.later(tpe))
def apply[T](f: Resolver[F, I, I] => Resolver[F, I, T])(implicit tpe: => Out[F, T]): Field[F, I, T] =
Field[F, I, T](f(Resolver.id[F, I]), Eval.later(tpe))
def lift = new FieldDsl.PartiallyAppliedLift[I]
def eff[T](resolver: I => F[T])(implicit tpe: => Out[F, T]): Field[F, I, T] =
Field(Resolver.effect(resolver), Eval.later(tpe))
}