-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.bal
More file actions
156 lines (142 loc) · 5.74 KB
/
Copy pathservice.bal
File metadata and controls
156 lines (142 loc) · 5.74 KB
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
import movie_rating_system.datasource;
import ballerina/graphql;
import ballerina/log;
import ballerina/uuid;
import ballerinax/jaeger as _;
import ballerinax/prometheus as _;
import xlibb/pubsub;
const MOVIE_TOPIC = "movies";
configurable boolean enableGraphiql = false;
configurable boolean enableIntrospection = false;
configurable int maxQueryDepth = 4;
configurable boolean initDatabase = true;
final datasource:Datasource datasource = check new (initDatabase);
final pubsub:PubSub pubsub = new;
@display {
label: "Movie Rating System",
id: "movie-rating-system"
}
@graphql:ServiceConfig {
graphiql: {
enabled: enableGraphiql
},
introspection: enableIntrospection,
maxQueryDepth,
contextInit: initContext,
cacheConfig: {
enabled: true,
maxSize: 200
}
}
service on new graphql:Listener(9091) {
# Returns the list of movies in the database.
# + return - List of movies
resource function get movies(graphql:Context context) returns Movie[]|error {
datasource:Datasource datasource = check context.get(DATASOURCE).ensureType();
stream<MovieRecord, error?> movieStream = check datasource->getMovies();
return from MovieRecord movieRecord in movieStream
select new (movieRecord);
}
# Returns the list of users in the database.
# + return - List of users
@graphql:ResourceConfig {
interceptors: new UserAuthInterceptor()
}
resource function get users(graphql:Context context) returns User[]|error {
datasource:Datasource datasource = check context.get(DATASOURCE).ensureType();
stream<UserRecord, error?> userStream = check datasource->getUsers();
return from UserRecord userRecord in userStream
select new (userRecord);
}
# Returns the list of directors in the database.
# + return - List of directors
resource function get directors(graphql:Context context) returns Director[]|error {
datasource:Datasource datasource = check context.get(DATASOURCE).ensureType();
stream<DirectorRecord, error?> directorStream = check datasource->getDirectors();
return from DirectorRecord directorRecord in directorStream
select new (directorRecord);
}
# Returns the Director with the given ID.
# + id - The ID of the director
# + return - The director with the given ID
resource function get director(graphql:Context context, @graphql:ID string id) returns Director|error {
datasource:Datasource datasource = check context.get(DATASOURCE).ensureType();
DirectorRecord|error directorRecord = datasource->getDirector(id);
if directorRecord is error {
return error("Director not found");
}
return new (directorRecord);
}
# Adds a new review to the database.
# + context - The GraphQL context
# + reviewInput - The input values for the review
# + return - The added review
@graphql:ResourceConfig {
interceptors: new UserAuthInterceptor()
}
remote function addReview(graphql:Context context, ReviewInput reviewInput) returns Review|error {
datasource:Datasource datasource = check context.get(DATASOURCE).ensureType();
string userId = check getUserIdFromContext(context);
ReviewRecord reviewRecord = {
id: uuid:createRandomUuid(),
userId,
...reviewInput
};
ReviewRecord|error result = datasource->addReview(reviewRecord);
if result is error {
log:printError("Failed to add the review", result);
return error("Failed to add the review");
}
check context.invalidate("reviews");
return new (result);
}
# Adds a new movie to the database.
# + context - The GraphQL context
# + movieInput - The input values for the movie
# + return - The added movie
@graphql:ResourceConfig {
interceptors: new UserAuthInterceptor()
}
remote function addMovie(graphql:Context context, MovieInput movieInput) returns Movie|error {
datasource:Datasource datasource = check context.get(DATASOURCE).ensureType();
MovieRecord movieRecord = {
id: uuid:createRandomUuid(),
...movieInput
};
MovieRecord|error result = datasource->addMovie(movieRecord);
if result is error {
log:printError("Failed to add the movie", result);
return error("Failed to add the movie");
}
Movie movie = new (result);
check pubsub.publish(MOVIE_TOPIC, movie, -1);
check context.invalidate("movies");
return movie;
}
# Adds a new director to the database.
# + context - The GraphQL context
# + directorInput - The input values for the director
# + return - The added director
@graphql:ResourceConfig {
interceptors: new UserAuthInterceptor()
}
remote function addDirector(graphql:Context context, DirectorInput directorInput) returns Director|error {
datasource:Datasource datasource = check context.get(DATASOURCE).ensureType();
DirectorRecord directorRecord = {
id: uuid:createRandomUuid(),
...directorInput
};
DirectorRecord|error result = datasource->addDirector(directorRecord);
if result is error {
log:printError("Failed to add the director", result);
return error("Failed to add the director");
}
check context.invalidate("directors");
return new (result);
}
# Subscribes to the movies, to get updates when a new movie is published.
# + return - A stream of movies
isolated resource function subscribe movies(string userId) returns stream<Movie, error?>|error {
return pubsub.subscribe(MOVIE_TOPIC, 10);
}
}