-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathElmReposRequest.elm
160 lines (126 loc) · 4.61 KB
/
ElmReposRequest.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 ElmReposRequest exposing (Repo, SortOrder(..), query, queryForRepos)
import Github.Enum.IssueState
import Github.Enum.SearchType
import Github.Interface
import Github.Interface.RepositoryOwner
import Github.Object
import Github.Object.IssueConnection
import Github.Object.Repository as Repository
import Github.Object.SearchResultItemConnection
import Github.Object.StargazerConnection
import Github.Query as Query
import Github.Scalar
import Github.Union
import Github.Union.SearchResultItem
import Graphql.Operation exposing (RootQuery)
import Graphql.OptionalArgument exposing (OptionalArgument(..))
import Graphql.SelectionSet as SelectionSet exposing (SelectionSet, with)
import Iso8601
import RepoWithOwner exposing (RepoWithOwner)
import Time exposing (Posix)
type alias Repo =
{ nameWithOwner : RepoWithOwner
, description : Maybe String
, stargazerCount : Int
, timestamps : Timestamps
, forkCount : Int
, issues : Int
, owner : Owner
, url : Github.Scalar.Uri
}
type SortOrder
= Forks
| Stars
| Updated
query : SortOrder -> SelectionSet (List Repo) RootQuery
query sortOrder =
Query.search (\optionals -> { optionals | first = Present 100 })
{ query = "language:Elm sort:" ++ (sortOrder |> Debug.toString |> String.toLower)
, type_ = Github.Enum.SearchType.Repository
}
thing
queryForRepos : List RepoWithOwner -> SelectionSet (List Repo) RootQuery
queryForRepos reposWithOwner =
reposWithOwner
|> List.map repoWithOwnerSelection
|> grouped
|> SelectionSet.map (List.filterMap identity)
grouped :
List (SelectionSet decodesTo1 scope)
-> SelectionSet (List decodesTo1) scope
grouped selections =
List.foldl (SelectionSet.map2 (::))
(SelectionSet.empty |> SelectionSet.map (\_ -> []))
selections
repoWithOwnerSelection : RepoWithOwner -> SelectionSet (Maybe Repo) RootQuery
repoWithOwnerSelection repoWithOwner =
let
{ owner, repoName } =
RepoWithOwner.ownerAndRepo repoWithOwner
in
Query.repository
{ owner = owner, name = repoName }
repositorySelection
thing : SelectionSet (List Repo) Github.Object.SearchResultItemConnection
thing =
Github.Object.SearchResultItemConnection.nodes searchResultSelection
|> SelectionSet.nonNullOrFail
|> SelectionSet.map (List.filterMap identity)
|> SelectionSet.map (List.filterMap identity)
searchResultSelection : SelectionSet (Maybe Repo) Github.Union.SearchResultItem
searchResultSelection =
let
maybeFragments =
Github.Union.SearchResultItem.maybeFragments
partialFragments =
{ maybeFragments
| onRepository = repositorySelection |> SelectionSet.map Just
}
in
Github.Union.SearchResultItem.fragments partialFragments
repositorySelection : SelectionSet Repo Github.Object.Repository
repositorySelection =
SelectionSet.succeed Repo
|> with (Repository.nameWithOwner |> SelectionSet.map RepoWithOwner.repoWithOwner)
|> with Repository.description
|> with stargazers
|> with timestampsSelection
|> with Repository.forkCount
|> with openIssues
|> with (Repository.owner ownerSelection)
|> with Repository.url
type alias Timestamps =
{ created : Posix
, updated : Posix
}
timestampsSelection : SelectionSet Timestamps Github.Object.Repository
timestampsSelection =
SelectionSet.map2 Timestamps
(Repository.createdAt |> mapToDateTime)
(Repository.updatedAt |> mapToDateTime)
mapToDateTime : SelectionSet Github.Scalar.DateTime scope -> SelectionSet Posix scope
mapToDateTime =
SelectionSet.mapOrFail
(\(Github.Scalar.DateTime value) ->
Iso8601.toTime value
|> Result.mapError (\_ -> "Failed to parse " ++ value ++ " as Iso8601 DateTime.")
)
stargazers : SelectionSet Int Github.Object.Repository
stargazers =
Repository.stargazers
(\optionals -> { optionals | first = Present 0 })
Github.Object.StargazerConnection.totalCount
openIssues : SelectionSet Int Github.Object.Repository
openIssues =
Repository.issues
(\optionals -> { optionals | first = Present 0, states = Present [ Github.Enum.IssueState.Open ] })
Github.Object.IssueConnection.totalCount
type alias Owner =
{ avatarUrl : Github.Scalar.Uri
, login : String
}
ownerSelection : SelectionSet Owner Github.Interface.RepositoryOwner
ownerSelection =
SelectionSet.map2 Owner
(Github.Interface.RepositoryOwner.avatarUrl identity)
Github.Interface.RepositoryOwner.login