Skip to content

Commit 5b366db

Browse files
committed
Experiment with an expectGraphQL
1 parent 459395b commit 5b366db

File tree

2 files changed

+90
-40
lines changed

2 files changed

+90
-40
lines changed

src/GraphQL/Client/Http.elm

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module GraphQL.Client.Http exposing (Error(..), RequestError, DocumentLocation,
99
import GraphQL.Client.Http.Util as Util
1010
import GraphQL.Request.Builder as Builder
1111
import Http
12+
import Json.Decode
1213
import Task exposing (Task)
1314

1415

@@ -75,9 +76,7 @@ send :
7576
send options request toMsg =
7677
let
7778
expect =
78-
Http.expectJson
79-
(Result.mapError (Util.convertHttpError HttpError GraphQLError) >> toMsg)
80-
(Util.dataDecoder (Builder.responseDataDecoder request))
79+
expectGraphQL toMsg request
8180

8281
documentString =
8382
Builder.requestBody request
@@ -89,5 +88,34 @@ send options request toMsg =
8988
|> Http.request
9089

9190

91+
expectGraphQL :
92+
(Result Error result -> msg)
93+
-> Builder.Request operationType result
94+
-> Http.Expect msg
95+
expectGraphQL toMsg request =
96+
let
97+
decoder =
98+
Builder.responseDataDecoder request
99+
in
100+
Http.expectStringResponse toMsg <|
101+
\response ->
102+
case response of
103+
Http.BadUrl_ url ->
104+
Err (HttpError (Http.BadUrl url))
105+
106+
Http.Timeout_ ->
107+
Err (HttpError Http.Timeout)
108+
109+
Http.NetworkError_ ->
110+
Err (HttpError Http.NetworkError)
111+
112+
Http.BadStatus_ metadata body ->
113+
Err (HttpError (Http.BadStatus metadata.statusCode))
114+
115+
Http.GoodStatus_ metadata body ->
116+
case Json.Decode.decodeString decoder body of
117+
Ok value ->
118+
Ok value
92119

93-
-- |> Task.mapError (
120+
Err err ->
121+
Err (HttpError (Http.BadBody (Json.Decode.errorToString err)))

src/GraphQL/Client/Http/Util.elm

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module GraphQL.Client.Http.Util exposing (..)
1+
module GraphQL.Client.Http.Util exposing (DocumentLocation, Error(..), RequestConfig, RequestError, RequestOptions, defaultRequestOptions, expectGraphQL, parameterizedUrl, postBody, postBodyJson, requestConfig)
22

33
import GraphQL.Response as Response
44
import Http
@@ -18,7 +18,7 @@ postBodyJson documentString variableValues =
1818
|> Maybe.map (\obj -> [ ( "variables", obj ) ])
1919
|> Maybe.withDefault []
2020
in
21-
Json.Encode.object ([ ( "query", documentValue ) ] ++ extraParams)
21+
Json.Encode.object ([ ( "query", documentValue ) ] ++ extraParams)
2222

2323

2424
postBody : String -> Maybe Json.Encode.Value -> Http.Body
@@ -32,6 +32,7 @@ parameterizedUrl url documentString variableValues =
3232
firstParamPrefix =
3333
if String.contains "?" url then
3434
"&"
35+
3536
else
3637
"?"
3738

@@ -46,7 +47,7 @@ parameterizedUrl url documentString variableValues =
4647
)
4748
|> Maybe.withDefault ""
4849
in
49-
url ++ queryParam ++ variablesParam
50+
url ++ queryParam ++ variablesParam
5051

5152

5253
type alias RequestOptions =
@@ -81,7 +82,7 @@ type alias RequestConfig a =
8182
, body : Http.Body
8283
, expect : Http.Expect a
8384
, timeout : Maybe Float
84-
, tracker: Maybe String
85+
, tracker : Maybe String
8586
}
8687

8788

@@ -105,40 +106,61 @@ requestConfig requestOptions documentString expect variableValues =
105106
( url, body ) =
106107
if requestOptions.method == "GET" then
107108
( parameterizedUrl requestOptions.url documentString variableValues, Http.emptyBody )
109+
108110
else
109111
( requestOptions.url, postBody documentString variableValues )
110112
in
111-
{ method = requestOptions.method
112-
, headers = requestOptions.headers
113-
, url = url
114-
, body = body
115-
, expect = expect
116-
, timeout = requestOptions.timeout
117-
, tracker = Nothing
118-
}
119-
120-
121-
dataDecoder =
122-
Json.Decode.field "data"
123-
124-
125-
errorsResponseDecoder : Json.Decode.Decoder (List RequestError)
126-
errorsResponseDecoder =
127-
Json.Decode.field "errors" Response.errorsDecoder
128-
113+
{ method = requestOptions.method
114+
, headers = requestOptions.headers
115+
, url = url
116+
, body = body
117+
, expect = expect
118+
, timeout = requestOptions.timeout
119+
, tracker = Nothing
120+
}
129121

130-
convertHttpError : (Http.Error -> err) -> (List RequestError -> err) -> Http.Error -> err
131-
convertHttpError wrapHttpError wrapGraphQLError httpError =
132-
let
133-
handleErrorWithResponseBody responseBody =
134-
responseBody
135-
|> Json.Decode.decodeString errorsResponseDecoder
136-
|> Result.map wrapGraphQLError
137-
|> Result.withDefault (wrapHttpError httpError)
138-
in
139-
case httpError of
140-
Http.BadBody body ->
141-
handleErrorWithResponseBody body
142122

143-
_ ->
144-
wrapHttpError httpError
123+
expectGraphQL : (Result Error a -> msg) -> Json.Decode.Decoder a -> Http.Expect msg
124+
expectGraphQL toMsg decoder =
125+
Http.expectStringResponse toMsg <|
126+
\response ->
127+
case response of
128+
Http.BadUrl_ url ->
129+
Err (HttpError (Http.BadUrl url))
130+
131+
Http.Timeout_ ->
132+
Err (HttpError Http.Timeout)
133+
134+
Http.NetworkError_ ->
135+
Err (HttpError Http.NetworkError)
136+
137+
Http.BadStatus_ metadata body ->
138+
Err (HttpError (Http.BadStatus metadata.statusCode))
139+
140+
Http.GoodStatus_ metadata body ->
141+
case Json.Decode.decodeString decoder body of
142+
Ok value ->
143+
Ok value
144+
145+
Err err ->
146+
Err (HttpError (Http.BadBody (Json.Decode.errorToString err)))
147+
148+
149+
150+
-- errorsResponseDecoder : Json.Decode.Decoder (List RequestError)
151+
-- errorsResponseDecoder =
152+
-- Json.Decode.field "errors" Response.errorsDecoder
153+
-- convertHttpError : (Http.Error -> err) -> (List RequestError -> err) -> Http.Error -> err
154+
-- convertHttpError wrapHttpError wrapGraphQLError httpError =
155+
-- let
156+
-- handleErrorWithResponseBody responseBody =
157+
-- responseBody
158+
-- |> Json.Decode.decodeString errorsResponseDecoder
159+
-- |> Result.map wrapGraphQLError
160+
-- |> Result.withDefault (wrapHttpError httpError)
161+
-- in
162+
-- case httpError of
163+
-- Http.BadBody body ->
164+
-- handleErrorWithResponseBody body
165+
-- _ ->
166+
-- wrapHttpError httpError

0 commit comments

Comments
 (0)