Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.
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"
}
}
}
178 changes: 129 additions & 49 deletions example/Main.elm
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module Main exposing (..)

import Html exposing (Html, div, text)
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 GraphQL.Client.Http as GraphQLClient
import GraphQL.Response
import Html exposing (Html, div, text)
import Http
import Task exposing (Task)


Expand All @@ -19,7 +22,6 @@ type alias FilmSummary =

{-| The definition of `starWarsRequest` builds up a query request value that
will later be encoded into the following GraphQL query document:

fragment filmPlanetsFragment on Film {
planetConnection(first: $pageSize) {
edges {
Expand All @@ -29,7 +31,6 @@ fragment filmPlanetsFragment on Film {
}
}
}

query ($filmID: ID!, $pageSize: Int = 3) {
film(filmID: $filmID) {
title
Expand All @@ -43,7 +44,6 @@ query ($filmID: ID!, $pageSize: Int = 3) {
...filmPlanetsFragment
}
}

This query is sent along with variable values extracted from the record passed
to `request`, and the response is decoded into a `FilmSummary`.
-}
Expand All @@ -66,24 +66,24 @@ starWarsRequest =
)
)
in
extract
(field "film"
[ ( "filmID", Arg.variable filmID ) ]
(object FilmSummary
|> with (field "title" [] (nullable string))
|> with
(field "characterConnection"
[ ( "first", Arg.variable pageSize ) ]
(connectionNodes (extract (field "name" [] (nullable string))))
)
|> with (fragmentSpread planetsFragment)
)
extract
(field "film"
[ ( "filmID", Arg.variable filmID ) ]
(object FilmSummary
|> with (field "title" [] (nullable string))
|> with
(field "characterConnection"
[ ( "first", Arg.variable pageSize ) ]
(connectionNodes (extract (field "name" [] (nullable string))))
)
|> with (fragmentSpread planetsFragment)
)
|> queryDocument
|> request
{ filmID = "1"
, pageSize = Nothing
}
)
|> queryDocument
|> request
{ filmID = "1"
, pageSize = Nothing
}


{-| A function that helps you extract node objects from paginated Relay connections.
Expand All @@ -103,55 +103,135 @@ connectionNodes spec =
)


type alias StarWarsResponse =
Result GraphQLClient.Error FilmSummary
type Model
= Resp FilmSummary
| Errors String
| Loading


type alias Model =
Maybe StarWarsResponse
type Msg
= QueryResponse FilmSummary
| GraphQLErrors (List GraphQL.Response.RequestError)
| HttpError Http.Error


type Msg
= ReceiveQueryResponse StarWarsResponse
graphQLToMsg : GraphQLClient.Result FilmSummary -> Msg
graphQLToMsg result =
case result of
GraphQLClient.Success data ->
QueryResponse data

GraphQLClient.SuccessWithErrors _ data ->
QueryResponse data

sendQueryRequest : Request Query a -> Task GraphQLClient.Error a
sendQueryRequest request =
GraphQLClient.sendQuery "/" request
GraphQLClient.DecoderError err _ ->
GraphQLErrors err

GraphQLClient.HttpError err ->
HttpError err


sendStarWarsQuery : Cmd Msg
sendStarWarsQuery =
sendQueryRequest starWarsRequest
|> Task.attempt ReceiveQueryResponse
sendQueryRequest : Request Query FilmSummary -> Cmd Msg
sendQueryRequest request =
GraphQLClient.sendQuery "/" graphQLToMsg request


main : Program Never Model Msg
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 () =
( Loading, sendQueryRequest starWarsRequest )


view : Model -> Html Msg
view : Model -> Browser.Document Msg
view model =
div []
[ model |> toString |> text ]
{ title = "Example"
, body =
[ viewModel model ]
}


viewModel : Model -> Html Msg
viewModel model =
case model of
Loading ->
Html.text "Loading..."

Errors e ->
Html.text ("Oh no! I got this error " ++ e)

Resp r ->
viewFilmSummary r


viewFilmSummary : FilmSummary -> Html Msg
viewFilmSummary summary =
Html.div []
[ Html.text ("Title: " ++ Maybe.withDefault "Unknown" summary.title)
, viewCharacterNames summary.someCharacterNames
, viewPlanetNames <| Maybe.withDefault [] summary.somePlanetNames
]


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


httpErrorToString : Http.Error -> String
httpErrorToString error =
case error of
Http.BadUrl err ->
"Bad url: " ++ err

Http.Timeout ->
"Timeout"

Http.NetworkError ->
"Network error"

Http.BadStatus code ->
"Bad status code: " ++ String.fromInt code

Http.BadBody err ->
"Bad body: " ++ err


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

GraphQLErrors gqlErrors ->
( gqlErrors |> List.map .message |> String.join ", " |> Errors, Cmd.none )

subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
HttpError err ->
( err |> httpErrorToString |> Errors, 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