@axinom/mosaic-graphql-common 0.1.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/README.md +16 -0
  2. package/dist/common/checks.d.ts +9 -0
  3. package/dist/common/checks.d.ts.map +1 -0
  4. package/dist/common/checks.js +21 -0
  5. package/dist/common/checks.js.map +1 -0
  6. package/dist/common/index.d.ts +3 -0
  7. package/dist/common/index.d.ts.map +1 -0
  8. package/dist/common/index.js +19 -0
  9. package/dist/common/index.js.map +1 -0
  10. package/dist/common/types.d.ts +13 -0
  11. package/dist/common/types.d.ts.map +1 -0
  12. package/dist/common/types.js +15 -0
  13. package/dist/common/types.js.map +1 -0
  14. package/dist/index.d.ts +4 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +20 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/plugins/add-error-codes-enum-plugin.d.ts +27 -0
  19. package/dist/plugins/add-error-codes-enum-plugin.d.ts.map +1 -0
  20. package/dist/plugins/add-error-codes-enum-plugin.js +78 -0
  21. package/dist/plugins/add-error-codes-enum-plugin.js.map +1 -0
  22. package/dist/plugins/annotate-types-with-permissions-plugin.d.ts +22 -0
  23. package/dist/plugins/annotate-types-with-permissions-plugin.d.ts.map +1 -0
  24. package/dist/plugins/annotate-types-with-permissions-plugin.js +145 -0
  25. package/dist/plugins/annotate-types-with-permissions-plugin.js.map +1 -0
  26. package/dist/plugins/deprecate-stray-node-id-fields-plugin.d.ts +14 -0
  27. package/dist/plugins/deprecate-stray-node-id-fields-plugin.d.ts.map +1 -0
  28. package/dist/plugins/deprecate-stray-node-id-fields-plugin.js +37 -0
  29. package/dist/plugins/deprecate-stray-node-id-fields-plugin.js.map +1 -0
  30. package/dist/plugins/generic-bulk-plugin-factory.d.ts +49 -0
  31. package/dist/plugins/generic-bulk-plugin-factory.d.ts.map +1 -0
  32. package/dist/plugins/generic-bulk-plugin-factory.js +181 -0
  33. package/dist/plugins/generic-bulk-plugin-factory.js.map +1 -0
  34. package/dist/plugins/graphiql-management-mode-plugin-hook.d.ts +13 -0
  35. package/dist/plugins/graphiql-management-mode-plugin-hook.d.ts.map +1 -0
  36. package/dist/plugins/graphiql-management-mode-plugin-hook.js +44 -0
  37. package/dist/plugins/graphiql-management-mode-plugin-hook.js.map +1 -0
  38. package/dist/plugins/index.d.ts +10 -0
  39. package/dist/plugins/index.d.ts.map +1 -0
  40. package/dist/plugins/index.js +26 -0
  41. package/dist/plugins/index.js.map +1 -0
  42. package/dist/plugins/omit-from-query-root-plugin.d.ts +17 -0
  43. package/dist/plugins/omit-from-query-root-plugin.d.ts.map +1 -0
  44. package/dist/plugins/omit-from-query-root-plugin.js +43 -0
  45. package/dist/plugins/omit-from-query-root-plugin.js.map +1 -0
  46. package/dist/plugins/operations-enum-generator-plugin-factory.d.ts +15 -0
  47. package/dist/plugins/operations-enum-generator-plugin-factory.d.ts.map +1 -0
  48. package/dist/plugins/operations-enum-generator-plugin-factory.js +108 -0
  49. package/dist/plugins/operations-enum-generator-plugin-factory.js.map +1 -0
  50. package/dist/plugins/subscriptions-plugin-factory.d.ts +9 -0
  51. package/dist/plugins/subscriptions-plugin-factory.d.ts.map +1 -0
  52. package/dist/plugins/subscriptions-plugin-factory.js +67 -0
  53. package/dist/plugins/subscriptions-plugin-factory.js.map +1 -0
  54. package/dist/plugins/validation-directives-plugin.d.ts +6 -0
  55. package/dist/plugins/validation-directives-plugin.d.ts.map +1 -0
  56. package/dist/plugins/validation-directives-plugin.js +117 -0
  57. package/dist/plugins/validation-directives-plugin.js.map +1 -0
  58. package/dist/postgraphile/enhance-graphql-errors.d.ts +48 -0
  59. package/dist/postgraphile/enhance-graphql-errors.d.ts.map +1 -0
  60. package/dist/postgraphile/enhance-graphql-errors.js +67 -0
  61. package/dist/postgraphile/enhance-graphql-errors.js.map +1 -0
  62. package/dist/postgraphile/index.d.ts +4 -0
  63. package/dist/postgraphile/index.d.ts.map +1 -0
  64. package/dist/postgraphile/index.js +20 -0
  65. package/dist/postgraphile/index.js.map +1 -0
  66. package/dist/postgraphile/postgraphile-options-builder.d.ts +273 -0
  67. package/dist/postgraphile/postgraphile-options-builder.d.ts.map +1 -0
  68. package/dist/postgraphile/postgraphile-options-builder.js +419 -0
  69. package/dist/postgraphile/postgraphile-options-builder.js.map +1 -0
  70. package/dist/postgraphile/websocket-utils.d.ts +11 -0
  71. package/dist/postgraphile/websocket-utils.d.ts.map +1 -0
  72. package/dist/postgraphile/websocket-utils.js +17 -0
  73. package/dist/postgraphile/websocket-utils.js.map +1 -0
  74. package/package.json +61 -0
  75. package/src/common/checks.ts +23 -0
  76. package/src/common/index.ts +2 -0
  77. package/src/common/types.ts +15 -0
  78. package/src/index.ts +3 -0
  79. package/src/plugins/add-error-codes-enum-plugin.ts +102 -0
  80. package/src/plugins/annotate-types-with-permissions-plugin.spec.ts +158 -0
  81. package/src/plugins/annotate-types-with-permissions-plugin.ts +205 -0
  82. package/src/plugins/deprecate-stray-node-id-fields-plugin.ts +41 -0
  83. package/src/plugins/generic-bulk-plugin-factory.ts +313 -0
  84. package/src/plugins/graphiql-management-mode-plugin-hook.ts +46 -0
  85. package/src/plugins/index.ts +9 -0
  86. package/src/plugins/omit-from-query-root-plugin.ts +69 -0
  87. package/src/plugins/operations-enum-generator-plugin-factory.ts +130 -0
  88. package/src/plugins/subscriptions-plugin-factory.ts +114 -0
  89. package/src/plugins/validation-directives-plugin.ts +141 -0
  90. package/src/postgraphile/enhance-graphql-errors.spec.ts +241 -0
  91. package/src/postgraphile/enhance-graphql-errors.ts +138 -0
  92. package/src/postgraphile/index.ts +3 -0
  93. package/src/postgraphile/postgraphile-options-builder.spec.ts +744 -0
  94. package/src/postgraphile/postgraphile-options-builder.ts +510 -0
  95. package/src/postgraphile/websocket-utils.ts +19 -0
