jamesmacaulay / elm-graphql / GraphQL.Client.Http

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


type Error
    = HttpError Http.Error
    | GraphQLError (List RequestError)

Represents errors that can occur when sending a GraphQL request over HTTP.


type alias RequestError =
{ message : String
, locations : List DocumentLocation 
}

An error returned by the GraphQL server that indicates there was something wrong with the request.


type alias DocumentLocation =
{ line : Basics.Int
, column : Basics.Int 
}

A location in a GraphQL request document.

sendQuery : String -> GraphQL.Request.Builder.Request GraphQL.Request.Builder.Query result -> Task Error result

Takes a URL and a Query Request and returns a Task that you can perform with Task.attempt which will send a POST request to a GraphQL server at the given endpoint.

sendMutation : String -> GraphQL.Request.Builder.Request GraphQL.Request.Builder.Mutation result -> Task Error result

Takes a URL and a Mutation Request and returns a Task that you can perform with Task.attempt which will send a POST request to a GraphQL server at the given endpoint.


type alias RequestOptions =
{ method : String
, headers : List Http.Header
, url : String
, timeout : Maybe Basics.Float
, withCredentials : Basics.Bool 
}

Options available for customizing GraphQL HTTP requests. method should be either "GET" or "POST". For GET requests, the url is modified to include extra parameters in the query string for the GraphQL document and variables. Otherwise, the document and variables are included in the HTTP request body.

customSendQuery : RequestOptions -> GraphQL.Request.Builder.Request GraphQL.Request.Builder.Query result -> Task Error result

Like sendQuery, but takes an RequestOptions value instead of a URL to let you further customize the HTTP request.

customSendMutation : RequestOptions -> GraphQL.Request.Builder.Request GraphQL.Request.Builder.Mutation result -> Task Error result

Like sendMutation, but takes an RequestOptions value instead of a URL to let you further customize the HTTP request.

customSendQueryRaw : RequestOptions -> GraphQL.Request.Builder.Request GraphQL.Request.Builder.Query result -> Task Error (Http.Response String)

Like sendQuery, but takes an RequestOptions value instead of a URL to let you further customize the HTTP request. You will get a plain Http.Response as Task result.

Useful for things like caching, custom errors decoding, etc.

Example of response decoding:

let
    decoder =
        GraphQL.Request.Builder.responseDataDecoder request
            |> Json.Decode.field "data"

    options =
        { method = "GET"
        , headers = []
        , url = "/graphql"
        , timeout = Nothing
        , 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
            )

customSendMutationRaw : RequestOptions -> GraphQL.Request.Builder.Request GraphQL.Request.Builder.Mutation result -> Task Error (Http.Response String)

Like sendMutation, but takes an RequestOptions value instead of a URL to let you further customize the HTTP request. You will get a plain Http.Response as Task result.

Useful for things like custom errors decoding, etc.

Example of response decoding:

let
    decoder =
        GraphQL.Request.Builder.responseDataDecoder mutationRequest
            |> Json.Decode.field "data"

    options =
        { method = "GET"
        , headers = []
        , url = "/graphql"
        , timeout = Nothing
        , 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
            )