/
ToGqlString.purs
456 lines (403 loc) · 13.8 KB
/
ToGqlString.purs
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
module GraphQL.Client.ToGqlString
( KeyVals(..)
, KeyVals_
, PropToGqlArg(..)
, PropToGqlString(..)
, ToGqlQueryStringOptions
, class GqlAndArgsString
, class GqlArgString
, class GqlQueryString
, class IsIgnoreArg
, dateString
, emptyKeyVals
, gqlArgStringRecord
, gqlArgStringRecordBody
, gqlArgStringRecordTopLevel
, gqlQueryStringRecord
, indent
, isIgnoreArg
, padMilli
, padl
, padl'
, removeTrailingZeros
, showInt
, timeString
, toGqlAndArgsStringImpl
, toGqlArgString
, toGqlArgStringImpl
, toGqlQueryString
, toGqlQueryStringFormatted
, toGqlQueryStringImpl
, toLines
)
where
import Prelude
import Data.Array (fold, foldMap, intercalate, length, mapWithIndex)
import Data.Array as Array
import Data.Date (Date)
import Data.DateTime (DateTime(..), Millisecond)
import Data.DateTime as DT
import Data.Enum (class BoundedEnum, fromEnum)
import Data.FoldableWithIndex (foldlWithIndex)
import Data.Function (on)
import Data.List (List)
import Data.List as List
import Data.Map (Map)
import Data.Map as Map
import Data.Maybe (Maybe(..), isJust, maybe)
import Data.Monoid (guard, power)
import Data.String (codePointFromChar, fromCodePointArray, joinWith, toCodePointArray)
import Data.String.CodeUnits as String
import Data.String.Regex (split)
import Data.String.Regex.Flags (global)
import Data.String.Regex.Unsafe (unsafeRegex)
import Data.Symbol (class IsSymbol, reflectSymbol)
import Data.Time (Time)
import Foreign (Foreign)
import Foreign.Object (Object)
import Foreign.Object as Object
import GraphQL.Client.Alias (Alias(..))
import GraphQL.Client.Alias.Dynamic (Spread(..))
import GraphQL.Client.Args (AndArgs(AndArgs), Args(..), IgnoreArg, OrArg(..))
import GraphQL.Client.Union (GqlUnion(..))
import GraphQL.Client.Variable (Var)
import GraphQL.Client.Variables (WithVars, getQuery)
import Heterogeneous.Folding (class FoldingWithIndex, class HFoldlWithIndex, hfoldlWithIndex)
import Type.Proxy (Proxy(..))
import Unsafe.Coerce (unsafeCoerce)
-- | Generate a GraphQL query from its purs representation
toGqlQueryString :: forall q. GqlQueryString q => q -> String
toGqlQueryString =
toGqlQueryStringImpl
{ indentation: Nothing
}
-- | Generate a GraphQL query from its purs representation, formatted with indentation for human readability
toGqlQueryStringFormatted :: forall q. GqlQueryString q => q -> String
toGqlQueryStringFormatted =
toGqlQueryStringImpl
{ indentation: Just 2
}
type ToGqlQueryStringOptions
= { indentation :: Maybe Int
}
class GqlQueryString q where
toGqlQueryStringImpl :: ToGqlQueryStringOptions -> q -> String
instance gqlQueryStringUnit :: GqlQueryString Unit where
toGqlQueryStringImpl _ _ = ""
else instance gqlQueryStringWithVars :: GqlQueryString query => GqlQueryString (WithVars query vars) where
toGqlQueryStringImpl opts withVars = toGqlQueryStringImpl opts $ getQuery withVars
else instance gqlQueryStringSymbol :: IsSymbol s => GqlQueryString (Proxy s) where
toGqlQueryStringImpl _ _ = ": " <> reflectSymbol (Proxy :: Proxy s)
else instance gqlQueryStringVar :: IsSymbol s => GqlQueryString (Var s a) where
toGqlQueryStringImpl _ _ = "$" <> reflectSymbol (Proxy :: Proxy s)
else instance gqlQueryStringSpread ::
( IsSymbol alias
, GqlQueryString (Args args fields)
) =>
GqlQueryString (Spread (Proxy alias) args fields) where
toGqlQueryStringImpl opts (Spread alias args fields) = indent opts $ " {" <> nl <> intercalate nl dynamicFields <> nl <> "}"
where
nl = if isJust opts.indentation then "\n" else " "
dynamicFields =
args
# mapWithIndex \idx arg ->
"_"
<> show idx
<> ": "
<> reflectSymbol alias
<> toGqlQueryStringImpl opts (Args arg fields)
else instance gqlQueryStringArgsScalar ::
( HFoldlWithIndex PropToGqlArg KeyVals (Record args) KeyVals
) =>
GqlQueryString (Args { | args } Unit) where
toGqlQueryStringImpl _ (Args args _) = gqlArgStringRecordTopLevel args
else instance gqlQueryStringArgs ::
( HFoldlWithIndex PropToGqlArg KeyVals (Record args) KeyVals
, GqlQueryString body
) =>
GqlQueryString (Args { | args } body) where
toGqlQueryStringImpl opts (Args args body) =
gqlArgStringRecordTopLevel args
<> toGqlQueryStringImpl opts body
else instance gqlQueryStringEmptyRecord ::
HFoldlWithIndex PropToGqlString KeyVals (Record r) KeyVals =>
GqlQueryString (Record r) where
toGqlQueryStringImpl r = gqlQueryStringRecord r
else instance gqlQueryStringGqlUnion ::
HFoldlWithIndex PropToGqlString KeyVals (Record r) KeyVals =>
GqlQueryString (GqlUnion r) where
toGqlQueryStringImpl opts (GqlUnion r) = gqlQueryStringUnion opts r
data PropToGqlString
= PropToGqlString ToGqlQueryStringOptions
newtype KeyVals
= KeyVals KeyVals_
type KeyVals_
= List { key :: String, val :: String }
emptyKeyVals :: KeyVals
emptyKeyVals = KeyVals List.Nil
instance propToGqlStringAlias ::
( GqlQueryString a
, IsSymbol sym
, IsSymbol alias
) =>
FoldingWithIndex PropToGqlString (Proxy sym) KeyVals (Alias (Proxy alias) a) KeyVals where
foldingWithIndex (PropToGqlString opts) prop (KeyVals kvs) (Alias alias a) =
KeyVals
$ { key: reflectSymbol prop
, val:
": "
<> reflectSymbol alias
<> toGqlQueryStringImpl opts a
}
`List.Cons`
kvs
else instance propToGqlString ::
( GqlQueryString a
, IsSymbol sym
) =>
FoldingWithIndex PropToGqlString (Proxy sym) KeyVals a KeyVals where
foldingWithIndex (PropToGqlString opts) prop (KeyVals kvs) a =
KeyVals
$ { key: reflectSymbol prop
, val: toGqlQueryStringImpl opts a
}
`List.Cons`
kvs
gqlQueryStringRecord ::
forall r.
ToGqlQueryStringOptions ->
HFoldlWithIndex PropToGqlString KeyVals { | r } KeyVals =>
{ | r } ->
String
gqlQueryStringRecord opts r = indent opts $ " {" <> body <> newline <> "}"
where
(KeyVals kvs) = hfoldlWithIndex (PropToGqlString opts) emptyKeyVals r
body =
sortByKeyIndex r kvs
# List.foldMap (\{ key, val } -> nl <> key <> val)
multiline = isJust opts.indentation
nl = if isJust opts.indentation then "\n" else " "
newline = guard multiline "\n"
data UnionToGqlString
= UnionToGqlString ToGqlQueryStringOptions
instance unionToGqlString ::
( GqlQueryString a
, IsSymbol sym
) =>
FoldingWithIndex UnionToGqlString (Proxy sym) KeyVals a KeyVals where
foldingWithIndex (UnionToGqlString opts) union (KeyVals kvs) a =
KeyVals
$ { key: reflectSymbol union
, val: toGqlQueryStringImpl opts a
}
`List.Cons`
kvs
gqlQueryStringUnion ::
forall r.
ToGqlQueryStringOptions ->
HFoldlWithIndex PropToGqlString KeyVals { | r } KeyVals =>
{ | r } ->
String
gqlQueryStringUnion opts r = indent opts $
" {" <> newline <>
" __typename" <> newline <>
body <> newline
<> "}"
where
(KeyVals kvs) = hfoldlWithIndex (PropToGqlString opts) emptyKeyVals r
body =
sortByKeyIndex r kvs
# List.foldMap (\{ key, val } -> nl <> "... on " <> key <> val)
multiline = isJust opts.indentation
nl = if multiline then "\n" else " "
newline = guard multiline "\n"
indent ::
forall r.
{ indentation :: Maybe Int
| r
} ->
String -> String
indent opts str = case opts.indentation of
Just indentation ->
let
lines = toLines str
in
lines
# mapWithIndex (\i l -> if i == 0 || i == length lines - 1 then l else power " " indentation <> l)
# joinWith nl
_ -> str
where
multiline = isJust opts.indentation
nl = guard multiline "\n"
toLines :: String -> Array String
toLines = split (unsafeRegex """\n""" global)
toGqlArgString :: forall q. GqlArgString q => q -> String
toGqlArgString = toGqlArgStringImpl
class GqlArgString q where
toGqlArgStringImpl :: q -> String
instance gqlArgStringIgnoreArg :: GqlArgString IgnoreArg where
toGqlArgStringImpl _ = ""
else instance gqlArgStringString :: GqlArgString String where
toGqlArgStringImpl = show
else instance gqlArgStringInt :: GqlArgString Int where
toGqlArgStringImpl = show
else instance gqlArgStringNumber :: GqlArgString Number where
toGqlArgStringImpl = show
else instance gqlArgStringBoolean :: GqlArgString Boolean where
toGqlArgStringImpl = show
else instance gqlArgStringDate :: GqlArgString Date where
toGqlArgStringImpl = show <<< dateString
else instance gqlArgStringTime :: GqlArgString Time where
toGqlArgStringImpl = show <<< timeString
else instance gqlArgStringDateTime :: GqlArgString DateTime where
toGqlArgStringImpl (DateTime d t) = show $ dateString d <> "T" <> timeString t
else instance gqlArgStringMaybe :: GqlArgString a => GqlArgString (Maybe a) where
toGqlArgStringImpl = maybe "null" toGqlArgStringImpl
else instance gqlArgStringArray :: GqlArgString a => GqlArgString (Array a) where
toGqlArgStringImpl = map toGqlArgStringImpl >>> \as -> "[" <> intercalate ", " as <> "]"
else instance gqlArgStringVar :: IsSymbol sym => GqlArgString (Var sym a) where
toGqlArgStringImpl _ = "$" <> reflectSymbol (Proxy :: Proxy sym)
else instance gqlArgStringOrArg ::
(GqlArgString argL, GqlArgString argR) =>
GqlArgString (OrArg argL argR) where
toGqlArgStringImpl = case _ of
ArgL a -> toGqlArgStringImpl a
ArgR a -> toGqlArgStringImpl a
else instance gqlArgStringAndArgs ::
( GqlAndArgsString (AndArgs a1 a2)
) =>
GqlArgString (AndArgs a1 a2) where
toGqlArgStringImpl andArg = "[" <> toGqlAndArgsStringImpl andArg <> "]"
else instance gqlArgStringRecord_ :: HFoldlWithIndex PropToGqlArg KeyVals (Record r) KeyVals => GqlArgString (Record r) where
toGqlArgStringImpl r = gqlArgStringRecord r
dateString :: Date -> String
dateString date =
fold
[ showInt $ DT.year date
, "-"
, padl 2 '0' $ showInt $ DT.month date
, "-"
, padl 2 '0' $ showInt $ DT.day date
]
timeString :: Time -> String
timeString time =
fold
[ padl 2 '0' $ showInt $ DT.hour time
, ":"
, padl 2 '0' $ showInt $ DT.minute time
, ":"
, padl 2 '0' $ showInt $ DT.second time
, "."
, removeTrailingZeros $ padMilli $ DT.millisecond time
, "Z"
]
showInt :: forall a. BoundedEnum a => a -> String
showInt = show <<< fromEnum
padl :: Int -> Char -> String -> String
padl n chr str =
String.fromCharArray
$ padl' (n - String.length str) chr (String.toCharArray str)
padl' :: Int -> Char -> Array Char -> Array Char
padl' n chr chrs
| n <= 0 = chrs
| otherwise = padl' (n - 1) chr (chr `Array.cons` chrs)
-- | Remove trailing zeros from a millisecond value.
removeTrailingZeros :: String -> String
removeTrailingZeros "000" = "0"
removeTrailingZeros s =
fromCodePointArray
<<< Array.reverse
<<< Array.dropWhile (_ == codePointFromChar '0')
<<< Array.reverse
$ toCodePointArray s
-- | Pad an integer from a millisecond value with enough zeros so it is three
-- digits.
padMilli :: Millisecond -> String
padMilli = padl 3 '0' <<< show <<< fromEnum
class GqlAndArgsString q where
toGqlAndArgsStringImpl :: q -> String
instance gqlArgStringAndArgsNotEnd ::
( GqlArgString a1
, GqlAndArgsString (AndArgs a2 a3)
) =>
GqlAndArgsString (AndArgs (Array a1) (AndArgs a2 a3)) where
toGqlAndArgsStringImpl (AndArgs head tail) = foldMap (toGqlArgStringImpl >>> \s -> s <> ", ") head <> toGqlAndArgsStringImpl tail
else instance gqlArgStringAndArgsEndArray ::
( GqlArgString a1
, GqlArgString a2
) =>
GqlAndArgsString (AndArgs (Array a1) (Array a2)) where
toGqlAndArgsStringImpl (AndArgs a1 a2) = intercalate ", " (map toGqlArgStringImpl a1 <> map toGqlArgStringImpl a2)
else instance gqlArgStringAndArgsEnd ::
( GqlArgString a1
, GqlArgString a2
) =>
GqlAndArgsString (AndArgs (Array a1) a2) where
toGqlAndArgsStringImpl (AndArgs a1 a2) = intercalate ", " (map toGqlArgStringImpl a1 <> [ toGqlArgStringImpl a2 ])
data PropToGqlArg
= PropToGqlArg
instance propToGqlArg ::
( GqlArgString a
, IsSymbol sym
, IsIgnoreArg a
) =>
FoldingWithIndex PropToGqlArg (Proxy sym) KeyVals a KeyVals where
foldingWithIndex PropToGqlArg prop (KeyVals kvs) a =
KeyVals
if isIgnoreArg a then
kvs
else
{ key: reflectSymbol prop
, val: toGqlArgStringImpl a
}
`List.Cons`
kvs
-- pre <> reflectSymbol prop <> ": " <> toGqlArgStringImpl a
-- where
-- pre = if str == "" then "" else str <> ", "
gqlArgStringRecord ::
forall r.
HFoldlWithIndex PropToGqlArg KeyVals { | r } KeyVals =>
{ | r } ->
String
gqlArgStringRecord r = "{" <> gqlArgStringRecordBody r <> "}"
gqlArgStringRecordTopLevel ::
forall r.
HFoldlWithIndex PropToGqlArg KeyVals { | r } KeyVals =>
{ | r } ->
String
gqlArgStringRecordTopLevel r = "(" <> gqlArgStringRecordBody r <> ")"
gqlArgStringRecordBody ::
forall r.
HFoldlWithIndex PropToGqlArg KeyVals { | r } KeyVals =>
{ | r } ->
String
gqlArgStringRecordBody r =
sortByKeyIndex r kvs
<#> (\{ key, val } -> key <> ": " <> val)
# List.intercalate ", "
where
(KeyVals kvs) = hfoldlWithIndex PropToGqlArg emptyKeyVals r
sortByKeyIndex :: forall r. { | r } -> KeyVals_ -> KeyVals_
sortByKeyIndex record = List.sortBy (compare `on` getKeyIndex)
where
obj :: Object Foreign
obj = unsafeCoerce record
objKeys :: Array String
objKeys = Object.keys obj
objIdxs :: Map String Int
objIdxs = foldlWithIndex getIdx Map.empty objKeys
getIdx :: Int -> Map String Int -> String -> Map String Int
getIdx idx idxs key = Map.insert key idx idxs
getKeyIndex :: _ -> Maybe Int
getKeyIndex { key } = Map.lookup key objIdxs
class IsIgnoreArg a where
isIgnoreArg :: a -> Boolean
instance isIgnoreArgIgnoreArg :: IsIgnoreArg IgnoreArg where
isIgnoreArg _ = true
else instance isIgnoreArgOrArg :: (IsIgnoreArg l, IsIgnoreArg r) => IsIgnoreArg (OrArg l r) where
isIgnoreArg = case _ of
ArgL l -> isIgnoreArg l
ArgR r -> isIgnoreArg r
else instance isIgnoreArgOther :: IsIgnoreArg a where
isIgnoreArg _ = false