This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
TestxServer.ts
295 lines (262 loc) · 8.03 KB
/
TestxServer.ts
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import {
sqliteInMemoryDatabaseBuilder,
SQLiteDatabase
} from "./SQLiteDatabase";
import {
initGraphbackServer,
GraphbackServer,
ServiceBuilder
} from "./GraphbackServer";
import { graphQLInputContext, InputModelTypeContext } from "graphback";
import { GraphbackClient, initGraphbackClient } from "./GraphbackClient";
import { TestxApi, StringDic } from "./TestxApi";
import { DatabaseImportData, DatabaseSchema, Database } from "./Database";
import { Data } from "ws";
/**
* Graphback configuration for generating the graphql resolvers.
* @constant
*/
const DEFAULT_CONFIG = {
create: true,
update: true,
findAll: true,
find: true,
delete: true,
subCreate: true,
subUpdate: true,
subDelete: true,
disableGen: false
};
export interface TestxServerOptions {
schema: string;
/**
* Custom service layer
*
* @experimental
*/
serviceBuilder?: ServiceBuilder;
/**
* Custom database
*
* @experimental
*/
database?: Database;
}
/**
* Describes a TestxServer. A TestxServer generates a GraphQL server from a data
* model with the resolvers, mutations, type defs and connection with a real
* in-memory database, and exposes it in a url.
*
* @example
* const server = new TestxServer({
* schema: `
* type Item {
* id: ID!
* name: String
* title: String!
* }
* `
* });
* await server.start();
* console.log(`Running on ${await server.httpUrl()}`);
* await server.close();
*/
export class TestxServer implements TestxApi {
protected readonly options: TestxServerOptions;
protected readonly context: InputModelTypeContext[];
protected client?: GraphbackClient;
protected server?: GraphbackServer;
protected database?: Database;
/**
* Create a TestxServer.
* @param options.schema - The Graphback data model definition
* @param options.serviceBuilder - Function to overwrite the default GraphbackCRUDService
* @see {@link https://graphback.dev/docs/datamodel|Grahpback data model definition}
*/
constructor(options: TestxServerOptions) {
this.options = options;
this.context = graphQLInputContext.createModelContext(
options.schema,
DEFAULT_CONFIG
);
}
/**
* Executes the bootstrap() method to generate the GraphQL backend and initialize
* the server.
* Starts the GraphQL server exposing it in http://localhost:port/graphql,
* the port is random.
* The full url server can be retrieved by httpUrl() method after the server starts.
*/
public async start(): Promise<void> {
await this.bootstrap();
if (this.server === undefined) {
// should be impossible until the bootstrap method create it
throw new Error("server is undefined");
}
await this.server.start();
}
/**
* Stops the server to receive requests, but keeps the generated GraphQL backend
* and database connection.
* The server can be resumed with the stored GraphQL backend and database
* connection by using start() method.
*/
public async stop(): Promise<void> {
if (this.server !== undefined) {
await this.server.stop();
}
}
/**
* Close and destroy, the GraphQl server and the database.
*/
public async close(): Promise<void> {
await this.stop();
if (this.database !== undefined) {
await this.database.close();
}
this.server = undefined;
this.database = undefined;
}
/**
* Clears all database data.
*/
public async cleanDatabase(): Promise<void> {
if (this.database !== undefined) {
await this.database.clean();
}
}
/**
* Get the server URL.
* This URL is used to make basic queries and mutations.
*/
public async httpUrl(): Promise<string> {
if (this.server === undefined) {
throw new Error(
`can not retrieve the http url from undefined server, ` +
`use bootstrap() or start() in order to initialize the server`
);
}
return Promise.resolve(this.server.getHttpUrl());
}
/**
* Get the subscriptions URL.
* This URL is used to make subscription queries.
*/
public async wsUrl(): Promise<string> {
if (this.server === undefined) {
throw new Error(
`can not retrieve the subscriptions url from undefined server, ` +
`use bootstrap() or start() in order to initialize the server`
);
}
return Promise.resolve(this.server.getWsUrl());
}
/**
* Get the generated GraphQL schema.
* Only returns the GraphQL schema if it's called after using bootstrap() or
* start() methods.
*/
public async getGraphQlSchema(): Promise<string> {
if (this.server === undefined) {
throw new Error(
`can not retrieve the graphql schema from undefined server, ` +
`use bootstrap() or start() in order to initialize the server`
);
}
return Promise.resolve(this.server.getSchema());
}
/**
* Get the generated database schema.
* Only returns the database schema if it's called after using bootstrap() or
* start() methods.
* @return {Object} An object containing the name of the tables as properties, each property has as value the info about the corresponding table
*/
public async getDatabaseSchema(): Promise<DatabaseSchema> {
if (this.database === undefined) {
throw new Error(
`can not retrieve database schema from undefined database, ` +
`use bootstrap() or start() in order to initialize the database`
);
}
return await this.database.getSchema();
}
/**
* Inserts the data directly in the database.
* Which means that the data doesn't pass through any mutation.
* @param {Object[]} data - Data to insert
*/
public async setData(data: DatabaseImportData): Promise<void> {
if (this.database === undefined) {
throw new Error(
`can not import data into undefined database, ` +
`use bootstrap() or start() in order to initialize the database`
);
}
await this.database.importData(data);
}
/**
* Bootstraps the TestxServer, generating the GraphQL backend with the
* database connection, client queries and mutations and filling in some
* properties needed to start the server.
*/
public async bootstrap(): Promise<void> {
if (this.database === undefined) {
if (this.options.database) {
this.database = this.options.database;
} else {
this.database = await sqliteInMemoryDatabaseBuilder(
this.options.schema
);
}
}
if (this.server === undefined) {
this.server = await initGraphbackServer(
this.context,
this.database.getProvider(),
this.options.serviceBuilder
);
}
if (this.client === undefined) {
this.client = await initGraphbackClient(this.context);
}
}
/**
* Get the generated client queries.
* @return {Object} An object containing the queries as properties
*/
public async getQueries(): Promise<StringDic> {
if (this.client === undefined) {
throw new Error(
`can not retrieve client queries from undefined client, ` +
`use bootstrap() or start() in order to initialize the client`
);
}
return Promise.resolve(this.client.getQueries());
}
/**
* Get the generated client mutations.
* @return {Object} An object containing the mutations as properties
*/
public async getMutations(): Promise<StringDic> {
if (this.client === undefined) {
throw new Error(
`can not retrieve client mutations from undefined client, ` +
`use bootstrap() or start() in order to initialize the client`
);
}
return Promise.resolve(this.client.getMutations());
}
/**
* Get the generated client subscriptions.
* @return {Object} An object containing the subscriptions as properties
*/
public async getSubscriptions(): Promise<StringDic> {
if (this.client === undefined) {
throw new Error(
`can not retrieve client subscriptions from undefined client, ` +
`use bootstrap() or start() in order to initialize the client`
);
}
return Promise.resolve(this.client.getSubscriptions());
}
}