-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.bal
65 lines (59 loc) · 2.14 KB
/
service.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
import sm_backend.datasource as ds;
import ballerina/graphql;
import ballerina/log;
@graphql:ServiceConfig {
cacheConfig: {
enabled: true,
maxSize: 30,
maxAge: 120
},
maxQueryDepth: 20
}
service /sm_backend on new graphql:Listener(9000) {
resource function get user(@graphql:ID int id) returns User? {
ds:UserRecord|error user = ds:getUser(id);
if user is ds:UserRecord {
return new (user);
}
return;
}
resource function get post(@graphql:ID int id) returns Post? {
ds:PostRecord|error post = ds:getPost(id);
if post is ds:PostRecord {
return new (post);
}
return;
}
resource function get postsByUser(@graphql:ID int userId) returns Post[] {
return ds:getPostsByAuthorId(userId).'map(function(ds:PostRecord post) returns Post {
return new (post);
});
}
remote function createPost(graphql:Context context, string title, string content, @graphql:ID int authorId) returns Post|error {
error? cache = context.invalidate("user.posts");
if cache is error {
log:printInfo(string `Error occured while removing the cache: ${cache.message()}`);
} else {
log:printInfo("Cache removed successfully");
}
ds:addPost(title, content, authorId);
ds:PostRecord? post = ds:getPostByAuthorId(authorId);
if post is () {
return error graphql:Error("Failed to create the post!");
}
return new (post);
}
remote function createComment(@graphql:ID int postId, string content, @graphql:ID int authorId) returns Comment|error {
ds:UserRecord|error user = ds:getUser(authorId);
ds:PostRecord|error post = ds:getPost(postId);
if post is ds:PostRecord && user is ds:UserRecord {
ds:CommentRecord comment = ds:addComment(postId, content, authorId);
return {
id: comment.id,
content: comment.content,
author: new User(user)
};
}
return error graphql:Error("Failed to create the comment!");
}
}