-
Notifications
You must be signed in to change notification settings - Fork 221
/
Schema.scala
1550 lines (1365 loc) · 55.5 KB
/
Schema.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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package sangria.schema
import sangria.ast.{AstNode, Document}
import language.implicitConversions
import sangria.execution.{FieldTag, SubscriptionField}
import sangria.marshalling.FromInput.{CoercedScalaResult, InputObjectResult}
import sangria.marshalling._
import sangria.{ast, introspection}
import sangria.validation._
import sangria.introspection._
import sangria.renderer.{QueryRenderer, SchemaFilter, SchemaRenderer}
import sangria.schema.InputObjectType.DefaultInput
import sangria.streaming.SubscriptionStreamLike
import scala.annotation.{implicitNotFound, tailrec}
import scala.reflect.ClassTag
import sangria.util.tag._
import scala.util.matching.Regex
sealed trait Type {
def namedType: Type with Named = {
@tailrec
def getNamedType(tpe: Type): Type with Named =
tpe match {
case OptionInputType(ofType) => getNamedType(ofType)
case OptionType(ofType) => getNamedType(ofType)
case ListInputType(ofType) => getNamedType(ofType)
case ListType(ofType) => getNamedType(ofType)
case n: Named => n
case t => throw new IllegalStateException("Expected named type, but got: " + t)
}
getNamedType(this)
}
}
sealed trait InputType[+T] extends Type {
lazy val isOptional: Boolean = this match {
case _: OptionInputType[_] => true
case _ => false
}
lazy val isList: Boolean = this match {
case _: ListInputType[_] => true
case _ => false
}
lazy val isNamed: Boolean = !(isOptional && isList)
lazy val nonOptionalType: InputType[_] = this match {
case tpe: OptionInputType[_] => tpe.ofType
case tpe => tpe
}
def namedInputType: InputType[_] = namedType.asInstanceOf[InputType[_]]
}
sealed trait OutputType[+T] extends Type
sealed trait LeafType extends Type with Named with HasAstInfo
sealed trait CompositeType[T] extends Type with Named with OutputType[T]
sealed trait AbstractType extends Type with Named {
def name: String
def typeOf[Ctx](value: Any, schema: Schema[Ctx, _]): Option[ObjectType[Ctx, _]] =
schema.possibleTypes
.get(name)
.flatMap(_.find(_.isInstanceOf(value)).asInstanceOf[Option[ObjectType[Ctx, _]]])
}
sealed trait MappedAbstractType[T] extends Type with AbstractType with OutputType[T] {
def contraMap(value: T): Any
}
sealed trait NullableType
sealed trait UnmodifiedType
sealed trait HasDescription {
/** A description of this schema element that can be presented to clients of the GraphQL service.
*/
def description: Option[String]
}
sealed trait Named extends HasDescription {
def name: String
def rename(newName: String): this.type
}
sealed trait HasDeprecation {
def deprecationReason: Option[String]
}
sealed trait HasAstInfo {
def astDirectives: Vector[ast.Directive]
def astNodes: Vector[ast.AstNode]
}
object Named {
val NameRegexp: Regex = """^[_a-zA-Z][_a-zA-Z0-9]*$""".r
def isValidName(name: String): Boolean = NameRegexp.pattern.matcher(name).matches()
}
/** Defines a GraphQL scalar value type.
*
* `coerceOutput` is allowed to return following scala values:
*
* - String
* - Boolean
* - Int
* - Long
* - Float
* - Double
* - scala.BigInt
* - scala.BigDecimal
* - sangria.ast.Value (it would be converted to raw scala value before given to a marshalling
* API)
*
* It may also return other values as well as long as underlying marshalling library supports them.
*
* You can provide additional meta-information to marshalling API with `scalarInfo`.
*
* @param description
* A description of this schema element that can be presented to clients of the GraphQL service.
*/
case class ScalarType[T](
name: String,
description: Option[String] = None,
coerceUserInput: Any => Either[Violation, T],
coerceOutput: (T, Set[MarshallerCapability]) => Any,
coerceInput: ast.Value => Either[Violation, T],
complexity: Double = 0.0d,
scalarInfo: Set[ScalarValueInfo] = Set.empty,
astDirectives: Vector[ast.Directive] = Vector.empty,
astNodes: Vector[ast.AstNode] = Vector.empty
) extends InputType[T @@ CoercedScalaResult]
with OutputType[T]
with LeafType
with NullableType
with UnmodifiedType
with Named {
def rename(newName: String): this.type = copy(name = newName).asInstanceOf[this.type]
def toAst: ast.TypeDefinition = SchemaRenderer.renderType(this)
}
case class ScalarAlias[T, ST](
aliasFor: ScalarType[ST],
toScalar: T => ST,
fromScalar: ST => Either[Violation, T]
) extends InputType[T @@ CoercedScalaResult]
with OutputType[T]
with LeafType
with NullableType
with UnmodifiedType
with Named {
def name: String = aliasFor.name
def description: Option[String] = aliasFor.description
def rename(newName: String): this.type =
copy(aliasFor = aliasFor.rename(newName)).asInstanceOf[this.type]
override def astDirectives: Vector[ast.Directive] = aliasFor.astDirectives
override def astNodes: Vector[AstNode] = aliasFor.astNodes
def toAst: ast.TypeDefinition = SchemaRenderer.renderType(this)
}
sealed trait ObjectLikeType[Ctx, Val]
extends OutputType[Val]
with CompositeType[Val]
with NullableType
with UnmodifiedType
with Named
with HasAstInfo {
def interfaces: List[InterfaceType[Ctx, _]]
def fieldsFn: () => List[Field[Ctx, Val]]
lazy val ownFields: Vector[Field[Ctx, Val]] = fieldsFn().toVector
private def removeDuplicates[T, E](list: Vector[T], valueFn: T => E) =
list
.foldLeft((Vector.empty, Vector.empty): (Vector[E], Vector[T])) {
case (a @ (visited, _), e) if visited contains valueFn(e) => a
case ((visited, acc), e) => (visited :+ valueFn(e), acc :+ e)
}
._2
lazy val allInterfaces: Vector[InterfaceType[Ctx, _]] =
removeDuplicates(
interfaces.toVector.flatMap(i => i +: i.allInterfaces),
(i: InterfaceType[Ctx, _]) => i.name)
lazy val fields: Vector[Field[Ctx, _]] =
ownFields ++ interfaces.flatMap(i => i.fields)
lazy val uniqueFields: Vector[Field[Ctx, _]] =
removeDuplicates(fields, (e: Field[Ctx, _]) => e.name)
lazy val fieldsByName: Map[String, Vector[Field[Ctx, _]]] = fields.groupBy(_.name)
def getField(schema: Schema[_, _], fieldName: String): Vector[Field[Ctx, _]] =
if (sangria.introspection.MetaFieldNames contains fieldName)
if (fieldName == SchemaMetaField.name && name == schema.query.name)
Vector(SchemaMetaField.asInstanceOf[Field[Ctx, _]])
else if (fieldName == TypeMetaField.name && name == schema.query.name)
Vector(TypeMetaField.asInstanceOf[Field[Ctx, _]])
else if (fieldName == TypeNameMetaField.name)
Vector(TypeNameMetaField.asInstanceOf[Field[Ctx, _]])
else Vector.empty
else fieldsByName.getOrElse(fieldName, Vector.empty)
def toAst: ast.TypeDefinition = SchemaRenderer.renderType(this)
}
/** GraphQL schema object description.
*
* Describes a type of object in a GraphQL schema that is presented by a Sangria server. Objects of
* the type contain [[fieldsFn fields]] and can be viewed as simply a container of fields—internal
* nodes in the tree of data that a GraphQL request returns. The data store operations take place
* at the level of the [[Field fields]] that are leaf nodes in that tree.
*
* Constructing the internal nodes of a [[Schema schema]] consists mostly of constructing instances
* of this class.
*
* @param description
* A description of this schema element that can be presented to clients of the GraphQL service.
* @see
* [[sangria.ast.ObjectTypeDefinition]]
*/
case class ObjectType[Ctx, Val: ClassTag](
name: String,
description: Option[String],
fieldsFn: () => List[Field[Ctx, Val]],
interfaces: List[InterfaceType[Ctx, _]],
instanceCheck: (Any, Class[_], ObjectType[Ctx, Val]) => Boolean,
astDirectives: Vector[ast.Directive],
astNodes: Vector[ast.AstNode]
) extends ObjectLikeType[Ctx, Val] {
lazy val valClass: Class[_] = implicitly[ClassTag[Val]].runtimeClass
def withInstanceCheck(
fn: (Any, Class[_], ObjectType[Ctx, Val]) => Boolean): ObjectType[Ctx, Val] =
copy(instanceCheck = fn)
def isInstanceOf(value: Any): Boolean = instanceCheck(value, valClass, this)
def rename(newName: String): this.type = copy(name = newName).asInstanceOf[this.type]
}
object ObjectType {
def apply[Ctx, Val: ClassTag](name: String, fields: List[Field[Ctx, Val]]): ObjectType[Ctx, Val] =
ObjectType(
name,
None,
fieldsFn = () => fields,
Nil,
instanceCheck = defaultInstanceCheck,
Vector.empty,
Vector.empty)
def apply[Ctx, Val: ClassTag](
name: String,
description: String,
fields: List[Field[Ctx, Val]]): ObjectType[Ctx, Val] =
ObjectType(
name,
Some(description),
fieldsFn = () => fields,
Nil,
instanceCheck = defaultInstanceCheck,
Vector.empty,
Vector.empty)
def apply[Ctx, Val: ClassTag](
name: String,
interfaces: List[PossibleInterface[Ctx, Val]],
fields: List[Field[Ctx, Val]]): ObjectType[Ctx, Val] =
ObjectType(
name,
None,
fieldsFn = () => fields,
interfaces.map(_.interfaceType),
instanceCheck = defaultInstanceCheck,
Vector.empty,
Vector.empty)
def apply[Ctx, Val: ClassTag](
name: String,
description: String,
interfaces: List[PossibleInterface[Ctx, Val]],
fields: List[Field[Ctx, Val]]): ObjectType[Ctx, Val] =
ObjectType(
name,
Some(description),
fieldsFn = () => fields,
interfaces.map(_.interfaceType),
instanceCheck = defaultInstanceCheck,
Vector.empty,
Vector.empty)
def apply[Ctx, Val: ClassTag](
name: String,
fieldsFn: () => List[Field[Ctx, Val]]): ObjectType[Ctx, Val] =
ObjectType(
name,
None,
fieldsFn,
Nil,
instanceCheck = defaultInstanceCheck,
Vector.empty,
Vector.empty)
def apply[Ctx, Val: ClassTag](
name: String,
description: String,
fieldsFn: () => List[Field[Ctx, Val]]): ObjectType[Ctx, Val] =
ObjectType(
name,
Some(description),
fieldsFn,
Nil,
instanceCheck = defaultInstanceCheck,
Vector.empty,
Vector.empty)
def apply[Ctx, Val: ClassTag](
name: String,
interfaces: List[PossibleInterface[Ctx, Val]],
fieldsFn: () => List[Field[Ctx, Val]]): ObjectType[Ctx, Val] =
ObjectType(
name,
None,
fieldsFn,
interfaces.map(_.interfaceType),
instanceCheck = defaultInstanceCheck,
Vector.empty,
Vector.empty)
def apply[Ctx, Val: ClassTag](
name: String,
description: String,
interfaces: List[PossibleInterface[Ctx, Val]],
fieldsFn: () => List[Field[Ctx, Val]]): ObjectType[Ctx, Val] =
ObjectType(
name,
Some(description),
fieldsFn,
interfaces.map(_.interfaceType),
instanceCheck = defaultInstanceCheck,
Vector.empty,
Vector.empty)
def createFromMacro[Ctx, Val: ClassTag](
name: String,
description: Option[String],
interfaces: List[InterfaceType[Ctx, _]],
fieldsFn: () => List[Field[Ctx, Val]]) =
ObjectType(
name,
description,
fieldsFn,
interfaces,
instanceCheck = defaultInstanceCheck,
Vector.empty,
Vector.empty)
implicit def acceptUnitCtx[Ctx, Val](objectType: ObjectType[Unit, Val]): ObjectType[Ctx, Val] =
objectType.asInstanceOf[ObjectType[Ctx, Val]]
def defaultInstanceCheck[Ctx, Val]: (Any, Class[_], ObjectType[Ctx, Val]) => Boolean =
(value, valClass, tpe) => valClass.isAssignableFrom(value.getClass)
}
/** @param description
* A description of this schema element that can be presented to clients of the GraphQL service.
*/
case class InterfaceType[Ctx, Val](
name: String,
description: Option[String] = None,
fieldsFn: () => List[Field[Ctx, Val]],
interfaces: List[InterfaceType[Ctx, _]],
manualPossibleTypes: () => List[ObjectType[_, _]],
astDirectives: Vector[ast.Directive],
astNodes: Vector[ast.AstNode] = Vector.empty
) extends ObjectLikeType[Ctx, Val]
with AbstractType {
def withPossibleTypes(possible: PossibleObject[Ctx, Val]*): InterfaceType[Ctx, Val] =
copy(manualPossibleTypes = () => possible.toList.map(_.objectType))
def withPossibleTypes(possible: () => List[PossibleObject[Ctx, Val]]): InterfaceType[Ctx, Val] =
copy(manualPossibleTypes = () => possible().map(_.objectType))
def rename(newName: String): this.type = copy(name = newName).asInstanceOf[this.type]
}
object InterfaceType {
val emptyPossibleTypes: () => List[ObjectType[_, _]] = () => Nil
def apply[Ctx, Val](name: String, fields: List[Field[Ctx, Val]]): InterfaceType[Ctx, Val] =
InterfaceType(
name,
None,
fieldsFn = () => fields,
Nil,
emptyPossibleTypes,
Vector.empty,
Vector.empty)
def apply[Ctx, Val](
name: String,
description: String,
fields: List[Field[Ctx, Val]]): InterfaceType[Ctx, Val] =
InterfaceType(
name,
Some(description),
fieldsFn = () => fields,
Nil,
emptyPossibleTypes,
Vector.empty,
Vector.empty)
def apply[Ctx, Val](
name: String,
fields: List[Field[Ctx, Val]],
interfaces: List[PossibleInterface[Ctx, Val]]): InterfaceType[Ctx, Val] =
InterfaceType(
name,
None,
fieldsFn = () => fields,
interfaces.map(_.interfaceType),
emptyPossibleTypes,
Vector.empty,
Vector.empty)
def apply[Ctx, Val](
name: String,
description: String,
fields: List[Field[Ctx, Val]],
interfaces: List[PossibleInterface[Ctx, Val]]): InterfaceType[Ctx, Val] =
InterfaceType(
name,
Some(description),
fieldsFn = () => fields,
interfaces.map(_.interfaceType),
emptyPossibleTypes,
Vector.empty,
Vector.empty)
def apply[Ctx, Val](
name: String,
fieldsFn: () => List[Field[Ctx, Val]]): InterfaceType[Ctx, Val] =
InterfaceType(name, None, fieldsFn, Nil, emptyPossibleTypes, Vector.empty, Vector.empty)
def apply[Ctx, Val](
name: String,
description: String,
fieldsFn: () => List[Field[Ctx, Val]]): InterfaceType[Ctx, Val] =
InterfaceType(
name,
Some(description),
fieldsFn,
Nil,
emptyPossibleTypes,
Vector.empty,
Vector.empty)
def apply[Ctx, Val](
name: String,
fieldsFn: () => List[Field[Ctx, Val]],
interfaces: List[PossibleInterface[Ctx, Val]]): InterfaceType[Ctx, Val] =
InterfaceType(
name,
None,
fieldsFn,
interfaces.map(_.interfaceType),
emptyPossibleTypes,
Vector.empty,
Vector.empty)
def apply[Ctx, Val](
name: String,
description: String,
fieldsFn: () => List[Field[Ctx, Val]],
interfaces: List[PossibleInterface[Ctx, Val]]): InterfaceType[Ctx, Val] =
InterfaceType(
name,
Some(description),
fieldsFn,
interfaces.map(_.interfaceType),
emptyPossibleTypes,
Vector.empty,
Vector.empty)
}
case class PossibleInterface[Ctx, Concrete](interfaceType: InterfaceType[Ctx, _])
object PossibleInterface extends PossibleInterfaceLowPrioImplicits {
def apply[Ctx, Abstract, Concrete](interface: InterfaceType[Ctx, Abstract])(implicit
ev: PossibleType[Abstract, Concrete]): PossibleInterface[Ctx, Concrete] =
PossibleInterface[Ctx, Concrete](interface)
implicit def convert[Ctx, Abstract, Concrete](interface: InterfaceType[Ctx, Abstract])(implicit
ev: PossibleType[Abstract, Concrete]): PossibleInterface[Ctx, Concrete] =
PossibleInterface[Ctx, Concrete](interface)
}
trait PossibleInterfaceLowPrioImplicits {
implicit def applyUnit[Ctx, Abstract, Concrete](interface: InterfaceType[Ctx, Abstract])(implicit
ev: PossibleType[Abstract, Concrete]): PossibleInterface[Unit, Concrete] =
PossibleInterface[Unit, Concrete](interface.asInstanceOf[InterfaceType[Unit, Abstract]])
}
case class PossibleObject[Ctx, Abstract](objectType: ObjectType[Ctx, _])
object PossibleObject {
implicit def apply[Ctx, Abstract, Concrete](obj: ObjectType[Ctx, Concrete])(implicit
ev: PossibleType[Abstract, Concrete]): PossibleObject[Ctx, Abstract] =
PossibleObject[Ctx, Abstract](obj)
implicit def applyUnit[Ctx, Abstract, Concrete](obj: ObjectType[Unit, Concrete])(implicit
ev: PossibleType[Abstract, Concrete]): PossibleObject[Ctx, Abstract] =
PossibleObject[Ctx, Abstract](obj.asInstanceOf[ObjectType[Ctx, Concrete]])
}
trait PossibleType[AbstrType, ConcreteType]
object PossibleType {
private object SingletonPossibleType extends PossibleType[AnyRef, AnyRef]
def create[AbstrType, ConcreteType]: PossibleType[AbstrType, ConcreteType] =
SingletonPossibleType.asInstanceOf[PossibleType[AbstrType, ConcreteType]]
implicit def InheritanceBasedPossibleType[Abstract, Concrete](implicit
ev: Concrete <:< Abstract): PossibleType[Abstract, Concrete] =
create[Abstract, Concrete]
}
/** @param description
* A description of this schema element that can be presented to clients of the GraphQL service.
*/
case class UnionType[Ctx](
name: String,
description: Option[String] = None,
typesFn: () => List[ObjectType[Ctx, _]],
astDirectives: Vector[ast.Directive] = Vector.empty,
astNodes: Vector[ast.AstNode] = Vector.empty)
extends OutputType[Any]
with CompositeType[Any]
with AbstractType
with NullableType
with UnmodifiedType
with HasAstInfo {
def rename(newName: String): this.type = copy(name = newName).asInstanceOf[this.type]
def toAst: ast.TypeDefinition = SchemaRenderer.renderType(this)
/** Creates a type-safe version of union type which might be useful in cases where the value is
* wrapped in a type like `Either`.
*/
def mapValue[T](func: T => Any): OutputType[T] =
new UnionType[Ctx](name, description, typesFn, astDirectives, astNodes)
with MappedAbstractType[T] {
override def contraMap(value: T): Any = func(value)
}.asInstanceOf[OutputType[T]]
lazy val types: List[ObjectType[Ctx, _]] = typesFn()
}
object UnionType {
def apply[Ctx](name: String, types: List[ObjectType[Ctx, _]]): UnionType[Ctx] =
UnionType[Ctx](name, None, () => types)
def apply[Ctx](
name: String,
description: Option[String],
types: List[ObjectType[Ctx, _]]): UnionType[Ctx] =
UnionType[Ctx](name, description, () => types)
def apply[Ctx](
name: String,
description: Option[String],
types: List[ObjectType[Ctx, _]],
astDirectives: Vector[ast.Directive]): UnionType[Ctx] =
UnionType[Ctx](name, description, () => types, astDirectives)
def apply[Ctx](
name: String,
description: Option[String],
types: List[ObjectType[Ctx, _]],
astDirectives: Vector[ast.Directive],
astNodes: Vector[ast.AstNode]): UnionType[Ctx] =
UnionType[Ctx](name, description, () => types, astDirectives, astNodes)
}
/** @param description
* A description of this schema element that can be presented to clients of the GraphQL service.
* @param resolve
* A function that maps the context of this field definition to an action that retrieves the
* field's data.
*/
case class Field[Ctx, Val](
name: String,
fieldType: OutputType[_],
description: Option[String],
arguments: List[Argument[_]],
resolve: Context[Ctx, Val] => Action[Ctx, _],
deprecationReason: Option[String],
tags: List[FieldTag],
complexity: Option[(Ctx, Args, Double) => Double],
manualPossibleTypes: () => List[ObjectType[_, _]],
astDirectives: Vector[ast.Directive],
astNodes: Vector[ast.AstNode])
extends Named
with HasArguments
with HasDeprecation
with HasAstInfo {
def withPossibleTypes(possible: PossibleObject[Ctx, Val]*): Field[Ctx, Val] =
copy(manualPossibleTypes = () => possible.toList.map(_.objectType))
def withPossibleTypes(possible: () => List[PossibleObject[Ctx, Val]]): Field[Ctx, Val] =
copy(manualPossibleTypes = () => possible().map(_.objectType))
def rename(newName: String): this.type = copy(name = newName).asInstanceOf[this.type]
def toAst: ast.FieldDefinition = SchemaRenderer.renderField(this)
}
object Field {
def apply[Ctx, Val, Res, Out](
name: String,
fieldType: OutputType[Out],
description: Option[String] = None,
arguments: List[Argument[_]] = Nil,
resolve: Context[Ctx, Val] => Action[Ctx, Res],
possibleTypes: => List[PossibleObject[_, _]] = Nil,
tags: List[FieldTag] = Nil,
complexity: Option[(Ctx, Args, Double) => Double] = None,
deprecationReason: Option[String] = None)(implicit
ev: ValidOutType[Res, Out]): Field[Ctx, Val] =
Field[Ctx, Val](
name,
fieldType,
description,
arguments,
resolve,
deprecationReason,
tags,
complexity,
() => possibleTypes.map(_.objectType),
Vector.empty,
Vector.empty)
def subs[Ctx, Val, StreamSource, Res, Out](
name: String,
fieldType: OutputType[Out],
description: Option[String] = None,
arguments: List[Argument[_]] = Nil,
resolve: Context[Ctx, Val] => StreamSource,
possibleTypes: => List[PossibleObject[_, _]] = Nil,
tags: List[FieldTag] = Nil,
complexity: Option[(Ctx, Args, Double) => Double] = None,
deprecationReason: Option[String] = None
)(implicit
stream: SubscriptionStreamLike[StreamSource, Action, Ctx, Res, Out]): Field[Ctx, Val] = {
val s = stream.subscriptionStream
Field[Ctx, Val](
name,
fieldType,
description,
arguments,
ctx => SubscriptionValue[Ctx, StreamSource, stream.StreamSource](resolve(ctx), s),
deprecationReason,
SubscriptionField[stream.StreamSource](s) +: tags,
complexity,
() => possibleTypes.map(_.objectType),
Vector.empty,
Vector.empty
)
}
}
@implicitNotFound(msg = "${Res} is invalid type for the resulting GraphQL type ${Out}.")
trait ValidOutType[-Res, +Out]
object ValidOutType {
private val valid = new ValidOutType[Any, Any] {}
implicit def validSubclass[Res, Out](implicit ev: Res <:< Out): ValidOutType[Res, Out] =
valid.asInstanceOf[ValidOutType[Res, Out]]
implicit def validNothing[Out]: ValidOutType[Nothing, Out] =
valid.asInstanceOf[ValidOutType[Nothing, Out]]
implicit def validOption[Res, Out](implicit ev: Res <:< Out): ValidOutType[Res, Option[Out]] =
valid.asInstanceOf[ValidOutType[Res, Option[Out]]]
}
trait InputValue[T] {
def name: String
def inputValueType: InputType[_]
def description: Option[String]
def defaultValue: Option[(_, ToInput[_, _])]
}
/** @param description
* A description of this schema element that can be presented to clients of the GraphQL service.
*/
case class Argument[T](
name: String,
argumentType: InputType[_],
description: Option[String],
defaultValue: Option[(_, ToInput[_, _])],
fromInput: FromInput[_],
astDirectives: Vector[ast.Directive],
astNodes: Vector[ast.AstNode])
extends InputValue[T]
with Named
with HasAstInfo {
override def inputValueType: InputType[_] = argumentType
override def rename(newName: String): this.type = copy(name = newName).asInstanceOf[this.type]
def toAst: ast.InputValueDefinition = SchemaRenderer.renderArg(this)
}
object Argument {
def apply[T, Default](
name: String,
argumentType: InputType[T],
description: String,
defaultValue: Default)(implicit
toInput: ToInput[Default, _],
fromInput: FromInput[T],
res: ArgumentType[T]): Argument[res.Res] =
Argument(
name,
argumentType,
Some(description),
Some(defaultValue -> toInput),
fromInput,
Vector.empty,
Vector.empty)
def apply[T, Default](name: String, argumentType: InputType[T], defaultValue: Default)(implicit
toInput: ToInput[Default, _],
fromInput: FromInput[T],
res: ArgumentType[T]): Argument[res.Res] =
Argument(
name,
argumentType,
None,
Some(defaultValue -> toInput),
fromInput,
Vector.empty,
Vector.empty)
def apply[T](name: String, argumentType: InputType[T], description: String)(implicit
fromInput: FromInput[T],
res: WithoutInputTypeTags[T]): Argument[res.Res] =
Argument(name, argumentType, Some(description), None, fromInput, Vector.empty, Vector.empty)
def apply[T](name: String, argumentType: InputType[T])(implicit
fromInput: FromInput[T],
res: WithoutInputTypeTags[T]): Argument[res.Res] =
Argument(name, argumentType, None, None, fromInput, Vector.empty, Vector.empty)
def createWithoutDefault[T](
name: String,
argumentType: InputType[T],
description: Option[String])(implicit
fromInput: FromInput[T],
res: ArgumentType[T]): Argument[res.Res] =
Argument(name, argumentType, description, None, fromInput, Vector.empty, Vector.empty)
def createWithDefault[T, Default](
name: String,
argumentType: InputType[T],
description: Option[String],
defaultValue: Default)(implicit
toInput: ToInput[Default, _],
fromInput: FromInput[T],
res: ArgumentType[T]): Argument[res.Res] =
Argument(
name,
argumentType,
description,
Some(defaultValue -> toInput),
fromInput,
Vector.empty,
Vector.empty)
}
trait WithoutInputTypeTags[T] {
type Res
}
object WithoutInputTypeTags extends WithoutInputTypeTagsLowPrio {
implicit def coercedArgTpe[T]: WithoutInputTypeTags[T @@ CoercedScalaResult] { type Res = T } =
new WithoutInputTypeTags[T @@ CoercedScalaResult] {
type Res = T
}
implicit def coercedOptArgTpe[T]
: WithoutInputTypeTags[Option[T @@ CoercedScalaResult]] { type Res = Option[T] } =
new WithoutInputTypeTags[Option[T @@ CoercedScalaResult]] {
type Res = Option[T]
}
implicit def coercedSeqOptArgTpe[T]
: WithoutInputTypeTags[Seq[Option[T @@ CoercedScalaResult]]] { type Res = Seq[Option[T]] } =
new WithoutInputTypeTags[Seq[Option[T @@ CoercedScalaResult]]] {
type Res = Seq[Option[T]]
}
implicit def coercedOptSeqArgTpe[T]
: WithoutInputTypeTags[Option[Seq[T @@ CoercedScalaResult]]] { type Res = Option[Seq[T]] } =
new WithoutInputTypeTags[Option[Seq[T @@ CoercedScalaResult]]] {
type Res = Option[Seq[T]]
}
implicit def coercedOptSeqOptArgTpe[T]
: WithoutInputTypeTags[Option[Seq[Option[T @@ CoercedScalaResult]]]] {
type Res = Option[Seq[Option[T]]]
} = new WithoutInputTypeTags[Option[Seq[Option[T @@ CoercedScalaResult]]]] {
type Res = Option[Seq[Option[T]]]
}
implicit def ioArgTpe[T]: WithoutInputTypeTags[T @@ InputObjectResult] { type Res = T } =
new WithoutInputTypeTags[T @@ InputObjectResult] {
type Res = T
}
implicit def ioOptArgTpe[T]
: WithoutInputTypeTags[Option[T @@ InputObjectResult]] { type Res = Option[T] } =
new WithoutInputTypeTags[Option[T @@ InputObjectResult]] {
type Res = Option[T]
}
implicit def ioSeqOptArgTpe[T]
: WithoutInputTypeTags[Seq[Option[T @@ InputObjectResult]]] { type Res = Seq[Option[T]] } =
new WithoutInputTypeTags[Seq[Option[T @@ InputObjectResult]]] {
type Res = Seq[Option[T]]
}
implicit def ioOptSeqArgTpe[T]
: WithoutInputTypeTags[Option[Seq[T @@ InputObjectResult]]] { type Res = Option[Seq[T]] } =
new WithoutInputTypeTags[Option[Seq[T @@ InputObjectResult]]] {
type Res = Option[Seq[T]]
}
implicit def ioOptSeqOptArgTpe[T]
: WithoutInputTypeTags[Option[Seq[Option[T @@ InputObjectResult]]]] {
type Res = Option[Seq[Option[T]]]
} = new WithoutInputTypeTags[Option[Seq[Option[T @@ InputObjectResult]]]] {
type Res = Option[Seq[Option[T]]]
}
}
trait WithoutInputTypeTagsLowPrio {
implicit def defaultArgTpe[T]: WithoutInputTypeTags[T] { type Res = T } =
new WithoutInputTypeTags[T] {
type Res = T
}
}
trait ArgumentType[T] {
type Res
}
object ArgumentType extends ArgumentTypeLowPrio {
implicit def coercedArgTpe[T]: ArgumentType[T @@ CoercedScalaResult] { type Res = T } =
new ArgumentType[T @@ CoercedScalaResult] {
type Res = T
}
implicit def coercedOptArgTpe[T]: ArgumentType[Option[T @@ CoercedScalaResult]] { type Res = T } =
new ArgumentType[Option[T @@ CoercedScalaResult]] {
type Res = T
}
implicit def coercedSeqOptArgTpe[T]
: ArgumentType[Seq[Option[T @@ CoercedScalaResult]]] { type Res = Seq[Option[T]] } =
new ArgumentType[Seq[Option[T @@ CoercedScalaResult]]] {
type Res = Seq[Option[T]]
}
implicit def coercedOptSeqArgTpe[T]
: ArgumentType[Option[Seq[T @@ CoercedScalaResult]]] { type Res = Seq[T] } =
new ArgumentType[Option[Seq[T @@ CoercedScalaResult]]] {
type Res = Seq[T]
}
implicit def coercedOptSeqOptArgTpe[T]
: ArgumentType[Option[Seq[Option[T @@ CoercedScalaResult]]]] { type Res = Seq[Option[T]] } =
new ArgumentType[Option[Seq[Option[T @@ CoercedScalaResult]]]] {
type Res = Seq[Option[T]]
}
implicit def ioArgTpe[T]: ArgumentType[T @@ InputObjectResult] { type Res = T } =
new ArgumentType[T @@ InputObjectResult] {
type Res = T
}
implicit def ioOptArgTpe[T]: ArgumentType[Option[T @@ InputObjectResult]] { type Res = T } =
new ArgumentType[Option[T @@ InputObjectResult]] {
type Res = T
}
implicit def ioSeqOptArgTpe[T]
: ArgumentType[Seq[Option[T @@ InputObjectResult]]] { type Res = Seq[Option[T]] } =
new ArgumentType[Seq[Option[T @@ InputObjectResult]]] {
type Res = Seq[Option[T]]
}
implicit def ioOptSeqArgTpe[T]
: ArgumentType[Option[Seq[T @@ InputObjectResult]]] { type Res = Seq[T] } =
new ArgumentType[Option[Seq[T @@ InputObjectResult]]] {
type Res = Seq[T]
}
implicit def ioOptSeqOptArgTpe[T]
: ArgumentType[Option[Seq[Option[T @@ InputObjectResult]]]] { type Res = Seq[Option[T]] } =
new ArgumentType[Option[Seq[Option[T @@ InputObjectResult]]]] {
type Res = Seq[Option[T]]
}
}
trait ArgumentTypeLowPrio extends ArgumentTypeLowestPrio {
implicit def optionArgTpe[T]: ArgumentType[Option[T]] { type Res = T } =
new ArgumentType[Option[T]] {
type Res = T
}
}
trait ArgumentTypeLowestPrio {
implicit def defaultArgTpe[T]: ArgumentType[T] { type Res = T } = new ArgumentType[T] {
type Res = T
}
}
/** @param description
* A description of this schema element that can be presented to clients of the GraphQL service.
*/
case class EnumType[T](
name: String,
description: Option[String] = None,
values: List[EnumValue[T]],
astDirectives: Vector[ast.Directive] = Vector.empty,
astNodes: Vector[ast.AstNode] = Vector.empty)
extends InputType[T @@ CoercedScalaResult]
with OutputType[T]
with LeafType
with NullableType
with UnmodifiedType
with Named
with HasAstInfo {
lazy val byName: Map[String, EnumValue[T]] =
values.groupBy(_.name).map { case (k, v) => (k, v.head) }
lazy val byValue: Map[T, EnumValue[T]] =
values.groupBy(_.value).map { case (k, v) => (k, v.head) }
def coerceUserInput(value: Any): Either[Violation, (T, Boolean)] = value match {
case valueName: String =>
byName
.get(valueName)
.map(v => Right(v.value -> v.deprecationReason.isDefined))
.getOrElse(Left(EnumValueCoercionViolation(valueName, name, values.map(_.name))))
case v if byValue.exists(_._1 == v) =>
Right(v.asInstanceOf[T] -> byValue(v.asInstanceOf[T]).deprecationReason.isDefined)
case _ => Left(EnumCoercionViolation)
}
def coerceInput(value: ast.Value): Either[Violation, (T, Boolean)] = value match {
case ast.EnumValue(valueName, _, _) =>
byName
.get(valueName)
.map(v => Right(v.value -> v.deprecationReason.isDefined))
.getOrElse(Left(EnumValueCoercionViolation(valueName, name, values.map(_.name))))
case _ => Left(EnumCoercionViolation)
}
def coerceOutput(value: T): String = byValue(value).name
def rename(newName: String): this.type = copy(name = newName).asInstanceOf[this.type]
def toAst: ast.TypeDefinition = SchemaRenderer.renderType(this)
}
/** @param description
* A description of this schema element that can be presented to clients of the GraphQL service.
*/
case class EnumValue[+T](
name: String,
description: Option[String] = None,
value: T,
deprecationReason: Option[String] = None,
astDirectives: Vector[ast.Directive] = Vector.empty,
astNodes: Vector[ast.AstNode] = Vector.empty)
extends Named
with HasDeprecation
with HasAstInfo {
def rename(newName: String): this.type = copy(name = newName).asInstanceOf[this.type]
def toAst: ast.EnumValueDefinition = SchemaRenderer.renderEnumValue(this)
}
/** @param description
* A description of this schema element that can be presented to clients of the GraphQL service.
*/
case class InputObjectType[T](
name: String,
description: Option[String] = None,
fieldsFn: () => List[InputField[_]],
astDirectives: Vector[ast.Directive],
astNodes: Vector[ast.AstNode]
) extends InputType[T @@ InputObjectResult]
with NullableType
with UnmodifiedType
with Named
with HasAstInfo {
lazy val fields: List[InputField[_]] = fieldsFn()