-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.bal
85 lines (71 loc) · 2.2 KB
/
types.bal
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
import ballerina/graphql;
import sm_backend.datasource as ds;
public distinct isolated service class Post {
private final readonly & ds:PostRecord post;
isolated function init(ds:PostRecord postRecord) {
self.post = postRecord;
}
isolated resource function get id() returns @graphql:ID int {
return self.post.id;
}
@graphql:ResourceConfig {
cacheConfig: {
maxAge: 60
}
}
isolated resource function get title() returns string {
return self.post.title;
}
isolated resource function get content() returns string {
return self.post.content;
}
isolated resource function get author() returns User|error {
ds:UserRecord user = check ds:getUser(self.post.authorId);
return new (user);
}
@graphql:ResourceConfig {
cacheConfig: {
enabled: false
}
}
isolated resource function get comments(CommentFilter filter = {}) returns Comment[] {
record {|
int id;
string content;
int authorId;
int postId;
|}[] comments = ds:getComments({postId: self.post.id, ...filter});
return from var comment in comments
select {
id: comment.id,
content: comment.content,
author: new User(ds:getAuthor(comment.authorId))
};
}
}
public distinct isolated service class User {
private final readonly & ds:UserRecord user;
isolated function init(ds:UserRecord userRecord) {
self.user = userRecord;
}
isolated resource function get id() returns @graphql:ID int {
return self.user.id;
}
isolated resource function get username() returns string {
return self.user.username;
}
isolated resource function get email() returns string {
return self.user.email;
}
isolated resource function get posts() returns Post[] {
return ds:getPostsByAuthorId(self.user.id).'map(isolated function(ds:PostRecord post) returns Post => new (post));
}
}
public type CommentFilter record {|
int authorId?;
|};
public type Comment record {|
int id;
string content;
User author;
|};