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
33import GraphQL.Response as Response
44import 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
2424postBody : 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
5253type 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