Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 9e08a82

Browse files
Replace Debug.toString in example
1 parent 1bd2f2f commit 9e08a82

File tree

1 file changed

+76
-40
lines changed

1 file changed

+76
-40
lines changed

example/Main.elm

Lines changed: 76 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
module Main exposing (..)
22

3-
import Html exposing (Html, div, text)
3+
import Browser
4+
import GraphQL.Client.Http as GraphQLClient
45
import GraphQL.Request.Builder exposing (..)
56
import GraphQL.Request.Builder.Arg as Arg
67
import GraphQL.Request.Builder.Variable as Var
7-
import GraphQL.Client.Http as GraphQLClient
8-
import Task exposing (Task)
9-
import Browser
8+
import Html exposing (Html, div, text)
109
import Http
10+
import Task exposing (Task)
11+
1112

1213
{-| Responses to `starWarsRequest` are decoded into this type.
1314
-}
@@ -22,31 +23,32 @@ type alias FilmSummary =
2223
will later be encoded into the following GraphQL query document:
2324
2425
fragment filmPlanetsFragment on Film {
25-
planetConnection(first: $pageSize) {
26-
edges {
27-
node {
28-
name
29-
}
30-
}
31-
}
26+
planetConnection(first: $pageSize) {
27+
edges {
28+
node {
29+
name
30+
}
31+
}
32+
}
3233
}
3334
3435
query ($filmID: ID!, $pageSize: Int = 3) {
35-
film(filmID: $filmID) {
36-
title
37-
characterConnection(first: $pageSize) {
38-
edges {
39-
node {
40-
name
41-
}
42-
}
43-
}
44-
...filmPlanetsFragment
45-
}
36+
film(filmID: $filmID) {
37+
title
38+
characterConnection(first: $pageSize) {
39+
edges {
40+
node {
41+
name
42+
}
43+
}
44+
}
45+
...filmPlanetsFragment
46+
}
4647
}
4748
4849
This query is sent along with variable values extracted from the record passed
4950
to `request`, and the response is decoded into a `FilmSummary`.
51+
5052
-}
5153
starWarsRequest : Request Query FilmSummary
5254
starWarsRequest =
@@ -67,24 +69,24 @@ starWarsRequest =
6769
)
6870
)
6971
in
70-
extract
71-
(field "film"
72-
[ ( "filmID", Arg.variable filmID ) ]
73-
(object FilmSummary
74-
|> with (field "title" [] (nullable string))
75-
|> with
76-
(field "characterConnection"
77-
[ ( "first", Arg.variable pageSize ) ]
78-
(connectionNodes (extract (field "name" [] (nullable string))))
79-
)
80-
|> with (fragmentSpread planetsFragment)
81-
)
72+
extract
73+
(field "film"
74+
[ ( "filmID", Arg.variable filmID ) ]
75+
(object FilmSummary
76+
|> with (field "title" [] (nullable string))
77+
|> with
78+
(field "characterConnection"
79+
[ ( "first", Arg.variable pageSize ) ]
80+
(connectionNodes (extract (field "name" [] (nullable string))))
81+
)
82+
|> with (fragmentSpread planetsFragment)
8283
)
83-
|> queryDocument
84-
|> request
85-
{ filmID = "1"
86-
, pageSize = Nothing
87-
}
84+
)
85+
|> queryDocument
86+
|> request
87+
{ filmID = "1"
88+
, pageSize = Nothing
89+
}
8890

8991

9092
{-| A function that helps you extract node objects from paginated Relay connections.
@@ -150,10 +152,44 @@ init () =
150152
view : Model -> Browser.Document Msg
151153
view model =
152154
{ title = "Example"
153-
, body = [ model |> Debug.toString |> text ]
155+
, body =
156+
[ Maybe.map viewFilmSummary model |> Maybe.withDefault (Html.text "Nothing") ]
154157
}
155158

156159

160+
viewFilmSummary : FilmSummary -> Html Msg
161+
viewFilmSummary summary =
162+
Html.div []
163+
[ Html.text ("Title: " ++ Maybe.withDefault "Unknown" summary.title)
164+
, viewCharacterNames summary.someCharacterNames
165+
, viewPlanetNames <| Maybe.withDefault [] summary.somePlanetNames
166+
]
167+
168+
169+
viewCharacterNames : List (Maybe String) -> Html Msg
170+
viewCharacterNames names =
171+
Html.div []
172+
[ Html.text "Character names: "
173+
, viewNameList names
174+
]
175+
176+
177+
viewPlanetNames : List (Maybe String) -> Html Msg
178+
viewPlanetNames names =
179+
Html.div []
180+
[ Html.text "Planet names: "
181+
, viewNameList names
182+
]
183+
184+
185+
viewNameList : List (Maybe String) -> Html Msg
186+
viewNameList names =
187+
names
188+
|> List.map (Maybe.withDefault " -- ")
189+
|> String.join ", "
190+
|> Html.text
191+
192+
157193
update : Msg -> Model -> ( Model, Cmd Msg )
158194
update msg model =
159195
case msg of

0 commit comments

Comments
 (0)