-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathMain.elm
160 lines (127 loc) · 3.69 KB
/
Main.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
module Main exposing (..)
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)
{-| Responses to `starWarsRequest` are decoded into this type.
-}
type alias FilmSummary =
{ title : Maybe String
, someCharacterNames : List (Maybe String)
, somePlanetNames : Maybe (List (Maybe String))
}
{-| The definition of `starWarsRequest` builds up a query request value that
will later be encoded into the following GraphQL query document:
```graphql
fragment filmPlanetsFragment on Film {
planetConnection(first: $pageSize) {
edges {
node {
name
}
}
}
}
query ($filmID: ID!, $pageSize: Int = 3) {
film(filmID: $filmID) {
title
characterConnection(first: $pageSize) {
edges {
node {
name
}
}
}
...filmPlanetsFragment
}
}
```
This query is sent along with variable values extracted from the record passed
to `request`, and the response is decoded into a `FilmSummary`.
-}
starWarsRequest : Request Query FilmSummary
starWarsRequest =
let
filmID =
Var.required "filmID" .filmID Var.id
pageSize =
Var.optional "pageSize" .pageSize (Var.nullable Var.int) (Just 3)
planetsFragment =
fragment "filmPlanetsFragment"
(onType "Film")
(extract
(field "planetConnection"
[ ( "first", Arg.variable pageSize ) ]
(connectionNodes (extract (field "name" [] (nullable string))))
)
)
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)
)
)
|> queryDocument
|> request
{ filmID = "1"
, pageSize = Nothing
}
{-| A function that helps you extract node objects from paginated Relay connections.
-}
connectionNodes :
ValueSpec NonNull ObjectType result vars
-> ValueSpec NonNull ObjectType (List result) vars
connectionNodes spec =
extract
(field "edges"
[]
(list
(extract
(field "node" [] spec)
)
)
)
type alias StarWarsResponse =
Result GraphQLClient.Error FilmSummary
type alias Model =
Maybe StarWarsResponse
type Msg
= ReceiveQueryResponse StarWarsResponse
sendQueryRequest : Request Query a -> Task GraphQLClient.Error a
sendQueryRequest request =
GraphQLClient.sendQuery "/" request
sendStarWarsQuery : Cmd Msg
sendStarWarsQuery =
sendQueryRequest starWarsRequest
|> Task.attempt ReceiveQueryResponse
main : Program Never Model Msg
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
init : ( Model, Cmd Msg )
init =
( Nothing, sendStarWarsQuery )
view : Model -> Html Msg
view model =
div []
[ model |> toString |> text ]
update : Msg -> Model -> ( Model, Cmd Msg )
update (ReceiveQueryResponse response) model =
( Just response, Cmd.none )
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none