-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathQuery.purs
More file actions
361 lines (333 loc) · 12.9 KB
/
Query.purs
File metadata and controls
361 lines (333 loc) · 12.9 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
module GraphQL.Client.Query
( decodeError
, decodeErrorsMaybe
, decodeGqlRes
, getFullRes
, mutation
, mutationFullRes
, mutationJson
, mutationOpts
, mutationOptsWithDecoder
, mutationWithDecoder
, query
, queryFullRes
, queryJson
, queryOpts
, queryOptsWithDecoder
, queryWithDecoder
) where
import Prelude
import Control.Monad.Error.Class (class MonadThrow)
import Control.Monad.Except (class MonadError, catchError)
import Data.Argonaut.Core (Json, stringify)
import Data.Argonaut.Decode (class DecodeJson, JsonDecodeError(..), decodeJson, printJsonDecodeError)
import Data.Argonaut.Decode.Combinators (getField, (.:), (.:?))
import Data.Array (intercalate, mapMaybe)
import Data.Either (Either(..), hush)
import Data.Maybe (Maybe(..))
import Data.Traversable (traverse)
import Effect.Aff (Aff, Error, error, message, throwError)
import Foreign.Object (Object)
import GraphQL.Client.GqlError (GqlError)
import GraphQL.Client.Operation (OpMutation, OpQuery)
import GraphQL.Client.SafeQueryName (safeQueryName)
import GraphQL.Client.ToGqlString (class GqlQueryString, toGqlQueryString, toGqlQueryStringFormatted)
import GraphQL.Client.Types (class GqlQuery, class QueryClient, Client(..), GqlRes, GqlResJson(..), clientMutation, clientQuery, defMutationOpts, defQueryOpts)
import GraphQL.Client.Variables (class VarsTypeChecked, getVarsJson, getVarsTypeNames)
import Type.Proxy (Proxy(..))
-- | Run a graphQL query with a custom decoder and custom options
queryOptsWithDecoder
:: forall client directives schema query returns queryOpts mutationOpts sr
. QueryClient client queryOpts mutationOpts
=> GqlQuery directives OpQuery schema query returns
=> (Json -> Either JsonDecodeError returns)
-> (queryOpts -> queryOpts)
-> (Client client { directives :: Proxy directives, query :: schema | sr })
-> String
-> query
-> Aff returns
queryOptsWithDecoder d optsF (Client c) name q =
-- runQuery undef undef undef (Proxy :: Proxy schema) undef q
runQuery d opts c (Proxy :: Proxy schema) name q
where
opts = optsF (defQueryOpts c)
-- | Run a graphQL query with custom options
queryOpts
:: forall client directives schema query returns queryOpts mutationOpts sr
. QueryClient client queryOpts mutationOpts
=> GqlQuery directives OpQuery schema query returns
=> DecodeJson returns
=> (queryOpts -> queryOpts)
-> (Client client { directives :: Proxy directives, query :: schema | sr })
-> String
-> query
-> Aff returns
queryOpts = queryOptsWithDecoder decodeJson
-- | Run a graphQL query with a custom decoder
queryWithDecoder
:: forall client directives schema query returns queryOpts mutationOpts sr
. QueryClient client queryOpts mutationOpts
=> GqlQuery directives OpQuery schema query returns
=> (Json -> Either JsonDecodeError returns)
-> (Client client { directives :: Proxy directives, query :: schema | sr })
-> String
-> query
-> Aff returns
queryWithDecoder d (Client c) = runQuery d (defQueryOpts c) c (Proxy :: Proxy schema)
-- | Run a graphQL query
query
:: forall client directives schema query returns queryOpts mutationOpts sr
. QueryClient client queryOpts mutationOpts
=> GqlQuery directives OpQuery schema query returns
=> DecodeJson returns
=> (Client client { directives :: Proxy directives, query :: schema | sr })
-> String
-> query
-> Aff returns
query = queryWithDecoder decodeJson
mutationWithDecoder
:: forall client directives schema mutation returns queryOpts mutationOpts sr
. QueryClient client queryOpts mutationOpts
=> GqlQuery directives OpMutation schema mutation returns
=> (Json -> Either JsonDecodeError returns)
-> (Client client { directives :: Proxy directives, mutation :: schema | sr })
-> String
-> mutation
-> Aff returns
mutationWithDecoder d (Client c) = runMutation d (defMutationOpts c) c (Proxy :: Proxy schema)
mutation
:: forall client directives schema mutation returns queryOpts mutationOpts sr
. QueryClient client queryOpts mutationOpts
=> GqlQuery directives OpMutation schema mutation returns
=> DecodeJson returns
=> (Client client { directives :: Proxy directives, mutation :: schema | sr })
-> String
-> mutation
-> Aff returns
mutation = mutationWithDecoder decodeJson
-- | Run a graphQL query with a custom decoder and custom options
mutationOptsWithDecoder
:: forall client directives schema query returns queryOpts mutationOpts sr
. QueryClient client queryOpts mutationOpts
=> GqlQuery directives OpMutation schema query returns
=> (Json -> Either JsonDecodeError returns)
-> (mutationOpts -> mutationOpts)
-> (Client client { directives :: Proxy directives, mutation :: schema | sr })
-> String
-> query
-> Aff returns
mutationOptsWithDecoder d optsF (Client c) = runMutation d opts c (Proxy :: Proxy schema)
where
opts = optsF (defMutationOpts c)
-- | Run a graphQL query with a custom decoder and custom options
mutationOpts
:: forall client directives schema query returns queryOpts mutationOpts sr
. QueryClient client queryOpts mutationOpts
=> GqlQuery directives OpMutation schema query returns
=> DecodeJson returns
=> (mutationOpts -> mutationOpts)
-> (Client client { directives :: Proxy directives, mutation :: schema | sr })
-> String
-> query
-> Aff returns
mutationOpts = mutationOptsWithDecoder decodeJson
runQuery
:: forall client directives schema query returns qOpts mOpts
. QueryClient client qOpts mOpts
=> GqlQuery directives OpQuery schema query returns
=> VarsTypeChecked schema query
=> (Json -> Either JsonDecodeError returns)
-> qOpts
-> client
-> Proxy schema
-> String
-> query
-> Aff returns
runQuery decodeFn opts client _ queryNameUnsafe q =
addErrorInfo (Proxy @schema) queryName q do
json <- clientQuery opts client queryName (getVarsTypeNames (Proxy :: _ schema) q <> toGqlQueryString q)
(getVarsJson (Proxy :: _ schema) q)
decodeJsonData decodeFn json
where
queryName = safeQueryName queryNameUnsafe
runMutation
:: forall client directives schema query returns qOpts mOpts
. QueryClient client qOpts mOpts
=> GqlQuery directives OpMutation schema query returns
=> (Json -> Either JsonDecodeError returns)
-> mOpts
-> client
-> Proxy schema
-> String
-> query
-> Aff returns
runMutation decodeFn opts client _ queryNameUnsafe q =
addErrorInfo (Proxy @schema) queryName q do
json <- clientMutation opts client queryName (getVarsTypeNames (Proxy :: _ schema) q <> toGqlQueryString q)
(getVarsJson (Proxy :: _ schema) q)
decodeJsonData decodeFn json
where
queryName = safeQueryName queryNameUnsafe
decodeJsonData :: forall m a. MonadThrow Error m => (Json -> Either JsonDecodeError a) -> Json -> m a
decodeJsonData decodeFn json = case decodeGqlRes decodeFn json of
Left err ->
throwError
$ error case decodeJson json of
Right ({ errors } :: { errors :: Array { message :: String } }) -> intercalate ", \n" $ map _.message errors
_ ->
" Response failed to decode from JSON: "
<> printJsonDecodeError err
<> "\n Full response: "
<> stringify json
Right result -> pure result
decodeGqlRes :: forall a. (Json -> Either JsonDecodeError a) -> Json -> Either JsonDecodeError a
decodeGqlRes decodeFn json = do
jsonObj <- decodeJson json
data_ <- getField jsonObj "data"
decodeFn data_
addErrorInfo
:: forall m a q schema
. MonadError Error m
=> VarsTypeChecked schema q
=> GqlQueryString q
=> Proxy schema
-> String
-> q
-> m a
-> m a
addErrorInfo schema queryName q =
flip catchError \err -> do
throwError
$ error
$ "\nGraphQL error: \nname: "
<> show queryName
<> ".\nerror: "
<> message err
<> ".\nquery: "
<> queryName
<> " "
<> getVarsTypeNames schema q
<> toGqlQueryStringFormatted q
-- | Run a graphQL query, getting the full response,
-- | According to https://spec.graphql.org/June2018/#sec-Response-Format
queryFullRes
:: forall client directives schema query returns queryOpts mutationOpts sr
. QueryClient client queryOpts mutationOpts
=> GqlQuery directives OpQuery schema query returns
=> (Json -> Either JsonDecodeError returns)
-> (queryOpts -> queryOpts)
-> (Client client { directives :: Proxy directives, query :: schema | sr })
-> String
-> query
-> Aff (GqlRes returns)
queryFullRes decodeFn optsF (Client client) queryNameUnsafe q =
addErrorInfo (Proxy @schema) queryName q do
(GqlResJson json) :: GqlResJson schema query returns <- queryJson optsF (Client client) queryNameUnsafe q
pure $ getFullRes decodeFn json
where
queryName = safeQueryName queryNameUnsafe
-- | Run a graphQL query, returning the response as json with phantom types
-- | The json will be of the format: https://spec.graphql.org/June2018/#sec-Response-Format
queryJson
:: forall client directives schema query returns queryOpts mutationOpts sr
. QueryClient client queryOpts mutationOpts
=> GqlQuery directives OpQuery schema query returns
=> (queryOpts -> queryOpts)
-> (Client client { directives :: Proxy directives, query :: schema | sr })
-> String
-> query
-> Aff (GqlResJson schema query returns)
queryJson optsF (Client client) queryNameUnsafe q =
GqlResJson <$>
clientQuery opts client queryName (getVarsTypeNames (Proxy :: _ schema) q <> toGqlQueryString q)
(getVarsJson (Proxy :: _ schema) q)
where
opts = optsF (defQueryOpts client)
queryName = safeQueryName queryNameUnsafe
-- | Run a graphQL mutation, getting the full response,
-- | According to https://spec.graphql.org/June2018/#sec-Response-Format
mutationFullRes
:: forall client directives schema mutation returns queryOpts mutationOpts sr
. QueryClient client queryOpts mutationOpts
=> GqlQuery directives OpMutation schema mutation returns
=> (Json -> Either JsonDecodeError returns)
-> (mutationOpts -> mutationOpts)
-> (Client client { directives :: Proxy directives, query :: schema | sr })
-> String
-> mutation
-> Aff (GqlRes returns)
mutationFullRes decodeFn optsF (Client client) queryNameUnsafe q =
addErrorInfo (Proxy @schema) queryName q do
(GqlResJson json) :: GqlResJson schema mutation returns <- mutationJson optsF (Client client) queryNameUnsafe q
pure $ getFullRes decodeFn json
where
queryName = safeQueryName queryNameUnsafe
-- | Run a graphQL mutation, returning the response as json with phantom types
-- | The json will be of the format: https://spec.graphql.org/June2018/#sec-Response-Format
mutationJson
:: forall client directives schema mutation returns queryOpts mutationOpts sr
. QueryClient client queryOpts mutationOpts
=> GqlQuery directives OpMutation schema mutation returns
=> (mutationOpts -> mutationOpts)
-> (Client client { directives :: Proxy directives, mutation :: schema | sr })
-> String
-> mutation
-> Aff (GqlResJson schema mutation returns)
mutationJson optsF (Client client) queryNameUnsafe q =
GqlResJson <$> clientMutation opts client queryName (getVarsTypeNames (Proxy :: _ schema) q <> toGqlQueryString q)
(getVarsJson (Proxy :: _ schema) q)
where
opts = optsF (defMutationOpts client)
queryName = safeQueryName queryNameUnsafe
getFullRes
:: forall res
. (Json -> Either JsonDecodeError res)
-> Json
-> GqlRes res
getFullRes decodeFn json =
let
data_ = decodeGqlRes decodeFn json
errors = getErrors json
extensions = getExtensions json
in
{ data_
, errors_json: errors
, errors: map decodeErrorsMaybe errors
, extensions
}
getErrors :: Json -> Maybe (Array Json)
getErrors json = case decodeJson json of
Right ({ errors } :: { errors :: _ }) -> Just errors
_ -> case decodeJson json of
Right ({ error } :: { error :: { graphQLErrors :: Array Json } }) -> Just error.graphQLErrors
_ -> Nothing
getExtensions :: Json -> Maybe (Object Json)
getExtensions json = case decodeJson json of
Right ({ extensions } :: { extensions :: _ }) -> Just extensions
_ -> Nothing
decodeErrorsMaybe :: Array Json -> Array GqlError
decodeErrorsMaybe = mapMaybe (decodeError >>> hush)
decodeError :: Json -> Either JsonDecodeError GqlError
decodeError json = do
obj :: Object Json <- decodeJson json
message <- decodeJson =<< obj .: "message"
locations <- traverse decodeJson =<< obj .:? "locations"
path <- traverse decodePath =<< obj .:? "path"
extensions <- traverse decodeJson =<< obj .:? "extensions"
pure
{ message
, locations
, path
, extensions
}
where
decodePath :: Json -> Either JsonDecodeError (Array (Either Int String))
decodePath json' = case decodeJson json' of
Right (arr :: Array Json) -> traverse decodePathPart arr
_ -> Left $ TypeMismatch "Path must be an array"
decodePathPart :: Json -> Either JsonDecodeError (Either Int String)
decodePathPart json' = case decodeJson json' of
Right (str :: String) -> pure $ Right str
_ -> case decodeJson json' of
Right (int :: Int) -> pure $ Left int
_ -> Left $ TypeMismatch "Path part must be either a string or an int"