This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
An ELM library for GraphQL API
version 1.0.0
license MPL-2.0
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 1.0.0
Committed At 2018-02-25 19:54:07 UTC
elm-lang/http 1.0.0 <= v < 2.0.0 1.0.0
elm-lang/core 5.1.1 <= v < 6.0.0 5.1.1
NoRedInk/elm-decode-pipeline 3.0.0 <= v < 4.0.0 3.0.1

Modules

README

elm-graphql

A ELM library to use the GraphQL API.

To install the library:

> elm-package install gribouille/elm-graphql

Example

  1. Configure the API. In this example, the API is protected with a JSON web token in the authorization header:
config : String -> GraphQL.Config
config token =
  GraphQL.Config [ ("authorization", "Bearer " ++ token) ] "/api/graphql"
  1. Create a GraphQL request with cmd and exec:
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 }
  }
  """
  1. Get the results in the update function:
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

Documentation

The API documentation is available here.

Usage

To run the examples:

$ cd test
$ npm install
$ npm run server
$ npm run dev

Open localhost:8080.

Contributing

Feedback and contributions are very welcome.

License

This project is licensed under Mozilla Public License Version 2.0.