Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
sudo: false

dist: focal
language: node_js
node_js: node

cache:
directories:
- elm-stuff/build-artifacts
- elm-stuff/packages
- sysconfcpus
os:
- linux

env: ELM_VERSION=0.19.0
env: ELM_VERSION=0.19.1

before_install:
- echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config

install:
- node --version
- npm --version
- npm install -g elm@$ELM_VERSION elm-test@0.19.0-beta4
- npm install elm@$ELM_VERSION elm-test@$ELM_VERSION [email protected]
# Faster compile on Travis.
- |
if [ ! -d sysconfcpus/bin ];
Expand All @@ -32,4 +31,5 @@ install:
fi

script:
- $TRAVIS_BUILD_DIR/sysconfcpus/bin/sysconfcpus -n 2 elm-test
- $TRAVIS_BUILD_DIR/sysconfcpus/bin/sysconfcpus -n 2 ./node_modules/.bin/elm-test --compiler $TRAVIS_BUILD_DIR/node_modules/.bin/elm
- $TRAVIS_BUILD_DIR/sysconfcpus/bin/sysconfcpus -n 2 ./node_modules/.bin/elm-format --validate .
41 changes: 22 additions & 19 deletions example/Main.elm
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Main exposing (..)

import Html exposing (Html, div, text)
import GraphQL.Client.Http as GraphQLClient
import GraphQL.Request.Builder exposing (..)
import GraphQL.Request.Builder.Arg as Arg
import GraphQL.Request.Builder.Variable as Var
import GraphQL.Client.Http as GraphQLClient
import Html exposing (Html, div, text)
import Task exposing (Task)


Expand All @@ -20,6 +20,7 @@ type alias FilmSummary =
{-| The definition of `starWarsRequest` builds up a query request value that
will later be encoded into the following GraphQL query document:

```graphql
fragment filmPlanetsFragment on Film {
planetConnection(first: $pageSize) {
edges {
Expand All @@ -43,9 +44,11 @@ query ($filmID: ID!, $pageSize: Int = 3) {
...filmPlanetsFragment
}
}
```

This query is sent along with variable values extracted from the record passed
to `request`, and the response is decoded into a `FilmSummary`.

-}
starWarsRequest : Request Query FilmSummary
starWarsRequest =
Expand All @@ -66,24 +69,24 @@ starWarsRequest =
)
)
in
extract
(field "film"
[ ( "filmID", Arg.variable filmID ) ]
(object FilmSummary
|> with (field "title" [] (nullable string))
|> with
(field "characterConnection"
[ ( "first", Arg.variable pageSize ) ]
(connectionNodes (extract (field "name" [] (nullable string))))
)
|> with (fragmentSpread planetsFragment)
)
extract
(field "film"
[ ( "filmID", Arg.variable filmID ) ]
(object FilmSummary
|> with (field "title" [] (nullable string))
|> with
(field "characterConnection"
[ ( "first", Arg.variable pageSize ) ]
(connectionNodes (extract (field "name" [] (nullable string))))
)
|> with (fragmentSpread planetsFragment)
)
|> queryDocument
|> request
{ filmID = "1"
, pageSize = Nothing
}
)
|> queryDocument
|> request
{ filmID = "1"
, pageSize = Nothing
}


{-| A function that helps you extract node objects from paginated Relay connections.
Expand Down
72 changes: 31 additions & 41 deletions src/GraphQL/Client/Http.elm
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
module GraphQL.Client.Http
exposing
( RequestError
, DocumentLocation
, Error(..)
, RequestOptions
, sendQuery
, sendMutation
, customSendQuery
, customSendQueryRaw
, customSendMutation
, customSendMutationRaw
)
module GraphQL.Client.Http exposing (Error(..), RequestError, DocumentLocation, sendQuery, sendMutation, RequestOptions, customSendQuery, customSendMutation, customSendQueryRaw, customSendMutationRaw)

{-| The functions in this module let you perform HTTP requests to conventional GraphQL server endpoints.

@docs Error, RequestError, DocumentLocation, sendQuery, sendMutation, RequestOptions, customSendQuery, customSendMutation, customSendQueryRaw, customSendMutationRaw
@docs Error, RequestError, DocumentLocation, sendQuery, sendMutation, RequestOptions, customSendQuery, customSendMutation, customSendQueryRaw, customSendMutationRaw

-}

import GraphQL.Client.Http.Util as Util
Expand Down Expand Up @@ -126,17 +115,18 @@ Example of response decoding:
, withCredentials = False
}
in
request
|> GraphQL.Client.Http.customSendQueryRaw options
|> Task.andThen
(\response ->
case Json.Decode.decodeString decoder response.body of
Err err ->
Task.fail <| GraphQL.Client.Http.HttpError <| Http.BadPayload err response

Ok decodedValue ->
Task.succeed decodedValue
)
request
|> GraphQL.Client.Http.customSendQueryRaw options
|> Task.andThen
(\response ->
case Json.Decode.decodeString decoder response.body of
Err err ->
Task.fail <| GraphQL.Client.Http.HttpError <| Http.BadPayload err response

Ok decodedValue ->
Task.succeed decodedValue
)

-}
customSendQueryRaw :
RequestOptions
Expand Down Expand Up @@ -175,17 +165,17 @@ Example of response decoding:
, withCredentials = False
}
in
mutationRequest
|> GraphQL.Client.Http.customSendMutationRaw options
|> Task.andThen
(\response ->
case Json.Decode.decodeString decoder response.body of
Err err ->
Task.fail <| GraphQL.Client.Http.HttpError <| Http.BadPayload err response

Ok decodedValue ->
Task.succeed decodedValue
)
mutationRequest
|> GraphQL.Client.Http.customSendMutationRaw options
|> Task.andThen
(\response ->
case Json.Decode.decodeString decoder response.body of
Err err ->
Task.fail <| GraphQL.Client.Http.HttpError <| Http.BadPayload err response

Ok decodedValue ->
Task.succeed decodedValue
)

