-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathProvidedTypesHelper.fs
More file actions
954 lines (918 loc) · 65.2 KB
/
ProvidedTypesHelper.fs
File metadata and controls
954 lines (918 loc) · 65.2 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
// The MIT License (MIT)
// Copyright (c) 2016 Bazinga Technologies Inc
namespace FSharp.Data.GraphQL
open System
open System.Collections
open System.Collections.Generic
open System.Net.Http
open System.Reflection
open System.Text.Json.Serialization
open FSharp.Core
open FSharp.Data.GraphQL
open FSharp.Data.GraphQL.Client
open FSharp.Data.GraphQL.Client.ReflectionPatterns
open FSharp.Data.GraphQL.Ast
open FSharp.Data.GraphQL.Ast.Extensions
open FSharp.Data.GraphQL.Types.Introspection
open FSharp.Data.GraphQL.Validation
open ProviderImplementation.ProvidedTypes
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Reflection
type internal FieldStringPath = string list
module internal QuotationHelpers =
let rec coerceValues fieldTypeLookup fields =
let arrayExpr (arrayType : Type) (v : obj) =
let typ = arrayType.GetElementType()
let instance =
match v with
| :? IEnumerable as x -> Seq.cast<obj> x |> Array.ofSeq
| _ -> failwith "Unexpected array value."
let exprs = coerceValues (fun _ -> typ) instance
Expr.NewArray(typ, exprs)
let tupleExpr (tupleType : Type) (v : obj) =
let typ = FSharpType.GetTupleElements tupleType |> Array.mapi (fun i t -> i, t) |> Map.ofArray
let fieldTypeLookup i = typ.[i]
let fields = FSharpValue.GetTupleFields v
let exprs = coerceValues fieldTypeLookup fields
Expr.NewTuple(exprs)
Array.mapi (fun i v ->
let expr =
if isNull v then simpleTypeExpr v
else
let tpy = v.GetType()
if tpy.IsArray then arrayExpr tpy v
elif FSharpType.IsTuple tpy then tupleExpr tpy v
elif FSharpType.IsUnion tpy then unionExpr v |> snd
elif FSharpType.IsRecord tpy then recordExpr v |> snd
else simpleTypeExpr v
Expr.Coerce(expr, fieldTypeLookup i)
) fields |> List.ofArray
and simpleTypeExpr instance = Expr.Value(instance)
and unionExpr instance =
let caseInfo, fields = FSharpValue.GetUnionFields(instance, instance.GetType())
let fieldInfo = caseInfo.GetFields()
let fieldTypeLookup indx = fieldInfo.[indx].PropertyType
caseInfo.DeclaringType, Expr.NewUnionCase(caseInfo, coerceValues fieldTypeLookup fields)
and recordExpr instance =
let typ = instance.GetType()
let fields = FSharpValue.GetRecordFields(instance)
let fieldInfo = FSharpType.GetRecordFields(typ)
let fieldTypeLookup indx = fieldInfo.[indx].PropertyType
typ, Expr.NewRecord(instance.GetType(), coerceValues fieldTypeLookup fields)
and arrayExpr (instance : 'a array) =
let typ = typeof<'a>
let arrayType = instance.GetType()
let exprs = coerceValues (fun _ -> typ) (instance |> Array.map box)
arrayType, Expr.NewArray(typ, exprs)
let createLetExpr varType instance body args =
let var = Var("instance", varType)
Expr.Let(var, instance, body args (Expr.Var(var)))
let quoteUnion instance =
let func instance = unionExpr instance ||> createLetExpr
Tracer.runAndMeasureExecutionTime "Quoted union type" (fun _ -> func instance)
let quoteRecord instance =
let func instance = recordExpr instance ||> createLetExpr
Tracer.runAndMeasureExecutionTime "Quoted record type" (fun _ -> func instance)
let quoteArray instance =
let func instance = arrayExpr instance ||> createLetExpr
Tracer.runAndMeasureExecutionTime "Quoted array type" (fun _ -> func instance)
module internal ProvidedEnum =
let ctor = typeof<EnumBase>.GetConstructors().[0]
let makeProvidedType(name, items : string seq) =
let tdef = ProvidedTypeDefinition(name, Some typeof<EnumBase>, nonNullable = true, isSealed = true)
tdef.AddMembersDelayed(fun _ ->
items
|> Seq.map (fun item ->
let getterCode (_ : Expr list) =
Expr.NewObject(ctor, [ <@@ name @@>; <@@ item @@> ])
ProvidedProperty(item, tdef, getterCode, isStatic = true))
|> Seq.cast<MemberInfo>
|> List.ofSeq)
tdef
type internal ProvidedTypeMetadata =
{ Name : string
Description : string option }
module internal ProvidedInterface =
let makeProvidedType(metadata : ProvidedTypeMetadata) =
let tdef = ProvidedTypeDefinition("I" + metadata.Name.FirstCharUpper(), None, nonNullable = true, isInterface = true)
metadata.Description |> Option.iter tdef.AddXmlDoc
tdef
type internal RecordPropertyMetadata =
{ Name : string
Alias : string voption
Description : string option
DeprecationReason : string option
Type : Type }
member x.AliasOrName =
match x.Alias with
| ValueSome x -> x
| ValueNone -> x.Name
type internal ProvidedRecordTypeDefinition(className, baseType) =
inherit ProvidedTypeDefinition(className, baseType, nonNullable = true)
let mutable properties : RecordPropertyMetadata list = []
member _.GetRecordProperties() = properties
member _.SetRecordProperties(props) = properties <- props
[<AutoOpen>]
module internal Failures =
let uploadTypeIsNotScalar uploadTypeName =
failwith $"""Upload type "%s{uploadTypeName}" was found on the schema, but it is not a Scalar type. Upload types can only be used if they are defined as scalar types."""
module internal ProvidedRecord =
let ctor = typeof<RecordBase>.GetConstructors().[0]
let makeProvidedType(tdef : ProvidedRecordTypeDefinition, properties : RecordPropertyMetadata list, explicitOptionalParameters: bool) =
let name = tdef.Name
tdef.AddMembersDelayed(fun _ ->
properties |> List.map (fun metadata ->
let pname = metadata.AliasOrName.FirstCharUpper()
let getterCode (args : Expr list) =
<@@ let this = %%args.[0] : RecordBase
match this.GetProperties() |> List.tryFind (fun prop -> prop.Name = pname) with
| Some prop -> prop.Value
| None -> failwith $"""Expected to find property "%s{pname}", but the property was not found.""" @@>
let pdef = ProvidedProperty(pname, metadata.Type, getterCode)
metadata.Description |> Option.iter pdef.AddXmlDoc
metadata.DeprecationReason |> Option.iter pdef.AddObsoleteAttribute
pdef))
let addConstructorDelayed (propertiesGetter : unit -> (string * string voption * Type) list) =
tdef.AddMembersDelayed(fun _ ->
// We need to build a constructor that takes all optional properties wrapped in another option.
// We need to do this because optional parameters have issues with non-nullable types
// in the type provider SDK. They require a default value, and if the type is not nullable
// it provides the type default value for it. So basically, what's needed is three-valued behavior
// so we can differentiate between no value and null/None.
//
// I.e. to not send a value the optional parameter can either be set implicitly (by not providing an
// argument) or explicitly (`?parameterName = Some None` or `parameterName = None`).
// To set a value it has to be wrapped in an option: `parameterName = Some argumentValue`
// (or `?parameterName = Some (Some argumentValue)`).
//
// To keep backwards compatibility this constructor is only created if a flag is turned on. Otherwise
// we keep the previous behavior: We build a constructor overload for each optional property.
// Since RecordBase needs to know each property information in its own constructor,
// we also need to know each property that was not filled in the currently used overload. So we make
// combinations of all possible overloads, and for each one we map the user's provided values and
// fill the others with a null value. This way we can construct the RecordBase type providing all
// needed properties.
let properties = propertiesGetter()
let optionalProperties, requiredProperties =
properties
|> List.map (fun (name, alias, t) -> ValueOption.defaultValue name alias, t)
|> List.partition (fun (_, t) -> isOption t)
if explicitOptionalParameters then
let constructorProperties = requiredProperties @ optionalProperties
let propertyNames = constructorProperties |> List.map (fst >> _.FirstCharUpper())
let constructorPropertyTypes = constructorProperties |> List.map snd
let invoker (args : Expr list) =
let properties =
let baseConstructorArgs =
let coercedArgs =
(constructorPropertyTypes, args)
||> List.map2 (fun t arg ->
let arg = Expr.Coerce(arg, typeof<obj>)
match t with
| Option (Option t) -> <@@ makeValue t %%arg @@>
| _ -> <@@ %%arg @@>)
(propertyNames, coercedArgs)
||> List.map2 (fun name value -> <@@ { RecordProperty.Name = name; Value = %%value } @@>)
Expr.NewArray(typeof<RecordProperty>, baseConstructorArgs)
Expr.NewObject(ctor, [Expr.Value(name); properties])
let constructorParams =
constructorProperties
|> List.map (fun (name, t) -> ProvidedParameter(name, t, ?optionalValue = if isOption t then Some null else None))
[ProvidedConstructor(constructorParams, invoker)]
else
List.combinations optionalProperties
|> List.map (fun (optionalProperties, nullValuedProperties) ->
let constructorProperties = requiredProperties @ optionalProperties
let propertyNames = (constructorProperties @ nullValuedProperties) |> List.map (fst >> _.FirstCharUpper())
let constructorPropertyTypes = constructorProperties |> List.map snd
let nullValuedPropertyTypes = nullValuedProperties |> List.map snd
let invoker (args : Expr list) =
let properties =
let baseConstructorArgs =
let coercedArgs =
(constructorPropertyTypes, args)
||> List.map2 (fun t arg ->
let arg = Expr.Coerce(arg, typeof<obj>)
if isOption t then <@@ makeSome %%arg @@> else <@@ %%arg @@>)
let nullValuedArgs = nullValuedPropertyTypes |> List.map (fun _ -> <@@ null @@>)
(propertyNames, (coercedArgs @ nullValuedArgs))
||> List.map2 (fun name value -> <@@ { RecordProperty.Name = name; Value = %%value } @@>)
Expr.NewArray(typeof<RecordProperty>, baseConstructorArgs)
Expr.NewObject(ctor, [Expr.Value(name); properties])
let constructorParams =
constructorProperties
|> List.map (fun (name, t) ->
match t with
| Option t -> ProvidedParameter(name, t)
| _ -> ProvidedParameter(name, t))
ProvidedConstructor(constructorParams, invoker)))
match tdef.BaseType with
| :? ProvidedRecordTypeDefinition as bdef ->
bdef.AddMembersDelayed(fun _ ->
let asType =
let invoker (args : Expr list) =
<@@ let this = %%args.[0] : RecordBase
if this.GetName() = name then this
else failwithf "Expected type to be \"%s\", but it is \"%s\". Make sure to check the type by calling \"Is%s\" method before calling \"As%s\" method." name (this.GetName()) name name @@>
ProvidedMethod("As" + name, [], tdef, invoker)
let tryAsType =
let invoker (args : Expr list) =
<@@ let this = %%args.[0] : RecordBase
if this.GetName() = name then Some this
else None @@>
ProvidedMethod("TryAs" + name, [], typedefof<_ option>.MakeGenericType(tdef), invoker)
let isType =
let invoker (args : Expr list) =
<@@ let this = %%args.[0] : RecordBase
this.GetName() = name @@>
ProvidedMethod("Is" + name, [], typeof<bool>, invoker)
let members : MemberInfo list = [asType; tryAsType; isType]
members)
let propertiesGetter() = bdef.GetRecordProperties() @ properties |> List.map (fun p -> p.Name, p.Alias, p.Type)
addConstructorDelayed propertiesGetter
| _ ->
let propertiesGetter() = properties |> List.map (fun p -> p.Name, p.Alias, p.Type)
addConstructorDelayed propertiesGetter
tdef.SetRecordProperties(properties)
tdef
let preBuildProvidedType(metadata : ProvidedTypeMetadata, baseType : Type option) =
let baseType = Option.defaultValue typeof<RecordBase> baseType
let name = metadata.Name.FirstCharUpper()
let tdef = ProvidedRecordTypeDefinition(name, Some baseType)
tdef
#nowarn "10001"
module internal ProvidedOperationResult =
let makeProvidedType(operationType : Type) =
let tdef = ProvidedTypeDefinition("OperationResult", Some typeof<OperationResultBase>, nonNullable = true)
tdef.AddMemberDelayed(fun _ ->
let getterCode (args : Expr list) =
<@@ let this = %%args.[0] : OperationResultBase
this.RawData @@>
let prop = ProvidedProperty("Data", operationType, getterCode)
prop.AddXmlDoc("Contains the data returned by the operation on the server.")
prop)
tdef
module internal ProvidedOperation =
let makeProvidedType(actualQuery : string,
operationDefinition : OperationDefinition,
operationTypeName : string,
operationFieldsExpr : Expr,
schemaTypes: Map<string,IntrospectionType>,
schemaProvidedTypes : Map<string, ProvidedTypeDefinition>,
operationType : Type,
contextInfo : GraphQLRuntimeContextInfo option,
uploadInputTypeName : string option,
className : string,
explicitOptionalParameters: bool) =
let tdef = ProvidedTypeDefinition(className, Some typeof<OperationBase>)
tdef.AddXmlDoc("Represents a GraphQL operation on the server.")
tdef.AddMembersDelayed(fun _ ->
let operationResultDef = ProvidedOperationResult.makeProvidedType(operationType)
let isScalar (typeName: string) =
match schemaTypes.TryFind typeName with
| Some introspectionType -> introspectionType.Kind = TypeKind.SCALAR
| None -> false
let variables =
let rec mapVariable (variableName : string) (variableType : InputType) =
match variableType with
| NamedType typeName ->
match uploadInputTypeName with
| Some uploadInputTypeName when typeName = uploadInputTypeName ->
variableName, TypeMapping.makeOption typeof<Upload>
| _ ->
match TypeMapping.scalar.TryFind(typeName) with
| Some t -> variableName, TypeMapping.makeOption t
| None when isScalar typeName -> variableName, typeof<string option>
| None ->
match schemaProvidedTypes.TryFind(typeName) with
| Some t -> variableName, TypeMapping.makeOption t
| None -> failwith $"""Unable to find variable type "%s{typeName}" in the schema definition."""
| ListType itype ->
let name, t = mapVariable variableName itype
name, t |> TypeMapping.makeArray |> TypeMapping.makeOption
| NonNullType itype ->
let name, t = mapVariable variableName itype
name, TypeMapping.unwrapOption t
operationDefinition.VariableDefinitions |> List.map (fun vdef -> mapVariable vdef.VariableName vdef.Type)
let buildVariablesExprFromArgs (varNames : string list) (args : Expr list) =
let mapVariableExpr (name : string) (value : Expr) =
let value = Expr.Coerce(value, typeof<obj>)
<@@ let rec mapVariableValue (value : obj) =
match value with
| null -> null
| :? string -> value // We need this because strings are enumerables, and we don't want to enumerate them recursively as an object
| :? EnumBase as v -> v.GetValue() |> box
| :? RecordBase as v -> v.ToDictionary() |> box
| OptionValue v -> v |> Option.map mapVariableValue |> box
| EnumerableValue v -> v |> Array.map mapVariableValue |> box
| v -> v
(name, mapVariableValue %%value) @@>
let args =
let varArgs = List.skip (args.Length - variables.Length) args
(varNames, varArgs) ||> List.map2 mapVariableExpr
Expr.NewArray(typeof<string * obj>, args)
let defaultContextExpr =
match contextInfo with
| Some info ->
let serverUrl = info.ServerUrl
let headerNames = info.HttpHeaders |> Seq.map fst |> Array.ofSeq
let headerValues = info.HttpHeaders |> Seq.map snd |> Array.ofSeq
<@@ { ServerUrl = serverUrl; HttpHeaders = Array.zip headerNames headerValues; Connection = new GraphQLClientConnection() } @@>
| None -> <@@ Unchecked.defaultof<GraphQLProviderRuntimeContext> @@>
// We need to use the combination strategy to generate overloads for variables in the Run/AsyncRun methods.
// The strategy follows the same principle with ProvidedRecord constructor overloads,
// except that we also must create one additional overload for the runtime context, for each already existent overload,
// if no default context is provided.
let methodOverloadDefinitions =
let overloadsWithoutContext =
let optionalVariables, requiredVariables =
variables |> List.partition (fun (_, t) -> isOption t)
if explicitOptionalParameters then
[requiredVariables @ optionalVariables]
else
List.combinations optionalVariables
|> List.map (fun (optionalVariables, _) ->
let optionalVariables = optionalVariables |> List.map (fun (name, t) -> name, (TypeMapping.unwrapOption t))
requiredVariables @ optionalVariables)
let overloadsWithContext =
overloadsWithoutContext
|> List.map (fun var -> ("runtimeContext", typeof<GraphQLProviderRuntimeContext>) :: var)
match contextInfo with
| Some _ -> overloadsWithoutContext @ overloadsWithContext
| None -> overloadsWithContext
// Multipart requests should only be used when the user specifies a upload type name AND the type
// is present in the query as an input value. If not, we fallback to classic requests.
let shouldUseMultipartRequest =
let rec existsUploadType (foundTypes : ProvidedTypeDefinition list) (t : Type) =
match t with
| :? ProvidedTypeDefinition as tdef when not (List.contains tdef foundTypes) -> tdef.DeclaredProperties |> Seq.exists ((fun p -> p.PropertyType) >> existsUploadType (tdef :: foundTypes))
| Option t -> existsUploadType foundTypes t
| Array t -> existsUploadType foundTypes t
| _ -> t = typeof<Upload>
variables |> Seq.exists (snd >> existsUploadType [])
let runMethodOverloads : MemberInfo list =
let operationName = ValueOption.toObj operationDefinition.Name
methodOverloadDefinitions |> List.map (fun overloadParameters ->
let variableNames = overloadParameters |> List.map fst |> List.filter (fun name -> name <> "runtimeContext")
let invoker (args : Expr list) =
// First arg is the operation instance, second should be the context, if the overload asks for one.
// We determine it by seeing if the variable names have one less item than the arguments without the instance.
let argsWithoutInstance = args.Tail
let variableArgs, isDefaultContext, context =
if argsWithoutInstance.Length - variableNames.Length = 1
then argsWithoutInstance.Tail, false, argsWithoutInstance.Head
else argsWithoutInstance, true, defaultContextExpr
let variables = buildVariablesExprFromArgs variableNames variableArgs
let variables =
if explicitOptionalParameters then
<@@ (%%variables: (string * obj) [])
|> Array.filter (fun (_, value) ->
match value with
| :? Option<obj> as option -> option.IsSome
| _ -> true) @@>
else
variables
<@@ let context = %%context : GraphQLProviderRuntimeContext
let request =
{ ServerUrl = context.ServerUrl
HttpHeaders = context.HttpHeaders
OperationName = Option.ofObj operationName
Query = actualQuery
Variables = %%variables }
let response =
if shouldUseMultipartRequest
then Tracer.runAndMeasureExecutionTime "Ran a multipart GraphQL query request" (fun _ -> GraphQLClient.sendMultipartRequest context.Connection request)
else Tracer.runAndMeasureExecutionTime "Ran a GraphQL query request" (fun _ -> GraphQLClient.sendRequest context.Connection request)
let responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult()
let responseJson = Tracer.runAndMeasureExecutionTime "Parsed a GraphQL response to a JsonValue" (fun _ -> JsonValue.Parse responseString)
// If the user does not provide a context, we should dispose the default one after running the query
if isDefaultContext then (context :> IDisposable).Dispose()
OperationResultBase(response, responseJson, %%operationFieldsExpr, operationTypeName) @@>
let methodParameters = overloadParameters |> List.map (fun (name, t) -> ProvidedParameter(name, t, ?optionalValue = if isOption t then Some null else None))
let methodDef = ProvidedMethod("Run", methodParameters, operationResultDef, invoker)
methodDef.AddXmlDoc("Executes the operation on the server and fetch its results.")
upcast methodDef)
let asyncRunMethodOverloads : MemberInfo list =
let operationName = ValueOption.toObj operationDefinition.Name
methodOverloadDefinitions |> List.map (fun overloadParameters ->
let variableNames = overloadParameters |> List.map fst |> List.filter (fun name -> name <> "runtimeContext")
let invoker (args : Expr list) =
// First arg is the operation instance, second should be the context, if the overload asks for one.
// We determine it by seeing if the variable names have one less item than the arguments without the instance.
let argsWithoutInstance = args.Tail
let variableArgs, isDefaultContext, context =
if argsWithoutInstance.Length - variableNames.Length = 1
then argsWithoutInstance.Tail, false, argsWithoutInstance.Head
else argsWithoutInstance, true, defaultContextExpr
let variables = buildVariablesExprFromArgs variableNames variableArgs
let variables =
if explicitOptionalParameters then
<@@ (%%variables: (string * obj) [])
|> Array.filter (fun (_, value) ->
match value with
| :? Option<obj> as option -> option.IsSome
| _ -> true) @@>
else
variables
<@@ let context = %%context : GraphQLProviderRuntimeContext
let request =
{ ServerUrl = context.ServerUrl
HttpHeaders = context.HttpHeaders
OperationName = Option.ofObj operationName
Query = actualQuery
Variables = %%variables }
async {
let! ct = Async.CancellationToken
let! response =
if shouldUseMultipartRequest
then Tracer.asyncRunAndMeasureExecutionTime "Ran a multipart GraphQL query request asynchronously" (fun _ -> GraphQLClient.sendMultipartRequestAsync ct context.Connection request |> Async.AwaitTask)
else Tracer.asyncRunAndMeasureExecutionTime "Ran a GraphQL query request asynchronously" (fun _ -> GraphQLClient.sendRequestAsync ct context.Connection request |> Async.AwaitTask)
let! responseString = response.Content.ReadAsStringAsync() |> Async.AwaitTask
let responseJson = Tracer.runAndMeasureExecutionTime "Parsed a GraphQL response to a JsonValue" (fun _ -> JsonValue.Parse responseString)
// If the user does not provide a context, we should dispose the default one after running the query
if isDefaultContext then (context :> IDisposable).Dispose()
return OperationResultBase(response, responseJson, %%operationFieldsExpr, operationTypeName)
} @@>
let methodParameters = overloadParameters |> List.map (fun (name, t) -> ProvidedParameter(name, t, ?optionalValue = if isOption t then Some null else None))
let methodDef = ProvidedMethod("AsyncRun", methodParameters, TypeMapping.makeAsync operationResultDef, invoker)
methodDef.AddXmlDoc("Executes the operation asynchronously on the server and fetch its results.")
upcast methodDef)
let parseResultDef =
let invoker (args : Expr list) = <@@ OperationResultBase(%%args.[1], JsonValue.Parse %%args.[2], %%operationFieldsExpr, operationTypeName) @@>
let parameters = [
ProvidedParameter("rawResponse", typeof<HttpResponseMessage>)
ProvidedParameter("responseJson", typeof<string>)
]
let methodDef = ProvidedMethod("ParseResult", parameters, operationResultDef, invoker)
methodDef.AddXmlDoc("Parses a JSON response that matches the response pattern of the current operation into a OperationResult type.")
methodDef
let members : MemberInfo list = [operationResultDef; parseResultDef] @ runMethodOverloads @ asyncRunMethodOverloads
members)
tdef
type internal ProvidedOperationMetadata =
{ OperationType : Type
UploadInputTypeName : string option
TypeWrapper : ProvidedTypeDefinition }
module internal Provider =
let getOperationMetadata (schemaTypes : Map<TypeName, IntrospectionType>, uploadInputTypeName : string option, enumProvidedTypes : Map<TypeName, ProvidedTypeDefinition>, operationAstFields, operationTypeRef, explicitOptionalParameters: bool) =
let generateWrapper name =
let rec resolveWrapperName actual =
if schemaTypes.ContainsKey(actual)
then resolveWrapperName (actual + "Fields")
else actual
ProvidedTypeDefinition(resolveWrapperName name, None, isSealed = true)
let wrappersByPath = Dictionary<FieldStringPath, ProvidedTypeDefinition>()
let rootWrapper = generateWrapper "Types"
wrappersByPath.Add([], rootWrapper)
let rec getWrapper (path : FieldStringPath) =
match wrappersByPath.TryGetValue path with
| true, wrappedPath -> wrappedPath
| false, _ ->
let wrapper = generateWrapper (path.Head.FirstCharUpper() + "Fields")
let upperWrapper =
let path = path.Tail
match wrappersByPath.TryGetValue path with
| true, wpath -> wpath
| false, _ -> getWrapper path
upperWrapper.AddMember(wrapper)
wrappersByPath.Add(path, wrapper)
wrapper
let includeType (path : FieldStringPath) (t : ProvidedTypeDefinition) =
let wrapper = getWrapper path
wrapper.AddMember(t)
let providedTypes = Dictionary<FieldStringPath * TypeName, ProvidedTypeDefinition>()
let rec getProvidedType (providedTypes : Dictionary<FieldStringPath * TypeName, ProvidedTypeDefinition>) (schemaTypes : Map<TypeName, IntrospectionType>) (path : FieldStringPath) (astFields : AstFieldInfo list) (tref : IntrospectionTypeRef) : Type =
match tref.Kind with
| TypeKind.SCALAR when tref.Name.IsSome -> TypeMapping.mapScalarType uploadInputTypeName tref.Name.Value |> TypeMapping.makeOption
| _ when uploadInputTypeName.IsSome && tref.Name.IsSome && uploadInputTypeName.Value = tref.Name.Value -> uploadTypeIsNotScalar uploadInputTypeName.Value
| TypeKind.NON_NULL when tref.Name.IsNone && tref.OfType.IsSome -> getProvidedType providedTypes schemaTypes path astFields tref.OfType.Value |> TypeMapping.unwrapOption
| TypeKind.LIST when tref.Name.IsNone && tref.OfType.IsSome -> getProvidedType providedTypes schemaTypes path astFields tref.OfType.Value |> TypeMapping.makeArray |> TypeMapping.makeOption
| TypeKind.ENUM when tref.Name.IsSome ->
match enumProvidedTypes.TryFind(tref.Name.Value) with
| Some providedEnum -> TypeMapping.makeOption providedEnum
| None -> failwith $"""Could not find a enum type based on a type reference. The reference is an "%s{tref.Name.Value}" enum, but that enum was not found in the introspection schema."""
| (TypeKind.OBJECT | TypeKind.INTERFACE | TypeKind.UNION) when tref.Name.IsSome ->
if providedTypes.ContainsKey(path, tref.Name.Value)
then TypeMapping.makeOption providedTypes.[path, tref.Name.Value]
else
let getIntrospectionFields typeName =
match schemaTypes.TryGetValue typeName with
| true, schematype -> schematype.Fields |> Option.defaultValue [||]
| false, _ -> failwith $"""Could not find a schema type based on a type reference. The reference is to a "%s{typeName}" type, but that type was not found in the schema types."""
let getPropertyMetadata typeName (info : AstFieldInfo) : RecordPropertyMetadata =
let ifield =
match getIntrospectionFields typeName |> Array.tryFind(fun f -> f.Name = info.Name) with
| Some ifield -> ifield
| None -> failwith $"""Could not find field "%s{info.Name}" of type "%s{tref.Name.Value}". The schema type does not have a field with the specified name."""
let path = info.AliasOrName :: path
let astFields = info.Fields
let ftype = getProvidedType providedTypes schemaTypes path astFields ifield.Type
{ Name = info.Name; Alias = info.Alias; Description = ifield.Description; DeprecationReason = ifield.DeprecationReason; Type = ftype }
let fragmentProperties =
astFields
|> List.choose (function FragmentField f when f.TypeCondition <> tref.Name.Value -> Some f | _ -> None)
|> List.groupBy (fun field -> field.TypeCondition)
|> List.map (fun (typeCondition, fields) ->
let conditionFields = fields |> Seq.distinctBy _.AliasOrName |> Seq.map FragmentField |> Seq.toList
typeCondition, List.map (getPropertyMetadata typeCondition) conditionFields)
let baseProperties =
astFields
|> List.choose (fun x ->
match x with
| TypeField _ -> Some x
| FragmentField f when f.TypeCondition = tref.Name.Value -> Some x
| _ -> None)
|> List.distinctBy _.AliasOrName
|> List.map (getPropertyMetadata tref.Name.Value)
let baseType =
let metadata : ProvidedTypeMetadata = { Name = tref.Name.Value; Description = tref.Description }
let tdef = ProvidedRecord.preBuildProvidedType(metadata, None)
providedTypes.Add((path, tref.Name.Value), tdef)
includeType path tdef
ProvidedRecord.makeProvidedType(tdef, baseProperties, explicitOptionalParameters)
let createFragmentType (typeName, properties) =
let itype =
match schemaTypes.TryGetValue typeName with
| true, schematype -> schematype
| false, _ -> failwithf "Could not find schema type based on the query. Type \"%s\" does not exist on the schema definition." typeName
let metadata : ProvidedTypeMetadata = { Name = itype.Name; Description = itype.Description }
let tdef = ProvidedRecord.preBuildProvidedType(metadata, Some (upcast baseType))
providedTypes.Add((path, typeName), tdef)
includeType path tdef
ProvidedRecord.makeProvidedType(tdef, properties, explicitOptionalParameters) |> ignore
fragmentProperties |> List.iter createFragmentType
TypeMapping.makeOption baseType
| _ -> failwith "Could not find a schema type based on a type reference. The reference has an invalid or unsupported combination of Name, Kind and OfType fields."
let operationType = getProvidedType providedTypes schemaTypes [] operationAstFields operationTypeRef
{ OperationType = operationType
UploadInputTypeName = uploadInputTypeName
TypeWrapper = rootWrapper }
let getSchemaProvidedTypes(schema : IntrospectionSchema, uploadInputTypeName : string option, explicitOptionalParameters: bool) =
let providedTypes = ref Map.empty<TypeName, ProvidedTypeDefinition>
let schemaTypes = TypeMapping.getSchemaTypes schema
let getSchemaType (tref : IntrospectionTypeRef) =
match tref.Name with
| Some name ->
match schemaTypes.TryFind(name) with
| Some itype -> itype
| None -> failwithf "Type \"%s\" was not found on the schema custom types." name
| None -> failwith "Expected schema type to have a name, but it does not have one."
let typeModifier (modifier : Type -> Type) (metadata : RecordPropertyMetadata) = { metadata with Type = modifier metadata.Type }
let makeOption = typeModifier TypeMapping.makeOption
let makeArrayOption = typeModifier (TypeMapping.makeArray >> TypeMapping.makeOption)
let unwrapOption = typeModifier TypeMapping.unwrapOption
let ofFieldType (field : IntrospectionField) = { field with Type = field.Type.OfType.Value }
let ofInputFieldType (field : IntrospectionInputVal) = { field with Type = field.Type.OfType.Value }
let rec resolveFieldMetadata (field : IntrospectionField) : RecordPropertyMetadata =
match field.Type.Kind with
| TypeKind.SCALAR when field.Type.Name.IsSome ->
let providedType = TypeMapping.mapScalarType uploadInputTypeName field.Type.Name.Value
{ Name = field.Name
Alias = ValueNone
Description = field.Description
DeprecationReason = field.DeprecationReason
Type = providedType }
|> makeOption
| _ when uploadInputTypeName.IsSome && field.Type.Name.IsSome && uploadInputTypeName.Value = field.Type.Name.Value -> uploadTypeIsNotScalar uploadInputTypeName.Value
| TypeKind.NON_NULL when field.Type.Name.IsNone && field.Type.OfType.IsSome -> ofFieldType field |> resolveFieldMetadata |> unwrapOption
| TypeKind.LIST when field.Type.Name.IsNone && field.Type.OfType.IsSome -> ofFieldType field |> resolveFieldMetadata |> makeArrayOption
| (TypeKind.OBJECT | TypeKind.INTERFACE | TypeKind.INPUT_OBJECT | TypeKind.UNION | TypeKind.ENUM) when field.Type.Name.IsSome ->
let itype = getSchemaType field.Type
let providedType = resolveProvidedType itype
{ Name = field.Name
Alias = ValueNone
Description = field.Description
DeprecationReason = field.DeprecationReason
Type = providedType }
|> makeOption
| _ -> failwith "Could not find a schema type based on a type reference. The reference has an invalid or unsupported combination of Name, Kind and OfType fields."
and resolveInputFieldMetadata (field : IntrospectionInputVal) : RecordPropertyMetadata =
match field.Type.Kind with
| TypeKind.SCALAR when field.Type.Name.IsSome ->
let providedType = TypeMapping.mapScalarType uploadInputTypeName field.Type.Name.Value
{ Name = field.Name
Alias = ValueNone
Description = field.Description
DeprecationReason = None
Type = providedType }
|> makeOption
| _ when uploadInputTypeName.IsSome && field.Type.Name.IsSome && uploadInputTypeName.Value = field.Type.Name.Value -> uploadTypeIsNotScalar uploadInputTypeName.Value
| TypeKind.NON_NULL when field.Type.Name.IsNone && field.Type.OfType.IsSome -> ofInputFieldType field |> resolveInputFieldMetadata |> unwrapOption
| TypeKind.LIST when field.Type.Name.IsNone && field.Type.OfType.IsSome -> ofInputFieldType field |> resolveInputFieldMetadata |> makeArrayOption
| (TypeKind.OBJECT | TypeKind.INTERFACE | TypeKind.INPUT_OBJECT | TypeKind.UNION | TypeKind.ENUM) when field.Type.Name.IsSome ->
let itype = getSchemaType field.Type
let providedType = resolveProvidedType itype
{ Name = field.Name
Alias = ValueNone
Description = field.Description
DeprecationReason = None
Type = providedType }
|> makeOption
| _ -> failwith "Could not find a schema type based on a type reference. The reference has an invalid or unsupported combination of Name, Kind and OfType fields."
and resolveProvidedType (itype : IntrospectionType) : ProvidedTypeDefinition =
match providedTypes.Value.TryGetValue itype.Name with
| true, ptval -> ptval
| false, _ ->
let metadata = { Name = itype.Name; Description = itype.Description }
match itype.Kind with
| TypeKind.OBJECT ->
let tdef = ProvidedRecord.preBuildProvidedType(metadata, None)
providedTypes.Value <- providedTypes.Value.Add(itype.Name, tdef)
let properties =
itype.Fields
|> Option.defaultValue [||]
|> Array.map resolveFieldMetadata
|> List.ofArray
upcast ProvidedRecord.makeProvidedType(tdef, properties, explicitOptionalParameters)
| TypeKind.INPUT_OBJECT ->
let tdef = ProvidedRecord.preBuildProvidedType(metadata, None)
providedTypes.Value <- providedTypes.Value.Add(itype.Name, tdef)
let properties =
itype.InputFields
|> Option.defaultValue [||]
|> Array.map resolveInputFieldMetadata
|> List.ofArray
upcast ProvidedRecord.makeProvidedType(tdef, properties, explicitOptionalParameters)
| TypeKind.INTERFACE | TypeKind.UNION ->
let bdef = ProvidedInterface.makeProvidedType(metadata)
providedTypes.Value <- providedTypes.Value.Add(itype.Name, bdef)
bdef
| TypeKind.ENUM ->
let items =
match itype.EnumValues with
| Some values -> values |> Array.map (fun value -> value.Name)
| None -> [||]
let tdef = ProvidedEnum.makeProvidedType(itype.Name, items)
providedTypes.Value <- providedTypes.Value.Add(itype.Name, tdef)
tdef
| _ -> failwithf "Type \"%s\" is not a Record, Union, Enum, Input Object, or Interface type." itype.Name
let ignoredKinds = [TypeKind.SCALAR; TypeKind.LIST; TypeKind.NON_NULL]
schemaTypes |> Map.iter (fun _ itype -> if not (List.contains itype.Kind ignoredKinds) then resolveProvidedType itype |> ignore)
let possibleTypes (itype : IntrospectionType) =
match itype.PossibleTypes with
| Some trefs -> trefs |> Array.map (getSchemaType >> resolveProvidedType)
| None -> [||]
let getProvidedType typeName =
match providedTypes.Value.TryFind(typeName) with
| Some ptype -> ptype
| None -> failwithf "Expected to find a type \"%s\" on the schema type map, but it was not found." typeName
schemaTypes
|> Seq.iter (fun kvp ->
if kvp.Value.Kind = TypeKind.INTERFACE || kvp.Value.Kind = TypeKind.UNION then
let itype = getProvidedType kvp.Value.Name
let ptypes = possibleTypes kvp.Value
ptypes |> Array.iter (fun ptype -> ptype.AddInterfaceImplementation(itype)))
providedTypes.Value
let makeProvidedType(asm : Assembly, ns : string, resolutionFolder : string) =
let generator = ProvidedTypeDefinition(asm, ns, "GraphQLProvider", None)
let staticParams =
[ ProvidedStaticParameter("introspection", typeof<string>)
ProvidedStaticParameter("httpHeaders", typeof<string>, parameterDefaultValue = "")
ProvidedStaticParameter("resolutionFolder", typeof<string>, parameterDefaultValue = resolutionFolder)
ProvidedStaticParameter("uploadInputTypeName", typeof<string>, parameterDefaultValue = "")
ProvidedStaticParameter("clientQueryValidation", typeof<bool>, parameterDefaultValue = true)
ProvidedStaticParameter("explicitOptionalParameters", typeof<bool>, parameterDefaultValue = false) ]
generator.DefineStaticParameters(staticParams, fun tname args ->
let clientQueryValidation : bool = downcast args.[4]
let explicitOptionalParameters : bool = downcast args.[5]
let introspectionLocation = IntrospectionLocation.Create(downcast args.[0], downcast args.[2])
let httpHeadersLocation = StringLocation.Create(downcast args.[1], resolutionFolder)
let uploadInputTypeName =
let name : string = unbox args.[3]
match name with
| null | "" -> None
| _ -> Some name
let maker =
lazy
let tdef = ProvidedTypeDefinition(asm, ns, tname, None)
tdef.AddXmlDoc("A type provider for GraphQL operations.")
tdef.AddMembersDelayed (fun _ ->
let httpHeaders = HttpHeaders.load httpHeadersLocation
let schemaJson =
match introspectionLocation with
| Uri serverUrl ->
use connection = new GraphQLClientConnection()
let response = GraphQLClient.sendIntrospectionRequest connection serverUrl httpHeaders
response.Content.ReadAsStringAsync().Result
| IntrospectionFile path ->
System.IO.File.ReadAllText path
// TODO: Deserialize directly from stream
let schema = Serialization.deserializeSchema schemaJson
let schemaProvidedTypes = getSchemaProvidedTypes(schema, uploadInputTypeName, explicitOptionalParameters)
let typeWrapper = ProvidedTypeDefinition("Types", None, isSealed = true)
typeWrapper.AddMembers(schemaProvidedTypes |> Seq.map (fun kvp -> kvp.Value) |> List.ofSeq)
let operationWrapper = ProvidedTypeDefinition("Operations", None, isSealed = true)
let getContextMethodDef =
let methodParameters =
let serverUrl =
match introspectionLocation with
| Uri serverUrl -> ProvidedParameter("serverUrl", typeof<string>, optionalValue = serverUrl)
| _ -> ProvidedParameter("serverUrl", typeof<string>)
let httpHeaders = ProvidedParameter("httpHeaders", typeof<seq<string * string>>, optionalValue = null)
let connectionFactory = ProvidedParameter("connectionFactory", typeof<unit -> GraphQLClientConnection>, optionalValue = null)
[serverUrl; httpHeaders; connectionFactory]
let defaultHttpHeadersExpr =
let names = httpHeaders |> Seq.map fst |> Array.ofSeq
let values = httpHeaders |> Seq.map snd |> Array.ofSeq
Expr.Coerce(<@@ Array.zip names values @@>, typeof<seq<string * string>>)
let invoker (args : Expr list) =
let serverUrl = args.[0]
<@@ let httpHeaders =
match %%args.[1] : seq<string * string> with
| null -> %%defaultHttpHeadersExpr
| argHeaders -> argHeaders
let connectionFactory =
match %%args.[2] : unit -> GraphQLClientConnection with
| argHeaders when obj.Equals(argHeaders, null) -> fun () -> new GraphQLClientConnection()
| argHeaders -> argHeaders
{ ServerUrl = %%serverUrl; HttpHeaders = httpHeaders; Connection = connectionFactory() } @@>
ProvidedMethod("GetContext", methodParameters, typeof<GraphQLProviderRuntimeContext>, invoker, isStatic = true)
let operationMethodDef =
let staticParams =
[ ProvidedStaticParameter("query", typeof<string>)
ProvidedStaticParameter("resolutionFolder", typeof<string>, parameterDefaultValue = resolutionFolder)
ProvidedStaticParameter("operationName", typeof<string>, parameterDefaultValue = "")
ProvidedStaticParameter("typeName", typeof<string>, parameterDefaultValue = "") ]
let staticMethodDef = ProvidedMethod("Operation", [], typeof<OperationBase>, isStatic = true)
let instanceBuilder (methodName : string) (args : obj []) =
let queryLocation = StringLocation.Create(downcast args.[0], downcast args.[1])
let query =
match queryLocation with
| String query -> query
| File path -> System.IO.File.ReadAllText(path)
let queryAst = Parser.parse query
#if IS_DESIGNTIME
let throwExceptionIfValidationFailed (validationResult : ValidationResult<GQLProblemDetails>) =
let rec formatValidationExceptionMessage(errors : GQLProblemDetails list) =
match errors with
| [] -> "Query validation resulted in invalid query, but no validation messages were produced."
| errors ->
errors
|> List.map (fun err ->
match err.Path with
| Include path -> $"%s{err.Message} Path: %A{path}"
| Skip -> err.Message)
|> List.reduce (fun x y -> x + Environment.NewLine + y)
match validationResult with
| ValidationError msgs -> failwith (formatValidationExceptionMessage msgs)
| Success -> ()
let key = { DocumentId = queryAst.GetHashCode(); SchemaId = schema.GetHashCode() }
let refMaker = lazy Validation.Ast.validateDocument schema queryAst
if clientQueryValidation then
refMaker.Force
|> QueryValidationDesignTimeCache.getOrAdd key
|> throwExceptionIfValidationFailed
#endif
let operationName : OperationName voption =
match args.[2] :?> string with
| null | "" ->
let operationDefinitions = queryAst.Definitions |> List.filter (function OperationDefinition _ -> true | _ -> false)
match operationDefinitions with
| opdef :: _ -> opdef.Name
| _ -> failwith "Error parsing query. Can not choose a default operation: query document has no operation definitions."
| x -> ValueSome x
let explicitOperationTypeName : TypeName voption =
match args.[3] :?> string with
| null | "" -> ValueNone
| x -> ValueSome x
let operationDefinition =
queryAst.Definitions
|> Seq.vchoose (function OperationDefinition odef -> ValueSome odef | _ -> ValueNone)
|> Seq.find (fun d -> d.Name = operationName)
let operationAstFields =
let infoMap = queryAst.GetInfoMap()
match infoMap.TryFind(operationName) with
| Some fields -> fields
| None -> failwith "Error parsing query. Could not find field information for requested operation."
let operationTypeRef =
let tref =
match operationDefinition.OperationType with
| Query -> schema.QueryType
| Mutation ->
match schema.MutationType with
| Some tref -> tref
| None -> failwith "The operation is a mutation operation, but the schema does not have a mutation type."
| Subscription ->
match schema.SubscriptionType with
| Some tref -> tref
| None -> failwithf "The operation is a subscription operation, but the schema does not have a subscription type."
let tinst =
match tref.Name with
| Some name -> schema.Types |> Array.tryFind (fun t -> t.Name = name)
| None -> None
match tinst with
| Some t -> { tref with Kind = t.Kind }
| None -> failwith "The operation was found in the schema, but it does not have a name."
let schemaTypes = TypeMapping.getSchemaTypes schema
let enumProvidedTypes = schemaProvidedTypes |> Map.filter (fun _ t -> t.BaseType = typeof<EnumBase>)
let actualQuery = queryAst.ToQueryString(QueryStringPrintingOptions.IncludeTypeNames).Replace("\r\n", "\n")
let className =
match explicitOperationTypeName, operationDefinition.Name with
| ValueSome name, _ -> name.FirstCharUpper()
| ValueNone, ValueSome name -> name.FirstCharUpper()
| ValueNone, ValueNone -> "Operation" + actualQuery.MD5Hash()
let metadata = getOperationMetadata(schemaTypes, uploadInputTypeName, enumProvidedTypes, operationAstFields, operationTypeRef, explicitOptionalParameters)
let operationTypeName : TypeName =
match operationTypeRef.Name with
| Some name -> name
| None -> failwith "Error parsing query. Operation type does not have a name."
let rec getKind (tref : IntrospectionTypeRef) =
match tref.Kind with
| TypeKind.NON_NULL | TypeKind.LIST when tref.OfType.IsSome -> getKind tref.OfType.Value
| _ -> tref.Kind
let rec getTypeName (tref : IntrospectionTypeRef) =
match tref.Kind with
| TypeKind.NON_NULL | TypeKind.LIST when tref.OfType.IsSome -> getTypeName tref.OfType.Value
| _ ->
match tref.Name with
| Some tname -> tname
| None -> failwithf "Expected type kind \"%s\" to have a name, but it does not have a name." (tref.Kind.ToString())
let rec getIntrospectionType (tref : IntrospectionTypeRef) =
match tref.Kind with
| TypeKind.NON_NULL | TypeKind.LIST when tref.OfType.IsSome -> getIntrospectionType tref.OfType.Value
| _ ->
let typeName = getTypeName tref
match schemaTypes.TryFind(typeName) with
| Some t -> t
| None -> failwithf "Type \"%s\" was not found in the introspection schema." typeName
let getOperationFields (operationAstFields : AstFieldInfo list) (operationType : IntrospectionType) =
let rec helper (acc : SchemaFieldInfo list) (astFields : AstFieldInfo list) (introspectionType : IntrospectionType) =
match introspectionType.Kind with
| TypeKind.OBJECT | TypeKind.INTERFACE | TypeKind.UNION ->
match astFields with
| [] -> acc
| field :: tail ->
let throw typeName = failwithf "Field \"%s\" of type \"%s\" was not found in the introspection schema." field.Name typeName
let tref =
match field with
| FragmentField fragf ->
let fragmentType =
let tref =
Option.defaultValue [||] introspectionType.PossibleTypes
|> Array.map getIntrospectionType
|> Array.append [|introspectionType|]
|> Array.tryFind (fun pt -> pt.Name = fragf.TypeCondition)
match tref with
| Some t -> t
| None -> failwithf "Fragment field defines a type condition \"%s\", but that type was not found in the schema definition." fragf.TypeCondition
let field =
fragmentType.Fields
|> Option.map (Array.tryFind (fun f -> f.Name = fragf.Name))
|> Option.flatten
match field with
| Some f -> f.Type
| None -> throw fragmentType.Name
| TypeField typef ->
let field =
introspectionType.Fields
|> Option.map (Array.tryFind (fun f -> f.Name = typef.Name))
|> Option.flatten
match field with
| Some f -> f.Type
| None -> throw introspectionType.Name
let fields =
match getKind tref with
| TypeKind.OBJECT | TypeKind.INTERFACE | TypeKind.UNION ->
let schemaType = getIntrospectionType tref
helper [] field.Fields schemaType
| _ -> []
let info = { AliasOrName = field.AliasOrName.FirstCharUpper(); SchemaTypeRef = tref; Fields = Array.ofList fields }
helper (info :: acc) tail introspectionType
| _ -> []
helper [] operationAstFields operationType |> Array.ofList
// Every time we run the query, we will need the schema types information as an expression.
// To avoid creating the type map expression every time we call Run method, we cache it here.
let operationFieldsExpr = getOperationFields operationAstFields (getIntrospectionType operationTypeRef) |> QuotationHelpers.arrayExpr |> snd
let contextInfo : GraphQLRuntimeContextInfo option =
match introspectionLocation with
| Uri serverUrl -> Some { ServerUrl = serverUrl; HttpHeaders = httpHeaders }
| _ -> None
let operationDef = ProvidedOperation.makeProvidedType(actualQuery, operationDefinition, operationTypeName, operationFieldsExpr, schemaTypes, schemaProvidedTypes, metadata.OperationType, contextInfo, metadata.UploadInputTypeName, className, explicitOptionalParameters)
operationDef.AddMember(metadata.TypeWrapper)
let invoker (_ : Expr list) = <@@ OperationBase(query) @@>
let methodDef = ProvidedMethod(methodName, [], operationDef, invoker, isStatic = true)
methodDef.AddXmlDoc("Creates an operation to be executed on the server and provide its return types.")
operationWrapper.AddMember(operationDef)
operationDef.AddMember(methodDef)
methodDef
staticMethodDef.DefineStaticParameters(staticParams, instanceBuilder)
staticMethodDef
let schemaPropertyDef =
let getter = QuotationHelpers.quoteRecord schema (fun (_ : Expr list) schema -> schema)
ProvidedProperty("Schema", typeof<IntrospectionSchema>, getter, isStatic = true)
let members : MemberInfo list = [typeWrapper; operationWrapper; getContextMethodDef; operationMethodDef; schemaPropertyDef]
members)
tdef
#if IS_DESIGNTIME
let providerKey =
{ IntrospectionLocation = introspectionLocation
CustomHttpHeadersLocation = httpHeadersLocation
UploadInputTypeName = uploadInputTypeName
ResolutionFolder = resolutionFolder
ClientQueryValidation = clientQueryValidation
ExplicitOptionalParameters = explicitOptionalParameters }
ProviderDesignTimeCache.getOrAdd providerKey maker.Force)
#else
maker.Force())
#endif
generator