forked from fsprojects/FSharp.Data.GraphQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeSystem.fs
More file actions
1434 lines (1236 loc) · 50.1 KB
/
Copy pathTypeSystem.fs
File metadata and controls
1434 lines (1236 loc) · 50.1 KB
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
/// The MIT License (MIT)
/// Copyright (c) 2016 Bazinga Technologies Inc
namespace FSharp.Data.GraphQL.Types
open System
open System.Collections.Concurrent
open Hopac
open Hopac.Extensions
open FSharp.Data.GraphQL
open FSharp.Data.GraphQL.Ast
[<Flags; Serializable>]
type DirectiveLocation =
| QUERY = 1
| MUTATION = 2
| SUBSCRIPTION = 4
| FIELD = 8
| FRAGMENT_DEFINITION = 16
| FRAGMENT_SPREAD = 32
| INLINE_FRAGMENT = 64
module Introspection =
[<Serializable>]
type TypeKind =
| SCALAR = 1
| OBJECT = 2
| INTERFACE = 3
| UNION = 4
| ENUM = 5
| INPUT_OBJECT = 6
| LIST = 7
| NON_NULL = 8
[<Serializable>]
type IntrospectionDirective =
{ Name : string
Description : string option
Locations : DirectiveLocation []
Args : IntrospectionInputVal [] }
and [<Serializable>] IntrospectionType =
{ Kind : TypeKind
Name : string
Description : string option
Fields : IntrospectionField [] option
Interfaces : IntrospectionTypeRef [] option
PossibleTypes : IntrospectionTypeRef [] option
EnumValues : IntrospectionEnumVal [] option
InputFields : IntrospectionInputVal [] option
OfType : IntrospectionTypeRef option }
static member Scalar(name: string, description: string option) =
{ Kind = TypeKind.SCALAR
Name = name
Description = description
Fields = None
Interfaces = None
PossibleTypes = None
EnumValues = None
InputFields = None
OfType = None }
static member Object(name: string, description: string option, fields: IntrospectionField [], interfaces: IntrospectionTypeRef []) =
{ Kind = TypeKind.OBJECT
Name = name
Description = description
Fields = Some fields
Interfaces = Some interfaces
PossibleTypes = None
EnumValues = None
InputFields = None
OfType = None }
static member InputObject(name: string, description: string option, inputFields: IntrospectionInputVal []) =
{ Kind = TypeKind.INPUT_OBJECT
Name = name
Description = description
Fields = None
Interfaces = None
PossibleTypes = None
EnumValues = None
InputFields = Some inputFields
OfType = None }
static member Union(name: string, description: string option, possibleTypes: IntrospectionTypeRef []) =
{ Kind = TypeKind.UNION
Name = name
Description = description
Fields = None
Interfaces = None
PossibleTypes = Some possibleTypes
EnumValues = None
InputFields = None
OfType = None }
static member Enum(name: string, description: string option, enumValues: IntrospectionEnumVal []) =
{ Kind = TypeKind.ENUM
Name = name
Description = description
Fields = None
Interfaces = None
PossibleTypes = None
EnumValues = Some enumValues
InputFields = None
OfType = None }
static member Interface(name: string, description: string option, fields: IntrospectionField [], possibleTypes: IntrospectionTypeRef []) =
{ Kind = TypeKind.INTERFACE
Name = name
Description = description
Fields = Some fields
Interfaces = None
PossibleTypes = Some possibleTypes
EnumValues = None
InputFields = None
OfType = None }
and [<Serializable>] IntrospectionTypeRef =
{ Kind : TypeKind
Name : string option
Description : string option
OfType : IntrospectionTypeRef option }
static member List(inner: IntrospectionTypeRef) = { Kind = TypeKind.LIST; Name = None; Description = None; OfType = Some inner }
static member NonNull(inner: IntrospectionTypeRef) = { Kind = TypeKind.NON_NULL; Name = None; Description = None; OfType = Some inner }
static member Named(inner: IntrospectionType) = { Kind = inner.Kind; Name = Some inner.Name; Description = inner.Description; OfType = None }
and [<Serializable>] IntrospectionInputVal =
{ Name : string
Description : string option
Type : IntrospectionTypeRef
DefaultValue : string option }
and [<Serializable>] IntrospectionEnumVal =
{ Name : string
Description : string option
IsDeprecated : bool
DeprecationReason : string option }
and [<Serializable>] IntrospectionField =
{ Name : string
Description : string option
Args : IntrospectionInputVal []
Type : IntrospectionTypeRef
IsDeprecated : bool
DeprecationReason : string option }
and [<Serializable>] IntrospectionSchema =
{ QueryType : IntrospectionTypeRef
MutationType : IntrospectionTypeRef option
SubscriptionType : IntrospectionTypeRef option
Types : IntrospectionType []
Directives : IntrospectionDirective [] }
type ISchema =
interface
inherit seq<NamedDef>
abstract TypeMap : Map<string, NamedDef>
abstract Query : ObjectDef
abstract Mutation : ObjectDef option
abstract Directives : DirectiveDef []
abstract TryFindType : string -> NamedDef option
abstract GetPossibleTypes : AbstractDef -> ObjectDef []
abstract IsPossibleType : AbstractDef -> ObjectDef -> bool
abstract Introspected : Introspection.IntrospectionSchema
end
// 3.1 Types
and TypeDef =
interface
abstract MakeList: unit -> ListOfDef
abstract MakeNullable: unit -> NullableDef
end
and TypeDef<'Val> =
interface
inherit TypeDef
end
and InputDef =
interface
inherit TypeDef
abstract InputType: Type
end
and InputDef<'Val> =
interface
inherit InputDef
inherit TypeDef<'Val>
end
and OutputDef =
interface
inherit TypeDef
abstract OutputType: Type
end
and OutputDef<'Val> =
interface
inherit OutputDef
inherit TypeDef<'Val>
end
and LeafDef =
interface
inherit TypeDef
end
and CompositeDef =
interface
inherit TypeDef
end
and AbstractDef =
interface
inherit TypeDef
end
and NamedDef =
interface
inherit TypeDef
abstract Name : string
end
and ExecuteField = ResolveFieldContext -> obj -> Job<obj>
and FieldDef =
interface
abstract Name : string
abstract Description : string option
abstract DeprecationReason : string option
abstract Type : OutputDef
abstract Args : InputFieldDef []
abstract Resolve : ResolveFieldContext -> obj -> Job<obj>
abstract Execute : ExecuteField with get,set
inherit IEquatable<FieldDef>
end
and FieldDef<'Val> =
interface
abstract Resolve : ResolveFieldContext -> 'Val -> Job<obj>
inherit FieldDef
end
and ExecutionContext =
{
Schema: ISchema
RootValue: obj
Document: Document
Operation: OperationDefinition
Fragments: FragmentDefinition list
Variables: Map<string, obj>
Errors: ConcurrentBag<exn>
}
and ResolveFieldContext =
{ FieldName : string
Fields : Field []
FieldType : FieldDef
ReturnType : TypeDef
ParentType : ObjectDef
Schema : ISchema
Args : Map<string, obj>
Operation : OperationDefinition
Fragments : FragmentDefinition list
Variables : Map<string, obj>
ExecutionContext: ExecutionContext
AddError: exn -> unit }
member x.TryArg(name : string) : 't option =
match Map.tryFind name x.Args with
| Some o -> Some(o :?> 't)
| None -> None
member x.Arg(name : string) : 't =
downcast Map.find name x.Args
and ScalarDef =
interface
abstract Name : string
abstract Description : string option
abstract CoerceInput : Value -> obj option
abstract CoerceOutput : obj -> Value option
abstract CoerceValue : obj -> obj option
inherit IEquatable<ScalarDef>
inherit TypeDef
inherit NamedDef
inherit InputDef
inherit OutputDef
inherit LeafDef
end
/// 3.1.1.1 Build-in Scalars
and [<CustomEquality; NoComparison>] ScalarDefinition<'Val> =
{ Name : string
Description : string option
CoerceInput : Value -> 'Val option
CoerceOutput : 'Val -> Value option
CoerceValue : obj -> 'Val option }
interface InputDef with
member __.InputType = typeof<'Val>
interface TypeDef with
member x.MakeNullable() =
let nullable: NullableDefinition<_> = { OfType = x }
upcast nullable
member x.MakeList() =
let list: ListOfDefinition<_> = { OfType = x }
upcast list
interface OutputDef with
member __.OutputType = typeof<'Val>
interface ScalarDef with
member x.Name = x.Name
member x.Description = x.Description
member x.CoerceInput input = x.CoerceInput input |> Option.map box
member x.CoerceOutput output = x.CoerceOutput(output :?> 'Val)
member x.CoerceValue value = (x.CoerceValue value) |> Option.map box
interface InputDef<'Val>
interface OutputDef<'Val>
interface LeafDef
interface NamedDef with
member x.Name = x.Name
interface IEquatable<ScalarDef> with
member x.Equals s = x.Name = s.Name
override x.Equals y =
match y with
| :? ScalarDef as s -> (x :> IEquatable<ScalarDef>).Equals(s)
| _ -> false
override x.GetHashCode() = x.Name.GetHashCode()
override x.ToString() = x.Name
and EnumVal =
interface
abstract Name : string
abstract Description : string option
abstract Value : obj
abstract DeprecationReason : string option
end
and EnumValue<'Val> =
{ Name : string
Value : 'Val
Description : string option
DeprecationReason : string option }
interface EnumVal with
member x.Name = x.Name
member x.Description = x.Description
member x.DeprecationReason = x.DeprecationReason
member x.Value = upcast x.Value
override x.ToString() = x.Name
and EnumDef =
interface
abstract Name : string
abstract Description : string option
abstract Options : EnumVal []
inherit TypeDef
inherit InputDef
inherit OutputDef
inherit LeafDef
inherit NamedDef
end
and EnumDef<'Val> =
interface
abstract Options : EnumValue<'Val> []
inherit EnumDef
inherit TypeDef<'Val>
inherit InputDef<'Val>
inherit OutputDef<'Val>
end
and EnumDefinition<'Val> =
{ Name : string
Description : string option
Options : EnumValue<'Val> [] }
interface InputDef with
member __.InputType = typeof<'Val>
interface TypeDef with
member x.MakeNullable() =
let nullable: NullableDefinition<_> = { OfType = x }
upcast nullable
member x.MakeList() =
let list: ListOfDefinition<_> = { OfType = x }
upcast list
interface OutputDef with
member __.OutputType = typeof<'Val>
interface EnumDef<'Val> with
member x.Options = x.Options
interface EnumDef with
member x.Name = x.Name
member x.Description = x.Description
member x.Options =
x.Options
|> Seq.ofArray
|> Seq.cast<EnumVal>
|> Seq.toArray
interface NamedDef with
member x.Name = x.Name
override x.ToString() = sprintf "enum %s {\n %s\n}" x.Name (String.Join("\n ", x.Options))
/// 3.1.2 Objects
and ObjectDef =
interface
abstract Name : string
abstract Description : string option
abstract Fields : FieldDef []
abstract Implements : InterfaceDef []
abstract IsTypeOf : (obj -> bool) option
inherit TypeDef
inherit NamedDef
inherit OutputDef
inherit CompositeDef
inherit IEquatable<ObjectDef>
end
and ObjectDef<'Val> =
interface
abstract Fields : FieldDef<'Val> []
inherit ObjectDef
inherit TypeDef<'Val>
inherit OutputDef<'Val>
end
and [<CustomEquality; NoComparison>] ObjectDefinition<'Val> =
{ Name : string
Description : string option
FieldsFn : Lazy<FieldDef<'Val> []>
Implements : InterfaceDef []
IsTypeOf : (obj -> bool) option }
interface TypeDef with
member x.MakeNullable() =
let nullable: NullableDefinition<_> = { OfType = x }
upcast nullable
member x.MakeList() =
let list: ListOfDefinition<_> = { OfType = x }
upcast list
interface OutputDef with
member __.OutputType = typeof<'Val>
interface ObjectDef with
member x.Name = x.Name
member x.Description = x.Description
member x.Fields =
x.FieldsFn.Force()
|> Seq.ofArray
|> Seq.cast<FieldDef>
|> Seq.toArray
member x.Implements = x.Implements
member x.IsTypeOf = x.IsTypeOf
interface ObjectDef<'Val> with
member x.Fields = x.FieldsFn.Force()
interface NamedDef with
member x.Name = x.Name
interface IEquatable<ObjectDef> with
member x.Equals f = x.Name = f.Name
override x.Equals y =
match y with
| :? ObjectDef as f -> (x :> IEquatable<ObjectDef>).Equals(f)
| _ -> false
override x.GetHashCode() =
let mutable hash = x.Name.GetHashCode()
hash
override x.ToString() =
let sb = System.Text.StringBuilder("type ")
sb.Append(x.Name) |> ignore
if not (Array.isEmpty x.Implements) then
sb.Append(" implements ").Append(String.Join(", ", x.Implements |> Array.map (fun i -> i.Name))) |> ignore
sb.Append("{") |> ignore
//x.Fields |> List.iter (fun f -> sb.Append("\n ").Append(f.ToString()) |> ignore)
sb.Append("\n}").ToString()
and [<CustomEquality; NoComparison>] FieldDefinition<'Val, 'Res> =
{ Name : string
Description : string option
Type : OutputDef<'Res>
Resolve : ResolveFieldContext -> 'Val -> Job<'Res>
Args : InputFieldDef []
DeprecationReason : string option
mutable Execute : ExecuteField }
interface FieldDef with
member x.Name = x.Name
member x.Description = x.Description
member x.DeprecationReason = x.DeprecationReason
member x.Type = x.Type :> OutputDef
member x.Args = x.Args
member x.Resolve (ctx : ResolveFieldContext) (value : obj) : Job<obj> =
x.Resolve ctx (value :?> 'Val)
|> Job.map (fun x -> upcast x)
member x.Execute with get() = x.Execute and set v = x.Execute <- v
interface FieldDef<'Val> with
member x.Resolve (ctx : ResolveFieldContext) (value : 'Val) : Job<obj> =
x.Resolve ctx value
|> Job.map (fun x -> upcast x)
interface IEquatable<FieldDef> with
member x.Equals f = x.Name = f.Name && x.Type :> OutputDef = f.Type && x.Args = f.Args
override x.Equals y =
match y with
| :? FieldDef as f -> (x :> IEquatable<FieldDef>).Equals(f)
| _ -> false
override x.GetHashCode() =
let mutable hash = x.Name.GetHashCode()
hash <- (hash * 397) ^^^ (x.Type.GetHashCode())
hash <- (hash * 397) ^^^ (x.Args.GetHashCode())
hash
override x.ToString() =
let mutable s = x.Name + ": " + x.Type.ToString()
if not (Array.isEmpty x.Args) then s <- "(" + String.Join(", ", x.Args) + ")"
s
/// 3.1.3 Interfaces
and InterfaceDef =
interface
abstract Name : string
abstract Description : string option
abstract Fields : FieldDef []
abstract ResolveType : (obj -> ObjectDef) option
inherit TypeDef
inherit OutputDef
inherit CompositeDef
inherit AbstractDef
inherit NamedDef
inherit IEquatable<InterfaceDef>
end
and InterfaceDef<'Val> =
interface
abstract Fields : FieldDef<'Val> []
inherit TypeDef<'Val>
inherit OutputDef<'Val>
inherit InterfaceDef
end
and [<CustomEquality; NoComparison>] InterfaceDefinition<'Val> =
{ Name : string
Description : string option
FieldsFn : unit -> FieldDef<'Val> []
ResolveType : (obj -> ObjectDef) option }
interface TypeDef with
member x.MakeNullable() =
let nullable: NullableDefinition<_> = { OfType = x }
upcast nullable
member x.MakeList() =
let list: ListOfDefinition<_> = { OfType = x }
upcast list
interface OutputDef with
member __.OutputType = typeof<'Val>
interface InterfaceDef with
member x.Name = x.Name
member x.Description = x.Description
member x.Fields = x.FieldsFn() |> Array.map (fun fdef -> upcast fdef)
member x.ResolveType = x.ResolveType
interface InterfaceDef<'Val> with
member x.Fields = x.FieldsFn()
interface NamedDef with
member x.Name = x.Name
interface IEquatable<InterfaceDef> with
member x.Equals f = x.Name = f.Name
override x.Equals y =
match y with
| :? InterfaceDef as f -> (x :> IEquatable<InterfaceDef>).Equals(f)
| _ -> false
override x.GetHashCode() =
let mutable hash = x.Name.GetHashCode()
hash
override x.ToString() =
let sb = System.Text.StringBuilder("interface ").Append(x.Name).Append(" {")
// x.Fields |> List.iter (fun f -> sb.Append("\n ").Append(f.ToString()) |> ignore)
sb.Append("\n}").ToString()
and UnionDef =
interface
abstract Name : string
abstract Description : string option
abstract Options : ObjectDef []
abstract ResolveType : (obj -> ObjectDef) option
abstract ResolveValue : obj -> obj
inherit TypeDef
inherit OutputDef
inherit CompositeDef
inherit AbstractDef
inherit NamedDef
inherit IEquatable<UnionDef>
end
and UnionDef<'In> =
interface
abstract ResolveType : ('In -> ObjectDef) option
abstract ResolveValue : 'In -> obj
inherit UnionDef
inherit TypeDef<'In>
inherit OutputDef<'In>
end
/// 3.1.4 Unions
and [<CustomEquality; NoComparison>] UnionDefinition<'In, 'Out> =
{ Name : string
Description : string option
Options : ObjectDef []
ResolveType : ('In -> ObjectDef) option
ResolveValue : 'In -> 'Out }
interface TypeDef with
member x.MakeNullable() =
let nullable: NullableDefinition<_> = { OfType = x }
upcast nullable
member x.MakeList() =
let list: ListOfDefinition<_> = { OfType = x }
upcast list
interface OutputDef with
member __.OutputType = typeof<'Out>
interface UnionDef with
member x.Name = x.Name
member x.Description = x.Description
member x.Options = x.Options
member x.ResolveType = x.ResolveType |> Option.map (fun fn -> (fun value -> fn (value :?> 'In)))
member x.ResolveValue value = upcast x.ResolveValue(value :?> 'In)
interface UnionDef<'In> with
member x.ResolveType = x.ResolveType
member x.ResolveValue value = upcast x.ResolveValue value
interface NamedDef with
member x.Name = x.Name
interface IEquatable<UnionDef> with
member x.Equals f = x.Name = f.Name && x.Description = f.Description && x.Options = f.Options
override x.Equals y =
match y with
| :? InterfaceDef as f -> (x :> IEquatable<UnionDef>).Equals(f)
| _ -> false
override x.GetHashCode() =
let mutable hash = x.Name.GetHashCode()
hash <- (hash * 397) ^^^ (x.Options.GetHashCode())
hash
override x.ToString() = "union " + x.Name + " = " + String.Join(" | ", x.Options |> Array.map (fun o -> o.Name))
and ListOfDef =
interface
abstract OfType : TypeDef
inherit InputDef
inherit OutputDef
end
and ListOfDef<'Val> =
interface
abstract OfType : TypeDef<'Val>
inherit TypeDef<'Val seq>
inherit InputDef<'Val seq>
inherit OutputDef<'Val seq>
end
and ListOfDefinition<'Val> =
{ OfType : TypeDef<'Val> }
interface InputDef with
member __.InputType = typeof<'Val seq>
interface TypeDef with
member x.MakeNullable() =
let nullable: NullableDefinition<_> = { OfType = x }
upcast nullable
member x.MakeList() =
let list: ListOfDefinition<_> = { OfType = x }
upcast list
interface OutputDef with
member __.OutputType = typeof<'Val seq>
interface ListOfDef with
member x.OfType = upcast x.OfType
interface ListOfDef<'Val> with
member x.OfType = x.OfType
override x.ToString() =
match x.OfType with
| :? NamedDef as named -> "[" + named.Name + "]"
| other -> "[" + other.ToString() + "]"
and NullableDef =
interface
abstract OfType : TypeDef
inherit InputDef
inherit OutputDef
end
and NullableDef<'Val> =
interface
abstract OfType : TypeDef<'Val>
inherit InputDef<'Val option>
inherit OutputDef<'Val option>
end
and NullableDefinition<'Val> =
{ OfType : TypeDef<'Val> }
interface InputDef with
member __.InputType = typeof<'Val option>
interface TypeDef with
member x.MakeNullable() = upcast x
member x.MakeList() =
let list: ListOfDefinition<_> = { OfType = x }
upcast list
interface OutputDef with
member __.OutputType = typeof<'Val option>
interface NullableDef with
member x.OfType = upcast x.OfType
interface NullableDef<'Val> with
member x.OfType = x.OfType
override x.ToString() =
match x.OfType with
| :? NamedDef as named -> named.Name.Substring(0, named.Name.Length - 1) // remobe bang on sufix
| other -> other.ToString()
and InputObjectDef =
interface
abstract Name : string
abstract Description : string option
abstract Fields : InputFieldDef []
inherit NamedDef
inherit InputDef
end
/// 3.1.6 Input Objects
and InputObjectDefinition<'Val> =
{ Name : string
Description : string option
FieldsFn : unit -> InputFieldDef [] }
interface InputDef with
member __.InputType = typeof<'Val>
interface InputObjectDef with
member x.Name = x.Name
member x.Description = x.Description
member x.Fields = x.FieldsFn()
interface TypeDef<'Val>
interface InputDef<'Val>
interface NamedDef with
member x.Name = x.Name
interface TypeDef with
member x.MakeNullable() =
let nullable: NullableDefinition<_> = { OfType = x }
upcast nullable
member x.MakeList() =
let list: ListOfDefinition<_> = { OfType = x }
upcast list
and ExecuteInput = Map<string,obj> -> Value -> obj
and InputFieldDef =
interface
abstract Name : string
abstract Description : string option
abstract Type : InputDef
abstract DefaultValue : obj option
abstract ExecuteInput : ExecuteInput with get,set
inherit IEquatable<InputFieldDef>
end
/// 3.1.2.1 Object Field Arguments
and [<CustomEquality; NoComparison>] InputFieldDefinition<'In> =
{ Name : string
Description : string option
Type : InputDef<'In>
DefaultValue : 'In option
mutable ExecuteInput: ExecuteInput }
interface InputFieldDef with
member x.Name = x.Name
member x.Description = x.Description
member x.Type = upcast x.Type
member x.DefaultValue = x.DefaultValue |> Option.map (fun x -> upcast x)
member x.ExecuteInput with get () = x.ExecuteInput and set v = x.ExecuteInput <- v
interface IEquatable<InputFieldDef> with
member x.Equals f = x.Name = f.Name && x.Type :> InputDef = f.Type
override x.Equals y =
match y with
| :? InputFieldDef as f -> (x :> IEquatable<InputFieldDef>).Equals(f)
| _ -> false
override x.GetHashCode() =
let mutable hash = x.Name.GetHashCode()
hash <- (hash * 397) ^^^ (x.Type.GetHashCode())
hash
override x.ToString() = x.Name
/// 5.7 Variables
and Variable =
{ Name : string
Schema : TypeDef
DefaultValue : obj }
override x.ToString() =
"$" + x.Name + ": " + x.Schema.ToString() + (if x.DefaultValue <> null then " = " + x.DefaultValue.ToString()
else "")
and DirectiveDef =
{ Name : string
Description : string option
Locations : DirectiveLocation
Args : InputFieldDef [] }
[<AutoOpen>]
module SchemaDefinitions =
open System.Globalization
open System.Reflection
let internal coerceIntValue (x : obj) : int option =
match x with
| null -> None
| :? int as i -> Some i
| :? int64 as l -> Some(int l)
| :? double as d -> Some(int d)
| :? string as s ->
match Int32.TryParse(s) with
| true, i -> Some i
| false, _ -> None
| :? bool as b ->
Some(if b then 1
else 0)
| other ->
try
Some(System.Convert.ToInt32 other)
with _ -> None
let internal coerceFloatValue (x : obj) : double option =
match x with
| null -> None
| :? int as i -> Some(double i)
| :? int64 as l -> Some(double l)
| :? double as d -> Some d
| :? string as s ->
match Double.TryParse(s) with
| true, i -> Some i
| false, _ -> None
| :? bool as b ->
Some(if b then 1.
else 0.)
| other ->
try
Some(System.Convert.ToDouble other)
with _ -> None
let internal coerceBoolValue (x : obj) : bool option =
match x with
| null -> None
| :? int as i -> Some(i <> 0)
| :? int64 as l -> Some(l <> 0L)
| :? double as d -> Some(d <> 0.)
| :? string as s ->
match Boolean.TryParse(s) with
| true, i -> Some i
| false, _ -> None
| :? bool as b -> Some b
| other ->
try
Some(System.Convert.ToBoolean other)
with _ -> None
let internal coerceUriValue (x : obj) : Uri option =
match x with
| null -> None
| :? Uri as u -> Some u
| :? string as s ->
match Uri.TryCreate(s, UriKind.RelativeOrAbsolute) with
| true, uri -> Some uri
| false, _ -> None
| other -> None
let internal coerceDateValue (x : obj) : DateTime option =
match x with
| null -> None
| :? DateTime as d -> Some d
| :? string as s ->
match DateTime.TryParse(s) with
| true, date -> Some date
| false, _ -> None
| other -> None
let private coerceIntOuput (x : obj) =
match x with
| :? int as y -> Some(IntValue y)
| _ -> None
let private coerceFloatOuput (x : obj) =
match x with
| :? float as y -> Some(FloatValue y)
| _ -> None
let private coerceBoolOuput (x : obj) =
match x with
| :? bool as y -> Some(BooleanValue y)
| _ -> None
let private coerceStringOuput (x : obj) =
match x with
| :? string as y -> Some(StringValue y)
| _ -> None
let private coerceUriOutput (x : obj) =
match x with
| :? Uri as uri -> Some(StringValue (uri.ToString()))
| _ -> None
let private coerceDateOutput (x : obj) =
match x with
| :? DateTime as date -> Some(StringValue (date.ToString("O")))
| _ -> None
/// Check if provided obj value is an Option and extract its wrapped value as object if possible
let (|Option|_|) (x : obj) =
if x = null then None
else
let t = x.GetType()
if t.IsGenericType && t.GetGenericTypeDefinition() = typedefof<option<_>> then
let _, fields = Microsoft.FSharp.Reflection.FSharpValue.GetUnionFields(x, t)
Some(fields.[0])
else None
let internal coerceStringValue (x : obj) : string option =
match x with
| null -> None
| :? string as s -> Some s
| :? bool as b ->
Some(if b then "true"
else "false")
| Option o -> Some(o.ToString())
| _ -> Some(x.ToString())
let private coerceIntInput =
function
| IntValue i -> Some i
| FloatValue f -> Some(int f)
| StringValue s ->
match Int32.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture) with
| true, i -> Some i
| false, _ -> None
| BooleanValue b ->
Some(if b then 1
else 0)
| _ -> None
let private coerceFloatInput =
function
| IntValue i -> Some(double i)
| FloatValue f -> Some f
| StringValue s ->
match Double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture) with
| true, i -> Some i
| false, _ -> None
| BooleanValue b ->
Some(if b then 1.
else 0.)
| _ -> None