dillonkearns/elm-graphql/1.2.0

NOTE: the latest version is 5.0.12

Graphql.Http

Send requests to your GraphQL endpoint. See this live code demo or the examples/ folder for some end-to-end examples. The builder syntax is inspired by Luke Westby's elm-http-builder package.

Data Types

type Request decodesTo

An internal request as it's built up. Once it's built up, send the request with Graphql.Http.send.

type Error parsedData
= GraphqlError
(PossiblyParsedData parsedData)
(List GraphqlError)
| HttpError Error

Represents the two types of errors you can get, an Http error or a GraphQL error. See the Graphql.Http.GraphqlError module docs for more details.

Begin Request Pipeline

queryRequest : String -> SelectionSet decodesTo RootQuery -> Request decodesTo

Initialize a basic request from a Query. You can add on options with withHeader, withTimeout, withCredentials, and send it with Graphql.Http.send.

mutationRequest : String -> SelectionSet decodesTo RootMutation -> Request decodesTo

Initialize a basic request from a Mutation. You can add on options with withHeader, withTimeout, withCredentials, and send it with Graphql.Http.send.

Exactly like queryRequest, but with an option to use the HTTP GET request method. You will probably want to use GetIfShortEnough, which uses GET if the full URL ends up being under 2000 characters, or POST otherwise, since some browsers don't support URLs over a certain length. GetIfShortEnough will typically do what you need. If you must use GET no matter what when hitting your endpoint, you can use AlwaysGet.

queryRequest always uses POST since some GraphQL API's don't support GET requests (for example, the Github API assumes that you are doing an introspection query if you make a GET request). But for semantic reasons, GET requests are sometimes useful for sending GraphQL Query requests. That is, a GraphQL Query does not perform side-effects on the server like a Mutation does, so a GET indicates this and allows some servers to cache requests. See this github thread from the Apollo project for more details.

= AlwaysGet
| GetIfShortEnough

Union type to pass in to queryRequestWithHttpGet. Only applies to queries. Mutations don't accept this configuration option and will always use POST.

Configure Request Options

withHeader : String -> String -> Request decodesTo -> Request decodesTo

Add a header.

makeRequest : Cmd Msg
makeRequest =
    query
        |> Graphql.Http.queryRequest "https://api.github.com/graphql"
        |> Graphql.Http.withHeader "authorization" "Bearer <my token>"
        |> Graphql.Http.send (RemoteData.fromResult >> GotResponse)
withTimeout : Float -> Request decodesTo -> Request decodesTo

Add a timeout.

withCredentials : Request decodesTo -> Request decodesTo

Set with credentials to true.

withQueryParams : List ( String, String ) -> Request decodesTo -> Request decodesTo

Add query params. The values will be Uri encoded.

makeRequest : Cmd Msg
makeRequest =
    query
        |> Graphql.Http.queryRequest "https://api.github.com/graphql"
        |> Graphql.Http.withQueryParams [ ( "version", "1.2.3" ) ]
        |> Graphql.Http.send (RemoteData.fromResult >> GotResponse)

Perform Request

(Result (Error decodesTo) decodesTo -> msg)
-> Request decodesTo
-> Cmd msg

Send the Graphql.Request You can use it on its own, or with a library like RemoteData.

import Graphql.Http
import Graphql.OptionalArgument exposing (OptionalArgument(..))
import RemoteData exposing (RemoteData)


type Msg
    = GotResponse RemoteData (Graphql.Http.Error Response) Response


makeRequest : Cmd Msg
makeRequest =
    query
        |> Graphql.Http.queryRequest "https://elm-graphql.herokuapp.com/"
        |> Graphql.Http.withHeader "authorization" "Bearer abcdefgh12345678"
        -- If you're not using remote data, it's just
        -- |> Graphql.Http.send GotResponse
        -- With remote data, it's as below
        |> Graphql.Http.send (RemoteData.fromResult >> GotResponse)

If any errors are present, you will get a GraphqlError that includes the details. GraphQL allows for partial data to be returned in the case of errors so you can inspect the data returned in the GraphqlError if you would like to try to recover any data that made it through in the response.

toTask : Request decodesTo -> Task (Error decodesTo) decodesTo

Convert a Request to a Task. See Graphql.Http.send for an example of how to build up a Request.

Map Errors

mapError : (a -> b) -> Error a -> Error b

Map the error data if it is ParsedData.

ignoreParsedErrorData : Error parsedData -> Error ()

Useful when you don't want to deal with the recovered data if there is ParsedData. Just a shorthand for mapError that will turn any ParsedData into ().

fromHttpError : Error -> Error ()

Turn an Http.Error into a Graphql.Http.Error.