-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraphQElm.elm
297 lines (222 loc) · 6.79 KB
/
GraphQElm.elm
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
module GraphQElm exposing (gql, concatQueries, nest, get, mutate, concatGQLUrl, Query)
{-| GraphQElm exposes a simple Elm API for composing GraphQL queries and making requests to a GraphQL endpoint.
# Defining and Composing Queries
@docs Query, gql, concatQueries, nest
# Making Requests
@docs get, mutate, concatGQLUrl
-}
import GraphQElm.Helpers exposing (..)
import Json.Decode exposing (Decoder)
import Http exposing (get, post, Request, emptyBody)
{-| Query type used to define a GraphQElm query.
import GraphQElm exposing (Query)
query : Query
query =
{ resource = "products"
, fields = [ "name", "price" ]
, args = []
, alias = ""
}
-}
type alias Query =
{ resource : String
, fields : List String
, args : List ( String, String )
, alias : String
}
{-| Converts a Query type into a GraphQL query.
import GraphQElm exposing (Query, gql)
query : Query
query =
{resource = "user"
, fields = ["name", "age", "city"]
, args = [("email", "[email protected]")]
, alias = "steveJobs"
}
gql query == "steveJobs: users(email: "[email protected]"){name, age, city}"
-}
gql : Query -> String
gql query =
parseQuery query
{-| Concatenates non-nested queries, allowing for multiple top-level queries.
import GraphQElm exposing (Query, gql, concatQueries)
usersQuery : Query
usersQuery =
{resource = "users"
, fields = ["name", "age", "city"]
, args = []
, alias = ""
}
boardsQuery : Query
boardsQuery =
{resource = "boards"
, fields = ["title", "created_at"]
, args = []
, alias = ""
}
concatQueries [(gql query1), (gql query2)] == "users{name, age, city}, boards{title, created_at}"
-}
concatQueries : List String -> String
concatQueries queries =
let
queryString =
List.foldr
(\query acc ->
(if not (String.contains query acc) then
(appendQuery query acc)
else
acc
)
)
""
queries
q =
if queryString |> String.endsWith "," then
String.dropRight 1 queryString
else
queryString
in
q
{-| Used for nesting a query.
import module GraphQElm exposing (Query, gql, nest)
usersQuery : Query
usersQuery =
{resource = "users"
, fields = ["name", "age", "city"]
, args = []
, alias = ""
}
boardsQuery : Query
boardsQuery =
{resource = "boards"
, fields = ["title", "created_at"]
, args = []
, alias = ""
}
usersBoards =
nest usersQuery boardsQuery
usersBoards == "users{name, age, city, boards{title, created_at}}"
-- another way to achieve nested queries would be to add (gql someQuery) to the Query fields list.
usersQuery : Query
usersQuery =
{resource = "users"
, fields = ["name", "age", "city", (gql boardsQuery)]
, args = []
, alias = ""
}
usersBoards =
gql usersQuery
-}
nest : String -> String -> String
nest a b =
let
str =
String.dropRight 1 a
in
str ++ "," ++ b ++ "}"
{-| Creates a "GET" request for a given query.
...
userQuery : Query
userQuery =
{resource = "user"
, fields = ["name", "email_address"]
, args = []
, alias = ""
}
view : Model -> Html Msg
view model =
div [onClick (QueryGQL (gql userQuery))] [button [] [text "Send Query"]]
update : Msg -> Model -> ( Model, Cmd Msg )
update m model =
case m of
QueryGQL query ->
( model, Http.send (\res -> GQLResponse res) (get model.serverBaseUrl query userDecoder) )
...
-}
get : String -> String -> Decoder a -> Http.Request a
get url query d =
Http.get (url ++ "?query={" ++ query ++ "}") d
{-| Creates a "POST" request for a given query.
...
createUser : Query
createUser =
{resource = "user"
, fields = ["name", "email_address"]
, args = [("name", "Chris"), ("email_address", "[email protected]")]
, alias = ""
}
view : Model -> Html Msg
view model =
div []
[button [onClick (QueryGQL (gql createUser))] [text "Send Query"]
]
update : Msg -> Model -> ( Model, Cmd Msg )
update m model =
case m of
QueryGQL query ->
( model, Http.send (\res -> GQLResponse res) (mutate model.serverBaseUrl query userDecoder) )
...
-}
mutate : String -> String -> Decoder a -> Http.Request a
mutate url query d =
Http.post (url ++ "?query=mutation{" ++ query ++ "}") Http.emptyBody d
{-| Concatenates a URL and query string.
Useful for custom Http requests as the url field.
mutate : Bool
mutate = False
usersQuery : Query
usersQuery =
{resource = "users"
, fields = ["name", "age"]
, args = []
, alias = ""}
url = concatGQLUrl mutate "http://localhost:4000/graphql" (gql usersQuery)
url == "http://localhost:4000/graphql?query={users{name, age}}"
-}
concatGQLUrl : Bool -> String -> String -> String
concatGQLUrl mutate url query =
case mutate of
False ->
url ++ "?query={" ++ query ++ "}"
True ->
url ++ "?query=mutation{" ++ query ++ "}"
-- private
parseQuery : Query -> String
parseQuery query =
let
args =
List.map
(\i ->
let
( arg, val ) =
i
valStr =
if isInt val then
val
else if String.startsWith "[" val then
val
else if String.startsWith "{" val then
val
else
"\"" ++ val ++ "\""
in
arg ++ ": " ++ valStr
)
query.args
argsString =
List.foldl (\i acc -> i ++ "," ++ acc) "" args
|> String.dropRight 1
finalString =
if (String.length argsString) > 0 then
"(" ++ argsString ++ ")"
else
""
aliasName =
if String.length query.alias > 0 then
query.alias ++ ": "
else
query.alias
queryString =
aliasName ++ query.resource ++ finalString ++ "{" ++ (String.join "," query.fields) ++ "}"
in
queryString