-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathExampleFromReadme.elm
78 lines (52 loc) · 1.51 KB
/
ExampleFromReadme.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
module ExampleFromReadme exposing (main)
import CustomScalarCodecs exposing (Id(..))
import Graphql.Document as Document
import Graphql.Http
import Graphql.Operation exposing (RootQuery)
import Graphql.SelectionSet as SelectionSet exposing (SelectionSet)
import Helpers.Main
import RemoteData exposing (RemoteData)
import Swapi.Object
import Swapi.Object.Human as Human
import Swapi.Query as Query
type alias Response =
Maybe Human
type alias Human =
{ name : String
, homePlanet : Maybe String
}
query : SelectionSet (Maybe Human) RootQuery
query =
Query.human { id = Id 1001 } humanSelection
humanSelection : SelectionSet Human Swapi.Object.Human
humanSelection =
SelectionSet.map2 Human
Human.name
Human.homePlanet
makeRequest : Cmd Msg
makeRequest =
query
|> Graphql.Http.queryRequest "https://elm-graphql.onrender.com"
|> Graphql.Http.send (RemoteData.fromResult >> GotResponse)
-- Elm Architecture Setup
type Msg
= GotResponse Model
type alias Model =
RemoteData (Graphql.Http.Error Response) Response
type alias Flags =
()
init : Flags -> ( Model, Cmd Msg )
init _ =
( RemoteData.Loading, makeRequest )
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
GotResponse response ->
( response, Cmd.none )
main : Helpers.Main.Program Flags Model Msg
main =
Helpers.Main.document
{ init = init
, update = update
, queryString = Document.serializeQuery query
}