-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathAstExtensions.fs
More file actions
356 lines (341 loc) · 14.2 KB
/
Copy pathAstExtensions.fs
File metadata and controls
356 lines (341 loc) · 14.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
/// Extensions for Ast.Document.
module FSharp.Data.GraphQL.Ast.Extensions
open System
open System.Globalization
open System.Runtime.InteropServices
open System.Text
open FSharp.Data.GraphQL
open FSharp.Data.GraphQL.Ast
type OperationName = string
type AstTypeFieldInfo = {
Name : string
Alias : string voption
Fields : AstFieldInfo list
} with
member x.AliasOrName = x.Alias |> ValueOption.defaultValue x.Name
and AstFragmentFieldInfo = {
Name : string
Alias : string voption
TypeCondition : string
Fields : AstFieldInfo list
} with
member x.AliasOrName = x.Alias |> ValueOption.defaultValue x.Name
and internal AstSelectionInfo = {
TypeCondition : string voption
Name : string
Alias : string voption
Path : FieldPath
mutable Fields : AstSelectionInfo list
} with
member x.AliasOrName = x.Alias |> ValueOption.defaultValue x.Name
static member Create
(typeCondition : string voption, path : FieldPath, name : string, alias : string voption, [<Optional>] fields : AstSelectionInfo list)
=
{
TypeCondition = typeCondition
Name = name
Alias = alias
Path = path
Fields = if obj.ReferenceEquals (fields, null) then [] else fields
}
member x.SetFields (fields : AstSelectionInfo list) = x.Fields <- fields
and AstFieldInfo =
| TypeField of AstTypeFieldInfo
| FragmentField of AstFragmentFieldInfo
static member internal Create (info : AstSelectionInfo) =
let fields = List.map AstFieldInfo.Create info.Fields
match info.TypeCondition with
| ValueSome typeCondition ->
FragmentField {
Name = info.Name
Alias = info.Alias
TypeCondition = typeCondition
Fields = fields
}
| ValueNone -> TypeField { Name = info.Name; Alias = info.Alias; Fields = fields }
member x.Name =
match x with
| TypeField info -> info.Name
| FragmentField info -> info.Name
member x.Alias =
match x with
| TypeField info -> info.Alias
| FragmentField info -> info.Alias
member x.AliasOrName =
match x with
| TypeField info -> info.Alias |> ValueOption.defaultValue info.Name
| FragmentField info -> info.Alias |> ValueOption.defaultValue info.Name
member x.Fields =
match x with
| TypeField info -> info.Fields
| FragmentField info -> info.Fields
type internal PaddedStringBuilder () =
let sb = StringBuilder ()
let mutable padCount = 0
member _.Pad () = padCount <- padCount + 2
member _.Unpad () = padCount <- padCount - 2
member _.AppendLine () =
sb.AppendLine().Append ("".PadLeft (padCount, ' '))
|> ignore
member _.Append (str : string) = sb.Append (str) |> ignore
override _.ToString () = sb.ToString ()
/// Specify voptions when printing an Ast.Document to a query string.
[<Flags>]
type QueryStringPrintingOptions =
/// No specific printing option.
| None = 0
/// Includes type names on selections by adding "__typename" meta field to them.
| IncludeTypeNames = 1
type Document with
/// <summary>
/// Generates a GraphQL query string from this document.
/// </summary>
/// <param name="options">Specify custom printing voptions for the query string.</param>
member x.ToQueryString ([<Optional; DefaultParameterValue(QueryStringPrintingOptions.None)>] options : QueryStringPrintingOptions) =
let sb = PaddedStringBuilder ()
let escapeGraphQLString (s : string) =
let escaped = StringBuilder (s.Length + s.Length / 4 + 2)
escaped.Append ('"') |> ignore
for c in s do
let appendStr =
match c with
| '"' -> "\\\""
| '\\' -> "\\\\"
| '\b' -> "\\b"
| '\f' -> "\\f"
| '\n' -> "\\n"
| '\r' -> "\\r"
| '\t' -> "\\t"
| '\u2028' -> "\\u2028"
| '\u2029' -> "\\u2029"
| c when c < '\u0020' ->
let hex = (int c).ToString ("x4", CultureInfo.InvariantCulture)
"\\u" + hex
| c -> string c
escaped.Append (appendStr) |> ignore
escaped.Append('"').ToString ()
let withQuotes = escapeGraphQLString
let rec printValue x =
let printObjectValue (name, value) =
sb.Append (name)
sb.Append (": ")
printValue value
let printOne f n ov =
match n with
| 0 -> f ov
| _ ->
sb.Append (", ")
f ov
let printCompound braces f xs =
match xs with
| [] -> sb.Append (braces)
| xs ->
sb.Append (braces.Substring (0, 1) + " ")
List.iteri (printOne f) xs
sb.Append (" " + braces.Substring (1, 1))
match x with
| IntValue x -> sb.Append (x.ToString (CultureInfo.InvariantCulture))
| FloatValue x -> sb.Append (x.ToString (CultureInfo.InvariantCulture))
| BooleanValue x -> sb.Append (if x then "true" else "false")
| StringValue x -> sb.Append (withQuotes x)
| EnumValue x -> sb.Append (x)
| NullValue -> sb.Append ("null")
| ListValue x -> printCompound "[]" printValue x
| ObjectValue x -> printCompound "{}" printObjectValue (Map.toList x)
| VariableName x -> sb.Append ("$" + x)
let printVariables (vdefs : VariableDefinition list) =
let printVariable (vdef : VariableDefinition) =
sb.Append ("$")
sb.Append (vdef.VariableName)
sb.Append (": ")
sb.Append (vdef.Type.ToString ())
vdef.DefaultValue
|> Option.iter (fun value ->
sb.Append (" = ")
printValue value)
if vdefs.Length > 0 then
sb.Append ("(")
let rec helper vdefs =
match vdefs with
| [] -> ()
| [ vdef ] ->
printVariable vdef
sb.Append (") ")
| vdef :: tail ->
printVariable vdef
sb.Append (", ")
helper tail
helper vdefs
let printArguments (arguments : Argument list) =
let printArgument (arg : Argument) =
sb.Append (arg.Name + ": ")
printValue arg.Value
if arguments.Length > 0 then
sb.Append ("(")
let rec helper args =
match args with
| [] -> ()
| [ arg ] ->
printArgument arg
sb.Append (")")
| arg :: tail ->
printArgument arg
sb.Append (", ")
helper tail
helper arguments
let printDirectives (directives : Directive list) =
let printDirective (directive : Directive) =
sb.Append ("@" + directive.Name)
printArguments directive.Arguments
let rec helper directives =
match directives with
| [] -> ()
| [ directive ] -> printDirective directive
| directive :: tail ->
printDirective directive
sb.Append (" ")
helper tail
helper directives
let setSelectionSetOptions (selectionSet : Selection list) =
let typeNameMetaField = {
Alias = ValueNone
Name = "__typename"
Arguments = []
Directives = []
SelectionSet = []
}
let shouldIncludeTypeName = options.HasFlag (QueryStringPrintingOptions.IncludeTypeNames)
let hasTypeName =
selectionSet
|> List.exists (function
| Field f -> f.Name = "__typename"
| _ -> false)
if
selectionSet.Length > 0
&& shouldIncludeTypeName
&& not (hasTypeName)
then
selectionSet @ [ Field typeNameMetaField ]
else
selectionSet
let rec printSelectionSet (selectionSet : Selection list) =
let printSelection =
function
| Field field ->
field.Alias
|> ValueOption.iter (fun alias -> sb.Append (alias + ": "))
sb.Append (field.Name)
printArguments field.Arguments
if field.Directives.Length > 0 then
sb.Append (" ")
printDirectives field.Directives
if field.SelectionSet.Length > 0 then
sb.Append (" ")
printSelectionSet (setSelectionSetOptions field.SelectionSet)
| FragmentSpread frag ->
sb.Append ("..." + frag.Name)
if frag.Directives.Length > 0 then
sb.Append (" ")
printDirectives frag.Directives
| InlineFragment frag ->
sb.Append ("... ")
frag.TypeCondition
|> ValueOption.iter (fun t -> sb.Append ("on " + t))
printDirectives frag.Directives
sb.Append (" ")
printSelectionSet (setSelectionSetOptions frag.SelectionSet)
if selectionSet.Length > 0 then
sb.Append ("{")
sb.Pad ()
let rec helper selectionSet =
match selectionSet with
| [] -> ()
| [ selection ] ->
sb.AppendLine ()
printSelection selection
sb.Unpad ()
sb.AppendLine ()
sb.Append ("}")
| selection :: tail ->
sb.AppendLine ()
printSelection selection
helper tail
helper selectionSet
let rec printDefinitions (definitions : Definition list) =
let printDefinition =
function
| OperationDefinition odef ->
match odef.OperationType with
| Query when odef.IsShortHandQuery -> ()
| Query -> sb.Append ("query ")
| Mutation -> sb.Append ("mutation ")
| Subscription -> sb.Append ("subscription ")
odef.Name
|> ValueOption.iter (fun name ->
if odef.VariableDefinitions.Length = 0 then
sb.Append (name + " ")
else
sb.Append (name))
printVariables odef.VariableDefinitions
printDirectives odef.Directives
printSelectionSet (setSelectionSetOptions odef.SelectionSet)
| FragmentDefinition fdef ->
sb.Append ("fragment " + fdef.Name.Value + " ")
sb.Append ("on " + fdef.TypeCondition.Value + " ")
printDirectives fdef.Directives
if fdef.Directives.Length > 0 then
sb.Append (" ")
printSelectionSet (setSelectionSetOptions fdef.SelectionSet)
match definitions with
| [] -> ()
| [ def ] -> printDefinition def
| def :: tail ->
printDefinition def
sb.AppendLine ()
sb.AppendLine ()
printDefinitions tail
printDefinitions x.Definitions
sb.ToString ()
/// <summary>
/// Gets a map containing general information for this Document.
/// </summary>
member this.GetInfoMap () : Map<OperationName voption, AstFieldInfo list> =
let fragments =
this.Definitions
|> List.choose (function
| OperationDefinition _ -> None
| FragmentDefinition def -> Some def)
|> List.map (fun def -> def.Name.Value, def)
|> Map.ofList
let findFragment name =
match Map.tryFind name fragments with
| Some fdef -> fdef
| None -> failwithf "Can not get information about fragment \"%s\". Fragment spread definition was not found in the query." name
let operations =
this.Definitions
|> List.choose (function
| FragmentDefinition _ -> None
| OperationDefinition def -> Some def)
|> List.map (fun operation -> operation.Name, operation)
let mapper (selectionSet : Selection list) =
let rec helper (acc : AstSelectionInfo list) (typeCondition : string voption) (path : FieldPath) (selectionSet : Selection list) =
match selectionSet with
| [] -> acc
| selection :: tail ->
let acc =
match selection with
| Field f ->
let finfo = AstSelectionInfo.Create (typeCondition, path, f.Name, f.Alias)
let fields = helper [] ValueNone (f.AliasOrName :: path) f.SelectionSet
finfo.SetFields (fields)
finfo :: acc
| FragmentSpread f ->
let fdef = findFragment f.Name
helper acc fdef.TypeCondition path fdef.SelectionSet
| InlineFragment fdef -> helper acc fdef.TypeCondition path fdef.SelectionSet
helper acc typeCondition path tail
helper [] ValueNone [] selectionSet
|> List.map AstFieldInfo.Create
operations
|> List.map (fun (n, o) -> n, mapper o.SelectionSet)
|> Map.ofList