-}
customSendMutationRaw :
Expand All @@ -210,7 +200,7 @@ send options request =
expect =
Util.defaultExpect (Builder.responseDataDecoder request)
in
sendExpecting expect options request
sendExpecting expect options request


sendExpecting :
Expand All @@ -226,7 +216,7 @@ sendExpecting expect requestOptions request =
variableValues =
Builder.jsonVariableValues request
in
Util.requestConfig requestOptions documentString expect variableValues
|> Http.request
|> Http.toTask
|> Task.mapError (Util.convertHttpError HttpError GraphQLError)
Util.requestConfig requestOptions documentString expect variableValues
|> Http.request
|> Http.toTask
|> Task.mapError (Util.convertHttpError HttpError GraphQLError)
36 changes: 19 additions & 17 deletions src/GraphQL/Client/Http/Util.elm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ postBodyJson documentString variableValues =
|> Maybe.map (\obj -> [ ( "variables", obj ) ])
|> Maybe.withDefault []
in
Json.Encode.object ([ ( "query", documentValue ) ] ++ extraParams)
Json.Encode.object ([ ( "query", documentValue ) ] ++ extraParams)


postBody : String -> Maybe Json.Encode.Value -> Http.Body
Expand All @@ -32,6 +32,7 @@ parameterizedUrl url documentString variableValues =
firstParamPrefix =
if String.contains "?" url then
"&"

else
"?"

Expand All @@ -46,7 +47,7 @@ parameterizedUrl url documentString variableValues =
)
|> Maybe.withDefault ""
in
url ++ queryParam ++ variablesParam
url ++ queryParam ++ variablesParam


type alias RequestOptions =
Expand Down Expand Up @@ -107,17 +108,18 @@ requestConfig requestOptions documentString expect variableValues =
( url, body ) =
if requestOptions.method == "GET" then
( parameterizedUrl requestOptions.url documentString variableValues, Http.emptyBody )

else
( requestOptions.url, postBody documentString variableValues )
in
{ method = requestOptions.method
, headers = requestOptions.headers
, url = url
, body = body
, expect = expect
, timeout = requestOptions.timeout
, withCredentials = requestOptions.withCredentials
}
{ method = requestOptions.method
, headers = requestOptions.headers
, url = url
, body = body
, expect = expect
, timeout = requestOptions.timeout
, withCredentials = requestOptions.withCredentials
}


defaultExpect : Json.Decode.Decoder result -> Http.Expect result
Expand All @@ -139,12 +141,12 @@ convertHttpError wrapHttpError wrapGraphQLError httpError =
|> Result.map wrapGraphQLError
|> Result.withDefault (wrapHttpError httpError)
in
case httpError of
Http.BadStatus { body } ->
handleErrorWithResponseBody body
case httpError of
Http.BadStatus { body } ->
handleErrorWithResponseBody body

Http.BadPayload _ { body } ->
handleErrorWithResponseBody body
Http.BadPayload _ { body } ->
handleErrorWithResponseBody body

_ ->
wrapHttpError httpError
_ ->
wrapHttpError httpError
9 changes: 4 additions & 5 deletions src/GraphQL/Request.elm
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module GraphQL.Request
exposing
( Document
, Request
)
module GraphQL.Request exposing
( Document
, Request
)

import GraphQL.Request.Document.AST as AST
import GraphQL.Request.Document.AST.Serialize exposing (serializeDocument)
Expand Down
Loading