gribouille/elm-graphql/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

GraphQL

Library to use GraphQL API easily.

Example to configure the API with a token in the authorization header:

config : String -> GraphQL.Config config token = GraphQL.Config [ ("authorization", token) ] "/api/graphql"

Example to create a request:

getUsers : String -> (GraphQL.Purpose Users -> msg) -> Cmd msg getUsers token resultCallback = GraphQL.cmd (myRequest token) resultCallback

myRequest : String -> Http.Request (GraphQL.Response Users) myRequest token = GraphQL.exec (config token) "users" |> GraphQL.query myQuery |> GraphQL.variabes Encode.null |> GraphQL.decoder User.usersDecoder

myQuery : String myQuery = """ query { users { id login firstname lastname email } } """

Example to get the results:

type Msg = GetUsers -- msg to execute the request | OnGetUsers (GraphQL.Purpose Users) -- msg to receive the data ...

update : Msg -> Model -> (Model, Cmd Msg) update msg model = case msg of GetUsers -> getUsers "myToken" OnGetUsers OnGetUsers res -> GraphQL.response model res (\users -> { model | ... } ! [ ... ] ) -- if success (\error -> { model | ... } ! [ ... ] ) -- if error

Types

type alias Config =
{ headers : List ( String, String )
, url : String
}

Configure the headers and the url to use the GraphQL API.

Example for an API with JWT token:

config : String -> GraphQL.Config config token = GraphQL.Config [ ("authorization", "Bearer " ++ token) ] "/api/graphql"

type alias Purpose a =
Result Error (Result String a)

Full API response.

There are two types of errors with the GraphQL:

  • the GraphQL error (see Response type)
  • the HTTP error.

Purpose is an abstraction for these 2 types of errors.

type alias Response a =
GraphQl (Maybe a)

GraphQL response:

{
  data: { <query>: <result>|null },
  errors: [
    { message: ... }, ...
  ]
}

If there is a GraphQL error:

  • the status code is 200
  • the data property is null
  • the errors array is not empty.

Functions

exec : Config -> String -> String -> Value -> Decoder a -> Request (Response a)

Create a GraphQL request.

Example:

myRequest : String -> Http.Request (GraphQL.Response Users) myRequest token = GraphQL.exec (config token) "users" |> GraphQL.query myQuery |> GraphQL.variabes Encode.null |> GraphQL.decoder User.usersDecoder

String
-> (String -> Value -> Decoder a -> Request (Response a))
-> Value
-> Decoder a
-> Request (Response a)

Helper to set the query for the GraphQL request in exec.

cmd : Request (Response a) -> (Purpose a -> msg) -> Cmd msg

Create a Cmd with the GraphQL request create with exec.

Example

getUsers : String -> (GraphQL.Purpose Users -> msg) -> Cmd msg getUsers token resultCallback = GraphQL.cmd (myRequest token) resultCallback

Value
-> (Value -> Decoder a -> Request (Response a))
-> Decoder a
-> Request (Response a)

Helper to set the query variables for the GraphQL request in exec.

Decoder a
-> (Decoder a -> Request (Response a))
-> Request (Response a)

Helper to set the JSON decoder for the GraphQL request response in exec.

model
-> Purpose data
-> (data -> ( model, Cmd msg ))
-> (String -> ( model, Cmd msg ))
-> ( model, Cmd msg )

Helper to parse the GraphQL response.

Example: update : Msg -> Model -> (Model, Cmd Msg) update msg model = case msg of ... OnGetUsers res -> GraphQL.response model res (\users -> { model | users = users, success = Just "get all users" } ! [] ) (\error -> { model | error = Just error } ! [] )