Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to elm http2 #43

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
"elm/core": "1.0.0 <= v < 2.0.0",
"elm/http": "1.0.0 <= v < 2.0.0",
"elm/http": "2.0.0 <= v < 3.0.0",
"elm/json": "1.0.0 <= v < 2.0.0",
"elm/url": "1.0.0 <= v < 2.0.0"
},
"test-dependencies": {
"elm-explorations/test": "1.0.0 <= v < 2.0.0"
}
}
}
101 changes: 70 additions & 31 deletions example/Main.elm
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Main exposing (..)

import Browser
import GraphQL.Client.Http as GraphQLClient
import GraphQL.Request.Builder exposing (..)
import GraphQL.Request.Builder.Arg as Arg
import GraphQL.Request.Builder.Variable as Var
import Html exposing (Html, div, text)
import Task exposing (Task)
import Html exposing (Html)


{-| Responses to `starWarsRequest` are decoded into this type.
Expand All @@ -30,7 +30,6 @@ fragment filmPlanetsFragment on Film {
}
}
}

query ($filmID: ID!, $pageSize: Int = 3) {
film(filmID: $filmID) {
title
Expand Down Expand Up @@ -106,55 +105,95 @@ connectionNodes spec =
)


type alias StarWarsResponse =
Result GraphQLClient.Error FilmSummary


type alias Model =
Maybe StarWarsResponse
Maybe FilmSummary


type Msg
= ReceiveQueryResponse StarWarsResponse
= ReceiveQueryResponse FilmSummary
| ReceiveQueryError


sendQueryRequest : Request Query a -> Task GraphQLClient.Error a
sendQueryRequest request =
GraphQLClient.sendQuery "/" request
graphQLToMsg : GraphQLClient.Result FilmSummary -> Msg
graphQLToMsg result =
case result of
GraphQLClient.GraphQLSucces data ->
ReceiveQueryResponse data

-- Explicitly ignoring GraphQL data
GraphQLClient.GraphQLErrors _ _ ->
ReceiveQueryError

sendStarWarsQuery : Cmd Msg
sendStarWarsQuery =
sendQueryRequest starWarsRequest
|> Task.attempt ReceiveQueryResponse
GraphQLClient.HttpError _ ->
ReceiveQueryError


main : Program Never Model Msg
sendQueryRequest : Request Query FilmSummary -> Cmd Msg
sendQueryRequest request =
GraphQLClient.sendQuery "/" graphQLToMsg request


main : Program () Model Msg
main =
Html.program
Browser.document
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
, subscriptions = \_ -> Sub.none
}


init : ( Model, Cmd Msg )
init =
( Nothing, sendStarWarsQuery )
init : () -> ( Model, Cmd Msg )
init () =
( Nothing, sendQueryRequest starWarsRequest )


view : Model -> Html Msg
view : Model -> Browser.Document Msg
view model =
div []
[ model |> toString |> text ]
{ title = "Example"
, body =
[ Maybe.map viewFilmSummary model |> Maybe.withDefault (Html.text "Nothing") ]
}


update : Msg -> Model -> ( Model, Cmd Msg )
update (ReceiveQueryResponse response) model =
( Just response, Cmd.none )
viewFilmSummary : FilmSummary -> Html Msg
viewFilmSummary summary =
Html.div []
[ Html.text ("Title: " ++ Maybe.withDefault "Unknown" summary.title)
, viewCharacterNames summary.someCharacterNames
, viewPlanetNames <| Maybe.withDefault [] summary.somePlanetNames
]


subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
viewCharacterNames : List (Maybe String) -> Html Msg
viewCharacterNames names =
Html.div []
[ Html.text "Character names: "
, viewNameList names
]


viewPlanetNames : List (Maybe String) -> Html Msg
viewPlanetNames names =
Html.div []
[ Html.text "Planet names: "
, viewNameList names
]


viewNameList : List (Maybe String) -> Html Msg
viewNameList names =
names
|> List.map (Maybe.withDefault " -- ")
|> String.join ", "
|> Html.text


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
ReceiveQueryResponse data ->
( Just data, Cmd.none )

ReceiveQueryError ->
( Nothing, Cmd.none )
17 changes: 0 additions & 17 deletions example/elm-package.json

This file was deleted.

28 changes: 28 additions & 0 deletions example/elm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"type": "application",
"source-directories": [
".",
"../src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"elm/http": "1.0.0",
"elm/json": "1.1.3",
"elm/url": "1.0.0"
},
"indirect": {
"elm/bytes": "1.0.8",
"elm/file": "1.0.5",
"elm/time": "1.0.0",
"elm/virtual-dom": "1.0.2"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}
Loading