Skip to content

Instantly share code, notes, and snippets.

@happysalada
Created April 25, 2017 22:00
Show Gist options
  • Save happysalada/650ad60adea72f70f469afd00405391d to your computer and use it in GitHub Desktop.
Save happysalada/650ad60adea72f70f469afd00405391d to your computer and use it in GitHub Desktop.
module Main exposing (..)
import Html exposing (Html, text, div, ul, li)
import Http
import Json.Decode as Decode
import Json.Decode.Pipeline as Pipeline
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = \_ -> Sub.none
}
type alias Model =
{ issues : List Issue
, message : String
}
type alias Issue =
{ id : Int
, title : String
}
type Msg
= FetchGithubIssues (Result Http.Error (List Issue))
githubRepo : String
githubRepo =
"""
{
github {
repo(ownerUsername: "nodejs", name: "node") {
id
name
issues(limit: 1) {
id
state
title
}
}
}
}
"""
issueDecoder : Decode.Decoder Issue
issueDecoder =
Pipeline.decode Issue
|> Pipeline.required "id" Decode.int
|> Pipeline.required "title" Decode.string
request : Http.Request (List Issue)
request =
let
encoded =
Http.encodeUri githubRepo
decoder =
Decode.at [ "data", "github", "repo", "issues" ] <|
Decode.list issueDecoder
in
Http.get ("https://www.graphqlhub.com/graphql?query=" ++ encoded) decoder
init : ( Model, Cmd Msg )
init =
{ issues = []
, message = "Waiting for a response... "
}
! [ Http.send FetchGithubIssues request ]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
FetchGithubIssues (Ok issues) ->
{ model | issues = issues, message = "Got issues!" } ! []
FetchGithubIssues (Err res) ->
{ model | message = toString res } ! []
listItem : String -> Html Msg
listItem str =
li [] [ text str ]
view : Model -> Html Msg
view model =
let
items =
List.map (listItem << .title) model.issues
issuesList =
ul [] items
in
div [] [ text model.message, issuesList ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment