This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GraphRoot.sml
97 lines (83 loc) · 2.24 KB
/
GraphRoot.sml
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
fragment realworld {
"std" 1.0
}
GraphRoot = resolver {
# user resolves a user by the given username
user (username Username) -> ?UserResolver => {
user = entity<User>(predicate: (u) => u.username == username)
& = user as u {
User then UserResolver{user: u}
}
}
# article resolves an article by the given id
article (slug String) -> ?ArticleResolver => {
article = entity<Article>(predicate: (a) => a.id == slug)
& = article as a {
Article then ArticleResolver{article: a}
}
}
# comment resolves an comment by the given id
comment (commentId CommentId) -> ?CommentResolver => {
comment = entity<Comment>(predicate: (a) => a.id == commentId)
& = comment as a {
Comment then CommentResolver{comment: a}
}
}
# articleBy resolves many articles by the given search criteria
articleBy (
tags ?Set<Tag>,
authors ?Set<Username>,
) -> Array<ArticleResolver> => {
matchByTags = (a Article) -> Bool => tags as tags {
Set<Tag> then any(tags, (tag) => tag in a.tags)
else false
}
matchByAuthors = (a Article) -> Bool => authors as authors {
Set<Username> then any(
authors,
(authorUsername) => a.author.username == authorUsername,
)
else false
}
articles = entities<Article>(
predicate: (a) => matchByTags(a) or matchByAuthors(a),
)
& = map(articles, (a) => ArticleResolver{article: a})
}
# feed resolves the articles feed for a particular user identified by
# the given username
feed (username Username) -> (
Array<ArticleResolver> or
ErrUserNotFound or
ErrUnauth
) => {
user = entity<User>(predicate: (u) => u.username == username)
& = {
// Ensure the user exists
user == Nil then ErrUserNotFound
// Ensure the client is the user for which the feed for requested
!isOwner(owner: User from user) then ErrUnauth
else {
feedArticles = map(
(User from user).following,
(followee) => followee.publishedArticles,
)
sorted = std::sortBy(feedArticles, std::Order{
by: Article.createdAt,
order: std::desc,
})
& = map(sorted, (a) => ArticleResolver{article: a})
}
}
}
registration
authentication
userUpdate
newFollower
canceledFollowership
articlePublication
articleDeletion
articleUpdate
commentPublication
commentDeletion
}