@@ -0,0 +1,419 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.PostgraphileOptionsBuilder = void 0;
7
+ /* eslint-disable @typescript-eslint/no-explicit-any */
8
+ const pg_pubsub_1 = __importDefault(require("@graphile/pg-pubsub"));
9
+ const graphile_build_1 = require("graphile-build");
10
+ const postgraphile_1 = require("postgraphile");
11
+ const enhance_graphql_errors_1 = require("./enhance-graphql-errors");
12
+ /**
13
+ * A builder class that allows construction of postgraphile options using multiple dedicated extension methods.
14
+ */
15
+ class PostgraphileOptionsBuilder {
16
+ constructor() {
17
+ this.transformers = [];
18
+ this.headers = {};
19
+ this.pluginHooks = new Set();
20
+ this.hookFuncs = new Set();
21
+ }
22
+ /**
23
+ * Builds a postgraphile options object with previously used extension methods.
24
+ */
25
+ build() {
26
+ let options = {};
27
+ for (const transform of this.transformers) {
28
+ options = transform(options);
29
+ }
30
+ this.buildHeadersHookPlugin();
31
+ // Build plugin hooks from hook functions
32
+ [...this.hookFuncs].map((hookFunc) => {
33
+ this.pluginHooks.add(hookFunc(options));
34
+ });
35
+ if (this.pluginHooks.size > 0) {
36
+ options.pluginHook = (0, postgraphile_1.makePluginHook)([...this.pluginHooks]);
37
+ }
38
+ return options;
39
+ }
40
+ /**
41
+ * Builds a hook plugin that adds custom headers to http responses.
42
+ */
43
+ buildHeadersHookPlugin() {
44
+ if (Object.keys(this.headers).length > 0) {
45
+ this.pluginHooks.add({
46
+ 'postgraphile:http:handler': (req, { res }) => {
47
+ for (const key in this.headers) {
48
+ if (Object.prototype.hasOwnProperty.call(this.headers, key)) {
49
+ res.setHeader(key, this.headers[key]);
50
+ }
51
+ }
52
+ return req;
53
+ },
54
+ });
55
+ }
56
+ }
57
+ /**
58
+ * This builder option must be used if the Hook Plugin requires PostGraphileOptions in the logic.
59
+ * A function which returns a PostGraphilePlugin should be passed into this method.
60
+ * That function will use PostGraphileOptions created in the build() method and attach the plugin returned from the function as a Postgraphile Hook.
61
+ * @example
62
+ *
63
+ * ```ts
64
+ new PostgraphileOptionsBuilder()
65
+ .addHookPluginFactory(customHookFactory)
66
+ * ```
67
+ *
68
+ * @param hookFunction This is a function which takes an argument of type PostGraphileOptions<Request, Response> and returns a PostGraphilePlugin object.
69
+ * @returns
70
+ */
71
+ addHookPluginFactory(hookFunction) {
72
+ this.hookFuncs.add(hookFunction);
73
+ return this;
74
+ }
75
+ /**
76
+ * This function takes a PostGraphilePlugin object and attaches it as a Postgraphile Hook when build() is called.
77
+ * @example
78
+ *
79
+ * ```ts
80
+ new PostgraphileOptionsBuilder()
81
+ .addHookPluginFunction(subscriptionAuthorizationHook)
82
+ * ```
83
+ *
84
+ * @param hookPlugin This is a PostGraphilePlugin object.
85
+ * @returns
86
+ */
87
+ addHookPlugin(hookPlugin) {
88
+ this.pluginHooks.add(hookPlugin);
89
+ return this;
90
+ }
91
+ /**
92
+ * Sets `pgSettings` property of postgraphile options object.
93
+ * Example:
94
+ *
95
+ * ```ts
96
+ new PostgraphileOptionsBuilder()
97
+ .setPgSettings(async req => {
98
+ const { subject } = await getManagementAuthenticationContext(req, idServiceParams);
99
+ return buildPgSettings(subject, config.dbGqlRole, config.serviceId);
100
+ })
101
+ * ```
102
+ *
103
+ * @param pgSettings A plain object specifying custom config values to set in the PostgreSQL transaction (accessed via current_setting('my.custom.setting')) or an (optionally asynchronous) function which will return the same (or a Promise to the same) based on the incoming web request (e.g. to extract session data).
104
+ */
105
+ setPgSettings(pgSettings) {
106
+ this.transformers.push((options) => {
107
+ options.pgSettings = pgSettings;
108
+ return options;
109
+ });
110
+ return this;
111
+ }
112
+ /**
113
+ * Sets `handleErrors` property of postgraphile options object.
114
+ * Example:
115
+ *
116
+ * ```ts
117
+ new PostgraphileOptionsBuilder()
118
+ .setErrorsHandler((errors, req) => {
119
+ return enhanceGraphqlErrors(
120
+ result.errors,
121
+ req.body.operationName,
122
+ customizeGraphQlErrorFields(defaultPgErrorMapper),
123
+ logGraphQlError(defaultWriteLogMapper, this.logger),
124
+ );
125
+ }
126
+ })
127
+ * ```
128
+ *
129
+ * @param handleErrors Enables ability to modify errors before sending them down to the client. Optionally can send down custom responses. If you use this then showErrorStack and extendedError may have no effect.
130
+ */
131
+ setErrorsHandler(handleErrors) {
132
+ this.transformers.push((options) => {
133
+ options.handleErrors = handleErrors;
134
+ return options;
135
+ });
136
+ return this;
137
+ }
138
+ /**
139
+ * Sets properties of postgraphile options object to be used in without any conditions. Additional calls will override and add properties.
140
+ * Example:
141
+ *
142
+ * ```ts
143
+ new PostgraphileOptionsBuilder()
144
+ .setProperties({
145
+ enableCors: true,
146
+ dynamicJson: true,
147
+ ignoreRBAC: false,
148
+ legacyRelations: 'omit',
149
+ setofFunctionsContainNulls: false,
150
+ })
151
+ * ```
152
+ * @param properties An object containing postgraphile options.
153
+ */
154
+ setProperties(properties) {
155
+ this.transformers.push((options) => {
156
+ return Object.assign(Object.assign({}, options), properties);
157
+ });
158
+ return this;
159
+ }
160
+ /**
161
+ * Sets properties of postgraphile options object to be used based on some
162
+ * condition (use if true, omit if false). Additional calls will override and
163
+ * add properties.
164
+ * Example:
165
+ *
166
+ * ```ts
167
+ new PostgraphileOptionsBuilder()
168
+ .setConditionalProperties(config.isDev, {
169
+ exportGqlSchemaPath: './src/generated/schema.graphql',
170
+ watchPg: true,
171
+ graphiql: true,
172
+ enhanceGraphiql: true,
173
+ allowExplain: true,
174
+ })
175
+ * ```
176
+ * @param properties An object containing postgraphile options.
177
+ */
178
+ setConditionalProperties(condition, properties) {
179
+ if (condition) {
180
+ this.transformers.push((options) => {
181
+ return Object.assign(Object.assign({}, options), properties);
182
+ });
183
+ }
184
+ return this;
185
+ }
186
+ /**
187
+ * Sets all necessary postgraphile properties to enable subscriptions.
188
+ * Example:
189
+ *
190
+ * ```ts
191
+ new PostgraphileOptionsBuilder()
192
+ .enableSubscriptions({
193
+ plugin: SubscriptionsPlugin,
194
+ websocketMiddlewares: middlewares,
195
+ hookFactory: subscriptionAuthorizationHookFactory
196
+ })
197
+ * ```
198
+ *
199
+ * Results in something like this:
200
+ *
201
+ * ```ts
202
+ import PgPubsub from '@graphile/pg-pubsub';
203
+ import { makePluginHook } from 'postgraphile';
204
+ //.........
205
+ {
206
+ appendPlugins: [SubscriptionsPlugin], // Other plugins will be included if they were added using other methods
207
+ pluginHook: makePluginHook([PgPubsub, subscriptionAuthorizationHook]),
208
+ subscriptions: true,
209
+ websocketMiddlewares, // Empty array if websocketMiddlewares are not specified
210
+ }
211
+ * ```
212
+ *
213
+ * To make sure that Postgraphile is able to correctly establish WebSockets
214
+ * connection, additional changes should be done by using
215
+ * `enhanceHttpServerWithSubscriptions`.
216
+ *
217
+ * See more here: https://www.graphile.org/postgraphile/subscriptions/#advanced-setup
218
+ *
219
+ * @param plugin Graphile engine plugin that adds one or multiple subscription
220
+ * endpoints. Can be added separately.
221
+ * @param websocketMiddlewares If you're using websockets (subscriptions ||
222
+ * live) then you may want to authenticate your users using sessions or
223
+ * similar. You can pass some simple middlewares here that will be executed
224
+ * against the websocket connection in order to perform authentication.
225
+ * @param hookFactory A factory function for a Postgraphile hook plugin.
226
+ */
227
+ enableSubscriptions({ plugin, websocketMiddlewares, hookFactory, }) {
228
+ this.transformers.push((options) => {
229
+ var _a, _b;
230
+ options.subscriptions = true;
231
+ options.appendPlugins = [
232
+ ...new Set([...((_a = options.appendPlugins) !== null && _a !== void 0 ? _a : []), plugin]),
233
+ ];
234
+ // Add the pub/sub realtime provider
235
+ // This is for PostGraphile server plugins: https://www.graphile.org/postgraphile/plugins/
236
+ this.pluginHooks.add(pg_pubsub_1.default);
237
+ if (websocketMiddlewares) {
238
+ options.websocketMiddlewares = [
239
+ ...new Set([
240
+ ...((_b = options.websocketMiddlewares) !== null && _b !== void 0 ? _b : []),
241
+ ...websocketMiddlewares,
242
+ ]),
243
+ ];
244
+ }
245
+ else if (!options.websocketMiddlewares) {
246
+ options.websocketMiddlewares = [];
247
+ }
248
+ if (hookFactory) {
249
+ this.hookFuncs.add(hookFactory);
250
+ }
251
+ return options;
252
+ });
253
+ return this;
254
+ }
255
+ /**
256
+ * Adds multiple graphile engine plugins to `appendPlugins` array of postgraphile options object. `setAdditionalGraphQLContextFromRequest` and `addGraphileBuildOptions` methods can be used to pass parameters to said plugins.
257
+ * Example:
258
+ *
259
+ ```ts
260
+ new PostgraphileOptionsBuilder()
261
+ .addPlugins(
262
+ PgSimplifyInflectorPlugin,
263
+ ScalarTypesPlugin,
264
+ SmartTagsPlugin,
265
+ ValidationDirectivesPlugin,
266
+ )
267
+ ```
268
+ * @param plugins Comma-separated list of Graphile Engine schema plugins to load after the default plugins.
269
+ */
270
+ addPlugins(...plugins) {
271
+ this.transformers.push((options) => {
272
+ var _a;
273
+ options.appendPlugins = [
274
+ ...new Set([...((_a = options.appendPlugins) !== null && _a !== void 0 ? _a : []), ...plugins]),
275
+ ];
276
+ return options;
277
+ });
278
+ return this;
279
+ }
280
+ /**
281
+ * Adds multiple graphile engine plugins to `appendPlugins` array of postgraphile options object.
282
+ * `setAdditionalGraphQLContextFromRequest` and `addGraphileBuildOptions` methods can be used to pass parameters to said plugins.
283
+ * Explicit condition parameter is used to make a decision if plugins should be used or not (use if true, omit if false).
284
+ * Example:
285
+ *
286
+ ```ts
287
+ new PostgraphileOptionsBuilder()
288
+ .addConditionalPlugins(config.isDev || config.enablePopulatePlugins, PopulateMoviesPlugin, PopulateTvshowsPlugin)
289
+ ```
290
+ * @param plugins Comma-separated list of Graphile Engine schema plugins to load after the default plugins.
291
+ */
292
+ addConditionalPlugins(condition, ...plugins) {
293
+ if (condition) {
294
+ this.addPlugins(...plugins);
295
+ }
296
+ return this;
297
+ }
298
+ /**
299
+ * Skips multiple default postgraphile plugins by adding them to `skipPlugins` array of postgraphile options object .
300
+ * Example:
301
+ *
302
+ ```ts
303
+ new PostgraphileOptionsBuilder()
304
+ .skipPlugins(NodePlugin, MutationPayloadQueryPlugin)
305
+ ```
306
+ * @param plugins Comma-separated list of Graphile Engine schema plugins to skip.
307
+ */
308
+ skipPlugins(...plugins) {
309
+ this.transformers.push((options) => {
310
+ var _a;
311
+ options.skipPlugins = [
312
+ ...new Set([...((_a = options.skipPlugins) !== null && _a !== void 0 ? _a : []), ...plugins]),
313
+ ];
314
+ return options;
315
+ });
316
+ return this;
317
+ }
318
+ /**
319
+ * Adds properties to resulting `graphileBuildOptions` property of postgraphile options object. Useful for modifying default postgraphile plugin settings.
320
+ * Example:
321
+ *
322
+ ```ts
323
+ new PostgraphileOptionsBuilder()
324
+ .addGraphileBuildOptions({ pgSkipInstallingWatchFixtures: true })
325
+ ```
326
+ * @param graphileBuildOptions Additional Options to pass through into the graphile schema building system (received via the second argument of a plugin).
327
+ */
328
+ addGraphileBuildOptions(graphileBuildOptions) {
329
+ this.transformers.push((options) => {
330
+ var _a;
331
+ options.graphileBuildOptions = Object.assign(Object.assign({}, ((_a = options.graphileBuildOptions) !== null && _a !== void 0 ? _a : {})), graphileBuildOptions);
332
+ return options;
333
+ });
334
+ return this;
335
+ }
336
+ /**
337
+ * Adds `additionalGraphQLContextFromRequest` property to postgraphile options object. Define only once. Multiple calls will not expand previous calls, but will override them.
338
+ *
339
+ * Example:
340
+ ```ts
341
+ new PostgraphileOptionsBuilder()
342
+ .setAdditionalGraphQLContextFromRequest(async req => {
343
+ const { subject } = await getManagementAuthenticationContext(req);
344
+ return { subject, serviceId: config.serviceId };
345
+ })
346
+ ```
347
+ * @param callback Some Graphile Engine schema plugins may need additional information available on the context argument to the resolver - you can use this function to provide such information based on the incoming request - you can even use this to change the response , e.g. setting cookies.
348
+ */
349
+ setAdditionalGraphQLContextFromRequest(callback) {
350
+ this.transformers.push((options) => {
351
+ options.additionalGraphQLContextFromRequest = callback;
352
+ return options;
353
+ });
354
+ return this;
355
+ }
356
+ /**
357
+ * Enables or disables graphiql GUI depending on a passed condition parameter. Enable if true, disable if false.
358
+ * Sets 3 postgraphile properties: `graphiql`, `enhanceGraphiql` and `allowExplain`.
359
+ * Example:
360
+ *
361
+ * ```ts
362
+ new PostgraphileOptionsBuilder()
363
+ .setGraphiql(config.graphqlGuiEnabled)
364
+ * ```
365
+ */
366
+ setGraphiql(condition) {
367
+ this.transformers.push((options) => {
368
+ return Object.assign(Object.assign({}, options), {
369
+ graphiql: condition,
370
+ enhanceGraphiql: condition,
371
+ allowExplain: condition,
372
+ });
373
+ });
374
+ return this;
375
+ }
376
+ /**
377
+ * Sets a response header for each GraphQL request.
378
+ *
379
+ * @param name header name
380
+ * @param value header value
381
+ */
382
+ setHeader(name, value) {
383
+ this.headers[name] = value;
384
+ return this;
385
+ }
386
+ /**
387
+ * Sets default values for properties that we see as best practices which most
388
+ * services should start from. This includes the default GraphQL errors handler,
389
+ * suggested default properties, development properties in dev-environments,
390
+ * and excludes the 'NodePlugin'.
391
+ */
392
+ setDefaultSettings(isDev, graphqlGuiEnabled) {
393
+ this.setErrorsHandler((errors) => {
394
+ return (0, enhance_graphql_errors_1.enhanceGraphqlErrors)(errors);
395
+ })
396
+ .setProperties({
397
+ enableCors: true,
398
+ dynamicJson: true,
399
+ ignoreRBAC: false,
400
+ legacyRelations: 'omit',
401
+ setofFunctionsContainNulls: false,
402
+ watchPg: false,
403
+ disableQueryLog: true,
404
+ retryOnInitFail: true,
405
+ })
406
+ .setConditionalProperties(isDev, {
407
+ exportGqlSchemaPath: './src/generated/graphql/schema.graphql',
408
+ watchPg: true,
409
+ disableQueryLog: false,
410
+ retryOnInitFail: false,
411
+ })
412
+ .setGraphiql(graphqlGuiEnabled)
413
+ // removes some nodeId and byNodeId endpoints, but still leaves support for cursor pagination
414
+ .skipPlugins(graphile_build_1.NodePlugin);
415
+ return this;
416
+ }
417
+ }
418
+ exports.PostgraphileOptionsBuilder = PostgraphileOptionsBuilder;
419
+ //# sourceMappingURL=postgraphile-options-builder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postgraphile-options-builder.js","sourceRoot":"","sources":["../../src/postgraphile/postgraphile-options-builder.ts"],"names":[],"mappings":";;;;;;AAAA,uDAAuD;AACvD,oEAA2C;AAE3C,mDAA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AW,CAAC,2BAAU,CAAC,CAAC;QAC3B,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAtdD,gEAsdC"}
@@ -0,0 +1,11 @@
1
+ import { Request } from 'express';
2
+ import { WebSocket } from 'graphql-ws';
3
+ /**
4
+ * Extract the reference to web socket when given a request object that is coming
5
+ * from Postgraphile. The web socket reference is attached as a symbol.
6
+ *
7
+ * @param req A Postgraphile request.
8
+ * @returns
9
+ */
10
+ export declare const getWebsocketFromRequest: (req: Request) => WebSocket | undefined;
11
+ //# sourceMappingURL=websocket-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket-utils.d.ts","sourceRoot":"","sources":["../../src/postgraphile/websocket-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,QAC7B,OAAO,KACX,SAAS,GAAG,SAMd,CAAC"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getWebsocketFromRequest = void 0;
4
+ /**
5
+ * Extract the reference to web socket when given a request object that is coming
6
+ * from Postgraphile. The web socket reference is attached as a symbol.
7
+ *
8
+ * @param req A Postgraphile request.
9
+ * @returns
10
+ */
11
+ const getWebsocketFromRequest = (req) => {
12
+ const websocketSymbol = Object.getOwnPropertySymbols(req.socket).filter((sym) => sym.description === 'websocket')[0];
13
+ const websocket = req.socket[websocketSymbol];
14
+ return websocket;
15
+ };
16
+ exports.getWebsocketFromRequest = getWebsocketFromRequest;
17
+ //# sourceMappingURL=websocket-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket-utils.js","sourceRoot":"","sources":["../../src/postgraphile/websocket-utils.ts"],"names":[],"mappings":";;;AAGA;;;;;;GAMG;AACI,MAAM,uBAAuB,GAAG,CACrC,GAAY,EACW,EAAE;IACzB,MAAM,eAAe,GAAG,MAAM,CAAC,qBAAqB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,MAAM,CACrE,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,WAAW,KAAK,WAAW,CACzC,CAAC,CAAC,CAAC,CAAC;IACL,MAAM,SAAS,GAAI,GAAW,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;IACvD,OAAO,SAAsB,CAAC;AAChC,CAAC,CAAC;AARW,QAAA,uBAAuB,2BAQlC"}
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@axinom/mosaic-graphql-common",
3
+ "version": "0.1.0-rc.0",
4
+ "description": "Common GraphQL and PostGraphile related functionality.",
5
+ "author": "Axinom",
6
+ "license": "PROPRIETARY",
7
+ "keywords": [
8
+ "axinom",
9
+ "mosaic",
10
+ "axinom mosaic"
11
+ ],
12
+ "files": [
13
+ "dist",
14
+ "src"
15
+ ],
16
+ "main": "dist/index.js",
17
+ "types": "dist/index.d.ts",
18
+ "scripts": {
19
+ "clean": "rimraf dist",
20
+ "build": "yarn clean && tsc --project tsconfig.build.json",
21
+ "build:ci": "yarn workspaces focus && yarn build",
22
+ "dev": "tsc -w --project tsconfig.build.json",
23
+ "publish:latest": "npm run build && npm publish",
24
+ "test": "jest --silent",
25
+ "test:watch": "jest --watch --silent",
26
+ "test:cov": "jest --coverage --silent && yarn posttest:cov",
27
+ "posttest:cov": "ts-node ../../scripts/open-test-coverage.ts -- libs/graphql-common",
28
+ "test:debug": "node --inspect -r ts-node/register ../../node_modules/jest/bin/jest.js --runInBand --silent",
29
+ "test:ci": "jest --reporters=default --reporters=jest-junit --coverage --coverageReporters=cobertura --coverageReporters=html",
30
+ "lint": "eslint . --ext .ts,.tsx,.js --color --cache"
31
+ },
32
+ "dependencies": {
33
+ "@graphile/pg-pubsub": "^4.12.3",
34
+ "endent": "^2.1.0",
35
+ "express": "^4.17.1",
36
+ "graphile-build": "^4.13.0",
37
+ "graphile-build-pg": "^4.13.0",
38
+ "graphile-utils": "^4.13.0",
39
+ "graphql": "^15.4.0",
40
+ "inflection": "^1.12.0",
41
+ "jest": "^29",
42
+ "jest-expect-message": "^1.1.3",
43
+ "pg": "^8.5.1",
44
+ "postgraphile": "^4.13.0"
45
+ },
46
+ "devDependencies": {
47
+ "@types/express": "^4.17.17",
48
+ "@types/inflection": "^1.5.28",
49
+ "@types/jest": "^29",
50
+ "@types/jest-expect-message": "^1.1.0",
51
+ "@types/node": "^18.11.18",
52
+ "eslint": "^8.35.0",
53
+ "rimraf": "^3.0.2",
54
+ "ts-node": "^10.9.1",
55
+ "typescript": "^5.0.4"
56
+ },
57
+ "publishConfig": {
58
+ "access": "public"
59
+ },
60
+ "gitHead": "294d6a683845640ef70dc4ed5e9f057dfaacc388"
61
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Check if a variable is a string index based object
3
+ * @param value variable to check
4
+ */
5
+ // must be a real function: https://github.com/microsoft/TypeScript/issues/36931
6
+ export function assertDictionary(
7
+ value: unknown,
8
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ ): asserts value is { [key: string]: any } {
10
+ if (typeof value !== 'object' || value === null) {
11
+ throw Error('The value is no string indexed object.');
12
+ }
13
+ }
14
+
15
+ export const isNullOrWhitespace = <T>(
16
+ value: T | null | undefined,
17
+ ): value is null | undefined => {
18
+ return (
19
+ value === null ||
20
+ value === undefined ||
21
+ (typeof value === 'string' && value.trim().length === 0)
22
+ );
23
+ };
@@ -0,0 +1,2 @@
1
+ export * from './checks';
2
+ export * from './types';
@@ -0,0 +1,15 @@
1
+ /** TYpe definition of an object with field accessor. */
2
+ export interface Dict<T> {
3
+ [key: string]: T;
4
+ }
5
+
6
+ /**
7
+ * Error to be thrown in a default block of an exhaustive switch-case block, if checked value has a finite amount of possible values (e.g enum).
8
+ * This error has a very low chance to actually be thrown.
9
+ * Instead, it is used by IDE to give an indication of which possible value is not covered by switch-case and will result in a failed build.
10
+ */
11
+ export class UnreachableCaseError extends Error {
12
+ constructor(val: never) {
13
+ super(`Unreachable case: ${val}`);
14
+ }
15
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './common';
2
+ export * from './plugins';
3
+ export * from './postgraphile';
@@ -0,0 +1,102 @@
1
+ import { GraphQLEnumType } from 'graphql';
2
+ import { Plugin } from 'postgraphile';
3
+ import { Dict } from '../common';
4
+
5
+ type GraphQLEnumValues = Dict<{ value: string; description: string }>;
6
+
7
+ export interface MosaicErrorDefinition {
8
+ message: string;
9
+ code: string;
10
+ }
11
+
12
+ /**
13
+ * Maps an array of error code enum objects to a single object, forming a
14
+ * unified ErrorCodes GraphQL Enum.
15
+ */
16
+ const mapEnumValues = (
17
+ errorCodeEnums: Dict<MosaicErrorDefinition>[],
18
+ ): GraphQLEnumValues => {
19
+ const values: GraphQLEnumValues = {};
20
+ for (const errorCodeEnum of errorCodeEnums) {
21
+ for (const key in errorCodeEnum) {
22
+ if (Object.prototype.hasOwnProperty.call(errorCodeEnum, key)) {
23
+ const element = errorCodeEnum[key];
24
+ if (values[element.code]) {
25
+ throw new Error(
26
+ `Error code '${element.code}' is used in multiple enum objects. Error codes must be unique.`,
27
+ );
28
+ }
29
+
30
+ values[element.code] = {
31
+ value: element.code,
32
+ description: element.message,
33
+ };
34
+ }
35
+ }
36
+ }
37
+
38
+ return values;
39
+ };
40
+
41
+ /**
42
+ * Adds a detached enum type to the GraphQL schema, listing all possible
43
+ * ErrorCode values and adding messages as a descriptions.
44
+ *
45
+ * Make sure to add it after `EnforceStrictPermissionsPlugin` or any other
46
+ * plugin that might filter queries out of the resulting schema.
47
+ *
48
+ * If there is a need to adjust this enum - use `GraphQLEnumType` postgraphile
49
+ * hook and `isErrorCodesEnum` scope to find the generated enum. E.g.
50
+ *
51
+ * ```ts
52
+ builder.hook('GraphQLEnumType', (enumField, _build, context) => {
53
+ if (context.scope.isErrorCodesEnum === true) {
54
+ // Modification logic
55
+ }
56
+ return enumField;
57
+ });
58
+ * ```
59
+ */
60
+ export const AddErrorCodesEnumPluginFactory: (
61
+ errorCodeEnums: Dict<MosaicErrorDefinition>[],
62
+ ) => Plugin = (errorCodeEnums: Dict<MosaicErrorDefinition>[]) => (builder) => {
63
+ const dummyQueryName = '_dummyErrorCodesEnum';
64
+
65
+ // Attaches the enum type and a dummy query endpoint to the schema.
66
+ // Postgraphile removes all unused types, so we are first adding a dummy
67
+ // query endpoint (to emulate enum usage), and during the `finalize` phase -
68
+ // we delete it, so that the cleanup would not remove the enum type itself.
69
+ builder.hook(
70
+ 'GraphQLObjectType:fields',
71
+ (fields, { extend, newWithHooks }, { scope: { isRootQuery } }) => {
72
+ if (!isRootQuery) {
73
+ return fields;
74
+ }
75
+ const values = mapEnumValues(errorCodeEnums);
76
+ const enumType = newWithHooks(
77
+ GraphQLEnumType,
78
+ {
79
+ name: 'ErrorCodesEnum',
80
+ description: `Exposes all error codes and messages for errors that a service requests can throw. In some cases, messages that are actually thrown can be different, since they can include more details or a single code can used for different errors of the same type.`,
81
+ values,
82
+ },
83
+ { isErrorCodesEnum: true },
84
+ );
85
+
86
+ return extend(fields, {
87
+ [dummyQueryName]: {
88
+ type: enumType,
89
+ },
90
+ });
91
+ },
92
+ );
93
+
94
+ // Removing the dummy type from schema, leaving only the enum type.
95
+ builder.hook('finalize', (schema) => {
96
+ const query = schema.getQueryType();
97
+ if (query) {
98
+ delete query.getFields()[dummyQueryName];
99
+ }
100
+ return schema;
101
+ });
102
+ };