owanturist/elm-graphql/1.0.1

NOTE: this package is not compatible with Elm 0.19.1

GraphQL

Building and sending GraphQL.

Building of HTTP request has been based on elm-http-builder.

Build a GraphQL

type GraphQL a
query : String -> Selector a -> GraphQL a

Build a GraphQL named query.

GraphQL.Selector.succeed identity
    |> GraphQL.Selector.field "me"
        []
        (GraphQL.Selector.succeed User
            |> GraphQL.Selector.field "id" [] GraphQL.Selector.string
            |> GraphQL.Selector.field "firstName" [] GraphQL.Selector.string
            |> GraphQL.Selector.field "lastName" [] GraphQL.Selector.string
        )
    |> query "Me"

It collects a GraphQL which is equal to:

"""
query Me {
    me {
        id
        firstName
        lastName
    }
}
"""
mutation : String -> Selector a -> GraphQL a

Build a GraphQL named mutation.

GraphQL.Selector.succeed identity
    |> GraphQL.Selector.field "updateMe"
        [ ( "firstName", GraphQL.Argument.string "Tom" )
        ]
        (GraphQL.Selector.succeed User
            |> GraphQL.Selector.field "id" [] GraphQL.Selector.string
            |> GraphQL.Selector.field "firstName" [] GraphQL.Selector.string
            |> GraphQL.Selector.field "lastName" [] GraphQL.Selector.string
        )
    |> mutation "RenameUser"

It collects a GraphQL which is equal to:

"""
mutation RenameUser {
    updateMe("firstName": "Tom") {
        id
        firstName
        lastName
    }
}
"""
subscription : String -> Selector a -> GraphQL a

Build a GraphQL named subscription.

GraphQL.Selector.succeed identity
    |> GraphQL.Selector.field "onUpdateMe"
        []
        (GraphQL.Selector.succeed User
            |> GraphQL.Selector.field "id" [] GraphQL.Selector.string
            |> GraphQL.Selector.field "firstName" [] GraphQL.Selector.string
            |> GraphQL.Selector.field "lastName" [] GraphQL.Selector.string
        )
    |> subscription "OnUpdateUser"

It collects a GraphQL which is equal to:

"""
subscription OnUpdateUser {
    onUpdateMe {
        id
        firstName
        lastName
    }
}
"""
select : GraphQL a -> ( Maybe String, Decoder a )

Render GraphQL representation and build a Decoder from a GraphQL.

Build a Request

type alias Request a =
{ method : String
, url : String
, headers : List Header
, body : Body
, expect : Expect a
, timeout : Maybe Time
, withCredentials : Bool
, queryParams : List ( String, String )
, cacheBuster : Maybe String
}

A type for chaining request configuration.

get : String -> GraphQL a -> Request a

Start building a GET request with a given URL.

GraphQL.Selector.succeed (,)
    |> GraphQL.Selector.field "me" [] userSelector
    |> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
    |> query "InitialData"
    |> get "https://example.com/graphql"
post : String -> GraphQL a -> Request a

Start building a POST request with a given URL.

GraphQL.Selector.succeed (,)
    |> GraphQL.Selector.field "me" [] userSelector
    |> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
    |> query "InitialData"
    |> get "https://example.com/graphql"
withHeader : String -> String -> Request a -> Request a

Add a single header to a request.

GraphQL.Selector.succeed (,)
    |> GraphQL.Selector.field "me" [] userSelector
    |> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
    |> query "InitialData"
    |> get "https://example.com/graphql"
    |> withHeader "Content-Type" "application/json"
    |> withHeader "Accept" "application/json"
withHeaders : List ( String, String ) -> Request a -> Request a

Add many headers to a request.

GraphQL.Selector.succeed (,)
    |> GraphQL.Selector.field "me" [] userSelector
    |> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
    |> query "InitialData"
    |> get "https://example.com/graphql"
    |> withHeaders
        [ ( "Content-Type", "application/json" )
        , ( "Accept", "application/json" )
        ]
withBearerToken : String -> Request a -> Request a

Add a bearer token to a request.

GraphQL.Selector.succeed (,)
    |> GraphQL.Selector.field "me" [] userSelector
    |> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
    |> query "InitialData"
    |> get "https://example.com/graphql"
    |> withBearerToken "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYSJ9.MvhYYpYBuN1rUaV0GGnQGvr889zY0xSc20Lnt8nMTfE"
withQueryParam : String -> String -> Request a -> Request a

Add a query param to the url for the request.

GraphQL.Selector.succeed (,)
    |> GraphQL.Selector.field "me" [] userSelector
    |> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
    |> query "InitialData"
    |> get "https://example.com/graphql"
    |> withQueryParams "hello" "world"
    |> withQueryParams baz" "qux"


-- sends a request to https://example.com/graphql?hello=world&baz=qux
withQueryParams : List ( String, String ) -> Request a -> Request a

Add some query params to the url for the request.

GraphQL.Selector.succeed (,)
    |> GraphQL.Selector.field "me" [] userSelector
    |> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
    |> query "InitialData"
    |> get "https://example.com/graphql"
    |> withQueryParams [ ( "hello", "world" ), ( "foo", "bar" ) ]
    |> withQueryParams [ ( "baz", "qux" ) ]


-- sends a request to https://example.com/graphql?hello=world&foo=bar&baz=qux
withTimeout : Time -> Request a -> Request a

Set the timeout setting on the request.

GraphQL.Selector.succeed (,)
    |> GraphQL.Selector.field "me" [] userSelector
    |> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
    |> query "InitialData"
    |> get "https://example.com/graphql"
    |> withTimeout (10 * Time.second)
withCredentials : Bool -> Request a -> Request a

Set the withCredentials flag on the request to True. Works via XMLHttpRequest#withCredentials.

GraphQL.Selector.succeed (,)
    |> GraphQL.Selector.field "me" [] userSelector
    |> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
    |> query "InitialData"
    |> get "https://example.com/graphql"
    |> withCredentials True
withCacheBuster : String -> Request a -> Request a

Send the request with a Time based cache buster added to the URL. You provide a key for an extra query param, and when the request is sent that query param will be given a value with the current timestamp.

type Msg
    = InitialData (Result Http.Error (User, List Article))


GraphQL.Selector.succeed (,)
    |> GraphQL.Selector.field "me" [] userSelector
    |> GraphQL.Selector.field "articles" [] (GraphQL.Selector.list articleSelector)
    |> query "InitialData"
    |> get "https://example.com/graphql"
    |> withCacheBuster "cache_buster"
    |> send InitialData


-- makes a request to https://example.com/graphql?cache_buster=1481633217383

Make a Request

type alias Error =
Error

A Request can fail in a couple ways:

  • BadUrl means you did not provide a valid URL.
  • Timeout means it took too long to get a response.
  • NetworkError means the user turned off their wifi, went in a cave, etc.
  • BadStatus means you got a response back, but the status code indicates failure.
  • BadPayload means you got a response back with a nice status code, but the body of the response was something unexpected. The String in this case is a debugging message that explains what went wrong with your JSON decoder or whatever.
toHttpRequest : Request a -> Request a

Extract the Http.Request component of the builder in case you want to use it directly. This function is lossy and will discard some of the extra stuff that HttpBuilder allows you to do.

Things that will be lost:

  • Attaching a cache buster to requests using withCacheBuster
toTask : Request a -> Task Error a

Convert the Request to a Task with all options applied. toTask differs from toRequest in that it retains all extra behavior allowed by HttpBuilder, including:

  • Attaching a cache buster to requests using withCacheBuster
send : (Result Error a -> msg) -> Request a -> Cmd msg

Send the request.