-
Notifications
You must be signed in to change notification settings - Fork 27
/
mapping.scala
622 lines (534 loc) · 24.4 KB
/
mapping.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA)
// Copyright (c) 2016-2023 Grackle Contributors
//
// 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 grackle
import scala.collection.Factory
import scala.reflect.ClassTag
import cats.MonadThrow
import cats.data.Chain
import cats.implicits._
import fs2.{ Stream, Compiler }
import io.circe.{Encoder, Json}
import io.circe.syntax._
import org.tpolecat.sourcepos.SourcePos
import org.tpolecat.typename._
import syntax._
import Cursor.{AbstractCursor, ProxyCursor}
import Query.EffectHandler
import QueryCompiler.{ComponentElaborator, EffectElaborator, IntrospectionLevel, SelectElaborator}
import QueryInterpreter.ProtoJson
import IntrospectionLevel._
/**
* Represents a mapping between a GraphQL schema and an underlying abstract data source.
*/
abstract class Mapping[F[_]] {
implicit val M: MonadThrow[F]
val schema: Schema
val typeMappings: List[TypeMapping]
/**
* Compile and run a single GraphQL query or mutation.
*
* Yields a JSON response containing the result of the query or mutation.
*/
def compileAndRun(text: String, name: Option[String] = None, untypedVars: Option[Json] = None, introspectionLevel: IntrospectionLevel = Full, reportUnused: Boolean = true, env: Env = Env.empty)(
implicit sc: Compiler[F,F]
): F[Json] =
compileAndRunSubscription(text, name, untypedVars, introspectionLevel, reportUnused, env).compile.toList.flatMap {
case List(j) => j.pure[F]
case Nil => M.raiseError(new IllegalStateException("Result stream was empty."))
case js => M.raiseError(new IllegalStateException(s"Result stream contained ${js.length} results; expected exactly one."))
}
/**
* Compile and run a GraphQL subscription.
*
* Yields a stream of JSON responses containing the results of the subscription.
*/
def compileAndRunSubscription(text: String, name: Option[String] = None, untypedVars: Option[Json] = None, introspectionLevel: IntrospectionLevel = Full, reportUnused: Boolean = true, env: Env = Env.empty): Stream[F,Json] = {
val compiled = compiler.compile(text, name, untypedVars, introspectionLevel, reportUnused, env)
Stream.eval(compiled.pure[F]).flatMap(_.flatTraverse(op => interpreter.run(op.query, op.rootTpe, env))).evalMap(mkResponse)
}
/** Combine and execute multiple queries.
*
* Each query is interpreted in the context of the Cursor it is
* paired with. The result list is aligned with the argument
* query list. For each query at most one stage will be run and the
* corresponding result may contain deferred components.
*
* Errors are aggregated across all the argument queries and are
* accumulated on the `Left` of the result.
*
* This method is typically called at the end of a stage to evaluate
* deferred subqueries in the result of that stage. These will be
* grouped by and passed jointly to the responsible mapping in
* the next stage using this method. Maappongs which are able
* to benefit from combining queries may do so by overriding this
* method to implement their specific combinging logic.
*/
def combineAndRun(queries: List[(Query, Cursor)]): F[Result[List[ProtoJson]]] =
queries.map { case (q, c) => (q, schema.queryType, c) }.traverse((interpreter.runOneShot _).tupled).map(ProtoJson.combineResults)
/** Yields a `Cursor` focused on the top level operation type of the query */
def defaultRootCursor(query: Query, tpe: Type, parentCursor: Option[Cursor]): F[Result[(Query, Cursor)]] =
Result((query, RootCursor(Context(tpe), parentCursor, Env.empty))).pure[F].widen
/**
* Root `Cursor` focussed on the top level operation of a query
*
* Construction of mapping-specific cursors is handled by delegation to
* `mkCursorForField which is typically overridden in `Mapping` subtypes.
*/
case class RootCursor(context: Context, parent: Option[Cursor], env: Env) extends AbstractCursor {
def withEnv(env0: Env): Cursor = copy(env = env.add(env0))
def focus: Any = ()
override def hasField(fieldName: String): Boolean =
fieldMapping(context, fieldName).isDefined
override def field(fieldName: String, resultName: Option[String]): Result[Cursor] =
mkCursorForField(this, fieldName, resultName)
}
/**
* Yields a `Cursor` suitable for traversing the query result corresponding to
* the `fieldName` child of `parent`.
*
* This method is typically overridden in and delegated to by `Mapping` subtypes.
*/
def mkCursorForField(parent: Cursor, fieldName: String, resultName: Option[String]): Result[Cursor] = {
val context = parent.context
val fieldContext = context.forFieldOrAttribute(fieldName, resultName)
def mkLeafCursor(focus: Any): Result[Cursor] =
LeafCursor(fieldContext, focus, Some(parent), parent.env).success
fieldMapping(context, fieldName) match {
case Some(_ : EffectMapping) =>
mkLeafCursor(parent.focus)
case Some(CursorField(_, f, _, _, _)) =>
f(parent).flatMap(res => mkLeafCursor(res))
case _ =>
Result.failure(s"No field '$fieldName' for type ${parent.tpe}")
}
}
/** Yields the `TypeMapping` associated with the provided type, if any. */
def typeMapping(tpe: NamedType): Option[TypeMapping] =
typeMappingIndex.get(tpe.name)
private lazy val typeMappingIndex =
typeMappings.flatMap(tm => tm.tpe.asNamed.map(tpe => (tpe.name, tm)).toList).toMap
val validator: MappingValidator =
MappingValidator(this)
/** Yields the `ObjectMapping` associated with the provided context, if any. */
def objectMapping(context: Context): Option[ObjectMapping] =
context.tpe.underlyingObject.flatMap { obj =>
obj.asNamed.flatMap(typeMapping) match {
case Some(om: ObjectMapping) => Some(om)
case Some(pm: PrefixedMapping) =>
val revPath = context.path.reverse
pm.mappings.filter(m => revPath.endsWith(m._1)).maxByOption(_._1.length).map(_._2)
case _ => None
}
}
/** Yields the `FieldMapping` associated with `fieldName` in `context`, if any. */
def fieldMapping(context: Context, fieldName: String): Option[FieldMapping] =
objectMapping(context).flatMap(_.fieldMapping(fieldName)).orElse {
context.tpe.underlyingObject match {
case Some(ot: ObjectType) =>
ot.interfaces.collectFirstSome(nt => fieldMapping(context.asType(nt), fieldName))
case _ => None
}
}
/** Yields the `RootEffect`, if any, associated with `fieldName`. */
def rootEffect(context: Context, fieldName: String): Option[RootEffect] =
fieldMapping(context, fieldName).collect {
case re: RootEffect => re
}
/** Yields the `RootStream`, if any, associated with `fieldName`. */
def rootStream(context: Context, fieldName: String): Option[RootStream] =
fieldMapping(context, fieldName).collect {
case rs: RootStream => rs
}
/** Yields the `LeafMapping` associated with the provided type, if any. */
def leafMapping[T](tpe: Type): Option[LeafMapping[T]] =
typeMappings.collectFirst {
case lm@LeafMapping(tpe0, _) if tpe0 =:= tpe => lm.asInstanceOf[LeafMapping[T]]
}
/**
* True if the supplied type is a leaf with respect to the GraphQL schema
* or mapping, false otherwise.
*/
def isLeaf(tpe: Type): Boolean = tpe.underlying match {
case (_: ScalarType)|(_: EnumType) => true
case tpe => leafMapping(tpe).isDefined
}
/** Yields the `Encoder` associated with the provided type, if any. */
def encoderForLeaf(tpe: Type): Option[Encoder[Any]] =
encoderMemo.get(tpe.dealias)
private lazy val encoderMemo: scala.collection.immutable.Map[Type, Encoder[Any]] = {
val intTypeEncoder: Encoder[Any] =
new Encoder[Any] {
def apply(i: Any): Json = (i: @unchecked) match {
case i: Int => Json.fromInt(i)
case l: Long => Json.fromLong(l)
}
}
val floatTypeEncoder: Encoder[Any] =
new Encoder[Any] {
def apply(f: Any): Json = (f: @unchecked) match {
case f: Float => Json.fromFloatOrString(f)
case d: Double => Json.fromDoubleOrString(d)
case d: BigDecimal => Json.fromBigDecimal(d)
}
}
val definedEncoders: List[(Type, Encoder[Any])] =
typeMappings.collect { case lm: LeafMapping[_] => (lm.tpe.dealias -> lm.encoder.asInstanceOf[Encoder[Any]]) }
val defaultEncoders: List[(Type, Encoder[Any])] =
List(
ScalarType.StringType -> Encoder[String].asInstanceOf[Encoder[Any]],
ScalarType.IntType -> intTypeEncoder,
ScalarType.FloatType -> floatTypeEncoder,
ScalarType.BooleanType -> Encoder[Boolean].asInstanceOf[Encoder[Any]],
ScalarType.IDType -> Encoder[String].asInstanceOf[Encoder[Any]]
)
(definedEncoders ++ defaultEncoders).toMap
}
trait TypeMapping extends Product with Serializable {
def tpe: Type
def pos: SourcePos
}
case class PrimitiveMapping(tpe: Type)(implicit val pos: SourcePos) extends TypeMapping
abstract class ObjectMapping extends TypeMapping {
private lazy val fieldMappingIndex = fieldMappings.map(fm => (fm.fieldName, fm)).toMap
def fieldMappings: List[FieldMapping]
def fieldMapping(fieldName: String): Option[FieldMapping] = fieldMappingIndex.get(fieldName)
}
object ObjectMapping {
case class DefaultObjectMapping(tpe: Type, fieldMappings: List[FieldMapping])(
implicit val pos: SourcePos
) extends ObjectMapping
def apply(tpe: Type, fieldMappings: List[FieldMapping])(
implicit pos: SourcePos
): ObjectMapping =
DefaultObjectMapping(tpe, fieldMappings.map(_.withParent(tpe)))
}
case class PrefixedMapping(tpe: Type, mappings: List[(List[String], ObjectMapping)])(
implicit val pos: SourcePos
) extends TypeMapping
trait FieldMapping extends Product with Serializable {
def fieldName: String
def hidden: Boolean
def withParent(tpe: Type): FieldMapping
def pos: SourcePos
}
case class PrimitiveField(fieldName: String, hidden: Boolean = false)(implicit val pos: SourcePos) extends FieldMapping {
def withParent(tpe: Type): PrimitiveField = this
}
/**
* Abstract type of field mappings with effects.
*/
trait EffectMapping extends FieldMapping
case class EffectField(fieldName: String, handler: EffectHandler[F], required: List[String] = Nil, hidden: Boolean = false)(implicit val pos: SourcePos)
extends EffectMapping {
def withParent(tpe: Type): EffectField = this
}
/**
* Root effects can perform an intial effect prior to computing the resulting
* `Cursor` and effective `Query`.
*
* These effects are used to perform initial effectful setup for a query or to
* perform the effect associated with a GraphQL mutation. Convenience methods
* are provided to cover the cases where only one of the query or the cursor
* are computed.
*
* If only the query is computed the default root cursor for the mapping will
* be used. If only the cursor is computed the client query (after elaboration)
* is used unmodified ... in this case results of the performed effect can only
* be passed to the result construction stage via the environment associated
* with the returned cursor.
*/
case class RootEffect private (fieldName: String, effect: (Query, Path, Env) => F[Result[(Query, Cursor)]])(implicit val pos: SourcePos)
extends EffectMapping {
def hidden = false
def withParent(tpe: Type): RootEffect = this
def toRootStream: RootStream = RootStream(fieldName)((q, p, e) => Stream.eval(effect(q, p, e)))
}
object RootEffect {
/**
* Yields a `RootEffect` which performs both an initial effect and yields an effect-specific query and
* corresponding root cursor.
*/
def apply(fieldName: String)(effect: (Query, Path, Env) => F[Result[(Query, Cursor)]])(implicit pos: SourcePos, di: DummyImplicit): RootEffect =
new RootEffect(fieldName, effect)
/**
* Yields a `RootEffect` which performs an initial effect which leaves the query and default root cursor
* unchanged.
*/
def computeUnit(fieldName: String)(effect: Env => F[Result[Unit]])(implicit pos: SourcePos): RootEffect =
new RootEffect(
fieldName,
(query, path, env) =>
(for {
_ <- ResultT(effect(env))
qc <- ResultT(defaultRootCursor(query, path.rootTpe, None))
} yield qc.map(_.withEnv(env))).value
)
/**
* Yields a `RootEffect` which performs an initial effect and yields an effect-specific root cursor.
*/
def computeCursor(fieldName: String)(effect: (Path, Env) => F[Result[Cursor]])(implicit pos: SourcePos): RootEffect =
new RootEffect(
fieldName,
(query, path, env) => effect(path, env).map(_.map(c => (query, c)))
)
/**
* Yields a `RootEffect` which performs an initial effect and yields an effect-specific query
* which is executed with respect to the default root cursor for the corresponding `Mapping`.
*/
def computeChild(fieldName: String)(effect: (Query, Path, Env) => F[Result[Query]])(implicit pos: SourcePos): RootEffect =
new RootEffect(
fieldName,
(query, path, env) =>
(for {
child <- ResultT(Query.extractChild(query).toResultOrError("Root query has unexpected shape").pure[F])
q <- ResultT(effect(child, path, env).map(_.flatMap(Query.substChild(query, _).toResultOrError("Root query has unexpected shape"))))
qc <- ResultT(defaultRootCursor(q, path.rootTpe, None))
} yield qc.map(_.withEnv(env))).value
)
}
/**
* Root streams can perform an intial effect prior to emitting the resulting
* cursors and effective queries.
*
* Stream effects are used for GraphQL subscriptions. Convenience methods are
* provided to cover the cases where only one of the query or the cursor are
* computed
*
* If only the query is computed the default root cursor for the mapping will
* be used. If only the cursor is computed the client query (after elaboration)
* is used unmodified ... in this case results of the performed effect can only
* be passed to the result construction stage via the environment associated
* with the returned cursor.
*/
case class RootStream private (fieldName: String, effect: (Query, Path, Env) => Stream[F, Result[(Query, Cursor)]])(implicit val pos: SourcePos)
extends EffectMapping {
def hidden = false
def withParent(tpe: Type): RootStream = this
}
object RootStream {
/**
* Yields a `RootStream` which performs both an initial effect and yields an effect-specific query and
* corresponding root cursor.
*/
def apply(fieldName: String)(effect: (Query, Path, Env) => Stream[F, Result[(Query, Cursor)]])(implicit pos: SourcePos, di: DummyImplicit): RootStream =
new RootStream(fieldName, effect)
/**
* Yields a `RootStream` which yields a stream of effect-specific root cursors.
*
* This form of effect is typically used to implement GraphQL subscriptions.
*/
def computeCursor(fieldName: String)(effect: (Path, Env) => Stream[F, Result[Cursor]])(implicit pos: SourcePos): RootStream =
new RootStream(
fieldName,
(query, path, env) => effect(path, env).map(_.map(c => (query, c)))
)
/**
* Yields a `RootStream` which yields a stream of effect-specific queries
* which are executed with respect to the default root cursor for the
* corresponding `Mapping`.
*
* This form of effect is typically used to implement GraphQL subscriptions.
*/
def computeChild(fieldName: String)(effect: (Query, Path, Env) => Stream[F, Result[Query]])(implicit pos: SourcePos): RootStream =
new RootStream(
fieldName,
(query, path, env) =>
Query.extractChild(query).fold(Stream.emit[F, Result[(Query, Cursor)]](Result.internalError("Root query has unexpected shape"))) { child =>
effect(child, path, env).flatMap(child0 =>
Stream.eval(
(for {
q <- ResultT(child0.flatMap(Query.substChild(query, _).toResultOrError("Root query has unexpected shape")).pure[F])
qc <- ResultT(defaultRootCursor(q, path.rootTpe, None))
} yield qc.map(_.withEnv(env))).value
)
)
}
)
}
trait LeafMapping[T] extends TypeMapping {
def tpe: Type
def encoder: Encoder[T]
def scalaTypeName: String
def pos: SourcePos
}
object LeafMapping {
case class DefaultLeafMapping[T](tpe: Type, encoder: Encoder[T], scalaTypeName: String)(
implicit val pos: SourcePos
) extends LeafMapping[T]
def apply[T: TypeName](tpe: Type)(implicit encoder: Encoder[T], pos: SourcePos): LeafMapping[T] =
DefaultLeafMapping(tpe, encoder, typeName)
def unapply[T](lm: LeafMapping[T]): Option[(Type, Encoder[T])] =
Some((lm.tpe, lm.encoder))
}
case class CursorField[T](fieldName: String, f: Cursor => Result[T], encoder: Encoder[T], required: List[String], hidden: Boolean)(
implicit val pos: SourcePos
) extends FieldMapping {
def withParent(tpe: Type): CursorField[T] = this
}
object CursorField {
def apply[T](fieldName: String, f: Cursor => Result[T], required: List[String] = Nil, hidden: Boolean = false)(implicit encoder: Encoder[T], di: DummyImplicit): CursorField[T] =
new CursorField(fieldName, f, encoder, required, hidden)
}
case class Delegate(
fieldName: String,
mapping: Mapping[F],
join: (Query, Cursor) => Result[Query] = ComponentElaborator.TrivialJoin
)(implicit val pos: SourcePos) extends FieldMapping {
def hidden = false
def withParent(tpe: Type): Delegate = this
}
val selectElaborator: SelectElaborator = SelectElaborator.identity
lazy val componentElaborator = {
val componentMappings =
typeMappings.flatMap {
case om: ObjectMapping =>
om.fieldMappings.collect {
case Delegate(fieldName, mapping, join) =>
ComponentElaborator.ComponentMapping(schema.ref(om.tpe.toString), fieldName, mapping, join)
}
case _ => Nil
}
ComponentElaborator(componentMappings)
}
lazy val effectElaborator = {
val effectMappings =
typeMappings.flatMap {
case om: ObjectMapping =>
om.fieldMappings.collect {
case EffectField(fieldName, handler, _, _) =>
EffectElaborator.EffectMapping(schema.ref(om.tpe.toString), fieldName, handler)
}
case _ => Nil
}
EffectElaborator(effectMappings)
}
def compilerPhases: List[QueryCompiler.Phase] = List(selectElaborator, componentElaborator, effectElaborator)
def parserConfig: GraphQLParser.Config = GraphQLParser.defaultConfig
lazy val graphQLParser: GraphQLParser = GraphQLParser(parserConfig)
lazy val queryParser: QueryParser = QueryParser(graphQLParser)
lazy val compiler: QueryCompiler = new QueryCompiler(queryParser, schema, compilerPhases)
val interpreter: QueryInterpreter[F] = new QueryInterpreter(this)
/** Cursor positioned at a GraphQL result leaf */
case class LeafCursor(context: Context, focus: Any, parent: Option[Cursor], env: Env) extends Cursor {
def withEnv(env0: Env): Cursor = copy(env = env.add(env0))
def mkChild(context: Context = context, focus: Any = focus): LeafCursor =
LeafCursor(context, focus, Some(this), Env.empty)
def isLeaf: Boolean = tpe.isLeaf
def asLeaf: Result[Json] =
encoderForLeaf(tpe).map(enc => enc(focus).success).getOrElse(Result.internalError(
s"Cannot encode value $focus at ${context.path.reverse.mkString("/")} (of GraphQL type ${context.tpe}). Did you forget a LeafMapping?".stripMargin.trim
))
def preunique: Result[Cursor] = {
val listTpe = tpe.nonNull.list
focus match {
case _: List[_] => mkChild(context.asType(listTpe), focus).success
case _ =>
Result.internalError(s"Expected List type, found $focus for ${listTpe}")
}
}
def isList: Boolean =
tpe match {
case ListType(_) => true
case _ => false
}
def asList[C](factory: Factory[Cursor, C]): Result[C] = (tpe, focus) match {
case (ListType(tpe), it: List[_]) => it.view.map(f => mkChild(context.asType(tpe), focus = f)).to(factory).success
case _ => Result.internalError(s"Expected List type, found $tpe")
}
def listSize: Result[Int] = (tpe, focus) match {
case (ListType(_), it: List[_]) => it.size.success
case _ => Result.internalError(s"Expected List type, found $tpe")
}
def isNullable: Boolean =
tpe match {
case NullableType(_) => true
case _ => false
}
def asNullable: Result[Option[Cursor]] =
(tpe, focus) match {
case (NullableType(_), None) => None.success
case (NullableType(tpe), Some(v)) => Some(mkChild(context.asType(tpe), focus = v)).success
case _ => Result.internalError(s"Not nullable at ${context.path}")
}
def isDefined: Result[Boolean] =
(tpe, focus) match {
case (NullableType(_), opt: Option[_]) => opt.isDefined.success
case _ => Result.internalError(s"Not nullable at ${context.path}")
}
def narrowsTo(subtpe: TypeRef): Boolean = false
def narrow(subtpe: TypeRef): Result[Cursor] =
Result.failure(s"Cannot narrow $tpe to $subtpe")
def hasField(fieldName: String): Boolean = false
def field(fieldName: String, resultName: Option[String]): Result[Cursor] =
Result.failure(s"Cannot select field '$fieldName' from leaf type $tpe")
}
/**
* Proxy `Cursor` which applies a function to the focus of an underlying `LeafCursor`.
*/
case class FieldTransformCursor[T : ClassTag : TypeName](underlying: Cursor, f: T => Result[T]) extends ProxyCursor(underlying) {
override def withEnv(env: Env): Cursor = new FieldTransformCursor(underlying.withEnv(env), f)
override def field(fieldName: String, resultName: Option[String]): Result[Cursor] =
underlying.field(fieldName, resultName).flatMap {
case l: LeafCursor =>
for {
focus <- l.as[T]
ffocus <- f(focus)
} yield l.copy(focus = ffocus)
case _ =>
Result.internalError(s"Expected leaf cursor for field $fieldName")
}
}
/**
* Construct a GraphQL response from the possibly absent result `data`
* and a collection of errors.
*/
def mkResponse(data: Option[Json], errors: Chain[Problem]): Json = {
val dataField = data.map { value => ("data", value) }.toList
val fields =
(dataField, errors.toList) match {
case (Nil, Nil) => List(("errors", Json.fromValues(List(Problem("Invalid query").asJson))))
case (data, Nil) => data
case (data, errs) => ("errors", errs.asJson) :: data
}
Json.fromFields(fields)
}
/** Construct a GraphQL response from a `Result`. */
def mkResponse(result: Result[Json]): F[Json] =
result match {
case Result.InternalError(err) => M.raiseError(err)
case _ => mkResponse(result.toOption, result.toProblems).pure[F]
}
}
abstract class ComposedMapping[F[_]](implicit val M: MonadThrow[F]) extends Mapping[F] {
override def mkCursorForField(parent: Cursor, fieldName: String, resultName: Option[String]): Result[Cursor] = {
val context = parent.context
val fieldContext = context.forFieldOrAttribute(fieldName, resultName)
fieldMapping(context, fieldName) match {
case Some(_) =>
ComposedCursor(fieldContext, parent.env).success
case _ =>
super.mkCursorForField(parent, fieldName, resultName)
}
}
case class ComposedCursor(context: Context, env: Env) extends AbstractCursor {
val focus = null
val parent = None
def withEnv(env0: Env): Cursor = copy(env = env.add(env0))
override def hasField(fieldName: String): Boolean =
fieldMapping(context, fieldName).isDefined
override def field(fieldName: String, resultName: Option[String]): Result[Cursor] =
mkCursorForField(this, fieldName, resultName)
}
}