@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.
- package/README.md +16 -0
- package/dist/common/checks.d.ts +9 -0
- package/dist/common/checks.d.ts.map +1 -0
- package/dist/common/checks.js +21 -0
- package/dist/common/checks.js.map +1 -0
- package/dist/common/index.d.ts +3 -0
- package/dist/common/index.d.ts.map +1 -0
- package/dist/common/index.js +19 -0
- package/dist/common/index.js.map +1 -0
- package/dist/common/types.d.ts +13 -0
- package/dist/common/types.d.ts.map +1 -0
- package/dist/common/types.js +15 -0
- package/dist/common/types.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins/add-error-codes-enum-plugin.d.ts +27 -0
- package/dist/plugins/add-error-codes-enum-plugin.d.ts.map +1 -0
- package/dist/plugins/add-error-codes-enum-plugin.js +78 -0
- package/dist/plugins/add-error-codes-enum-plugin.js.map +1 -0
- package/dist/plugins/annotate-types-with-permissions-plugin.d.ts +22 -0
- package/dist/plugins/annotate-types-with-permissions-plugin.d.ts.map +1 -0
- package/dist/plugins/annotate-types-with-permissions-plugin.js +145 -0
- package/dist/plugins/annotate-types-with-permissions-plugin.js.map +1 -0
- package/dist/plugins/deprecate-stray-node-id-fields-plugin.d.ts +14 -0
- package/dist/plugins/deprecate-stray-node-id-fields-plugin.d.ts.map +1 -0
- package/dist/plugins/deprecate-stray-node-id-fields-plugin.js +37 -0
- package/dist/plugins/deprecate-stray-node-id-fields-plugin.js.map +1 -0
- package/dist/plugins/generic-bulk-plugin-factory.d.ts +49 -0
- package/dist/plugins/generic-bulk-plugin-factory.d.ts.map +1 -0
- package/dist/plugins/generic-bulk-plugin-factory.js +181 -0
- package/dist/plugins/generic-bulk-plugin-factory.js.map +1 -0
- package/dist/plugins/graphiql-management-mode-plugin-hook.d.ts +13 -0
- package/dist/plugins/graphiql-management-mode-plugin-hook.d.ts.map +1 -0
- package/dist/plugins/graphiql-management-mode-plugin-hook.js +44 -0
- package/dist/plugins/graphiql-management-mode-plugin-hook.js.map +1 -0
- package/dist/plugins/index.d.ts +10 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +26 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/plugins/omit-from-query-root-plugin.d.ts +17 -0
- package/dist/plugins/omit-from-query-root-plugin.d.ts.map +1 -0
- package/dist/plugins/omit-from-query-root-plugin.js +43 -0
- package/dist/plugins/omit-from-query-root-plugin.js.map +1 -0
- package/dist/plugins/operations-enum-generator-plugin-factory.d.ts +15 -0
- package/dist/plugins/operations-enum-generator-plugin-factory.d.ts.map +1 -0
- package/dist/plugins/operations-enum-generator-plugin-factory.js +108 -0
- package/dist/plugins/operations-enum-generator-plugin-factory.js.map +1 -0
- package/dist/plugins/subscriptions-plugin-factory.d.ts +9 -0
- package/dist/plugins/subscriptions-plugin-factory.d.ts.map +1 -0
- package/dist/plugins/subscriptions-plugin-factory.js +67 -0
- package/dist/plugins/subscriptions-plugin-factory.js.map +1 -0
- package/dist/plugins/validation-directives-plugin.d.ts +6 -0
- package/dist/plugins/validation-directives-plugin.d.ts.map +1 -0
- package/dist/plugins/validation-directives-plugin.js +117 -0
- package/dist/plugins/validation-directives-plugin.js.map +1 -0
- package/dist/postgraphile/enhance-graphql-errors.d.ts +48 -0
- package/dist/postgraphile/enhance-graphql-errors.d.ts.map +1 -0
- package/dist/postgraphile/enhance-graphql-errors.js +67 -0
- package/dist/postgraphile/enhance-graphql-errors.js.map +1 -0
- package/dist/postgraphile/index.d.ts +4 -0
- package/dist/postgraphile/index.d.ts.map +1 -0
- package/dist/postgraphile/index.js +20 -0
- package/dist/postgraphile/index.js.map +1 -0
- package/dist/postgraphile/postgraphile-options-builder.d.ts +273 -0
- package/dist/postgraphile/postgraphile-options-builder.d.ts.map +1 -0
- package/dist/postgraphile/postgraphile-options-builder.js +419 -0
- package/dist/postgraphile/postgraphile-options-builder.js.map +1 -0
- package/dist/postgraphile/websocket-utils.d.ts +11 -0
- package/dist/postgraphile/websocket-utils.d.ts.map +1 -0
- package/dist/postgraphile/websocket-utils.js +17 -0
- package/dist/postgraphile/websocket-utils.js.map +1 -0
- package/package.json +61 -0
- package/src/common/checks.ts +23 -0
- package/src/common/index.ts +2 -0
- package/src/common/types.ts +15 -0
- package/src/index.ts +3 -0
- package/src/plugins/add-error-codes-enum-plugin.ts +102 -0
- package/src/plugins/annotate-types-with-permissions-plugin.spec.ts +158 -0
- package/src/plugins/annotate-types-with-permissions-plugin.ts +205 -0
- package/src/plugins/deprecate-stray-node-id-fields-plugin.ts +41 -0
- package/src/plugins/generic-bulk-plugin-factory.ts +313 -0
- package/src/plugins/graphiql-management-mode-plugin-hook.ts +46 -0
- package/src/plugins/index.ts +9 -0
- package/src/plugins/omit-from-query-root-plugin.ts +69 -0
- package/src/plugins/operations-enum-generator-plugin-factory.ts +130 -0
- package/src/plugins/subscriptions-plugin-factory.ts +114 -0
- package/src/plugins/validation-directives-plugin.ts +141 -0
- package/src/postgraphile/enhance-graphql-errors.spec.ts +241 -0
- package/src/postgraphile/enhance-graphql-errors.ts +138 -0
- package/src/postgraphile/index.ts +3 -0
- package/src/postgraphile/postgraphile-options-builder.spec.ts +744 -0
- package/src/postgraphile/postgraphile-options-builder.ts +510 -0
- package/src/postgraphile/websocket-utils.ts +19 -0
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import PgPubsub from '@graphile/pg-pubsub';
|
|
3
|
+
import { Request, Response } from 'express';
|
|
4
|
+
import { NodePlugin, Plugin } from 'graphile-build';
|
|
5
|
+
import { GraphQLError } from 'graphql';
|
|
6
|
+
import { IncomingMessage } from 'http';
|
|
7
|
+
import {
|
|
8
|
+
GraphQLErrorExtended,
|
|
9
|
+
Middleware,
|
|
10
|
+
Options,
|
|
11
|
+
PostGraphileOptions,
|
|
12
|
+
PostGraphilePlugin,
|
|
13
|
+
makePluginHook,
|
|
14
|
+
mixed,
|
|
15
|
+
} from 'postgraphile';
|
|
16
|
+
import { Dict } from '../common';
|
|
17
|
+
import { enhanceGraphqlErrors } from './enhance-graphql-errors';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* This is the function type that must be used when developing Hook Plugin factory functions.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
const subscriptionAuthorizationHookFactory = (
|
|
25
|
+
buildOptions: PostGraphileOptions<Request, Response> | undefined,
|
|
26
|
+
): PostGraphilePlugin => {
|
|
27
|
+
return {
|
|
28
|
+
// returns a PostGraphilePlugin
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export type HookPluginFactory = (
|
|
34
|
+
buildOptions: PostGraphileOptions<Request, Response> | undefined,
|
|
35
|
+
) => PostGraphilePlugin;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A builder class that allows construction of postgraphile options using multiple dedicated extension methods.
|
|
39
|
+
*/
|
|
40
|
+
export class PostgraphileOptionsBuilder {
|
|
41
|
+
private transformers: ((
|
|
42
|
+
options: PostGraphileOptions<Request, Response>,
|
|
43
|
+
) => PostGraphileOptions<Request, Response>)[] = [];
|
|
44
|
+
|
|
45
|
+
private headers: Dict<number | string | string[]> = {};
|
|
46
|
+
private pluginHooks: Set<PostGraphilePlugin> = new Set<PostGraphilePlugin>();
|
|
47
|
+
private hookFuncs: Set<HookPluginFactory> = new Set<HookPluginFactory>();
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Builds a postgraphile options object with previously used extension methods.
|
|
51
|
+
*/
|
|
52
|
+
public build(): PostGraphileOptions<Request, Response> {
|
|
53
|
+
let options: PostGraphileOptions<Request, Response> = {};
|
|
54
|
+
for (const transform of this.transformers) {
|
|
55
|
+
options = transform(options);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
this.buildHeadersHookPlugin();
|
|
59
|
+
|
|
60
|
+
// Build plugin hooks from hook functions
|
|
61
|
+
[...this.hookFuncs].map((hookFunc) => {
|
|
62
|
+
this.pluginHooks.add(hookFunc(options));
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
if (this.pluginHooks.size > 0) {
|
|
66
|
+
options.pluginHook = makePluginHook([...this.pluginHooks]);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return options;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Builds a hook plugin that adds custom headers to http responses.
|
|
74
|
+
*/
|
|
75
|
+
private buildHeadersHookPlugin(): void {
|
|
76
|
+
if (Object.keys(this.headers).length > 0) {
|
|
77
|
+
this.pluginHooks.add({
|
|
78
|
+
'postgraphile:http:handler': (
|
|
79
|
+
req: IncomingMessage,
|
|
80
|
+
{ res }: { res: Response },
|
|
81
|
+
) => {
|
|
82
|
+
for (const key in this.headers) {
|
|
83
|
+
if (Object.prototype.hasOwnProperty.call(this.headers, key)) {
|
|
84
|
+
res.setHeader(key, this.headers[key]);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return req;
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* This builder option must be used if the Hook Plugin requires PostGraphileOptions in the logic.
|
|
95
|
+
* A function which returns a PostGraphilePlugin should be passed into this method.
|
|
96
|
+
* That function will use PostGraphileOptions created in the build() method and attach the plugin returned from the function as a Postgraphile Hook.
|
|
97
|
+
* @example
|
|
98
|
+
*
|
|
99
|
+
* ```ts
|
|
100
|
+
new PostgraphileOptionsBuilder()
|
|
101
|
+
.addHookPluginFactory(customHookFactory)
|
|
102
|
+
* ```
|
|
103
|
+
*
|
|
104
|
+
* @param hookFunction This is a function which takes an argument of type PostGraphileOptions<Request, Response> and returns a PostGraphilePlugin object.
|
|
105
|
+
* @returns
|
|
106
|
+
*/
|
|
107
|
+
public addHookPluginFactory(
|
|
108
|
+
hookFunction: HookPluginFactory,
|
|
109
|
+
): PostgraphileOptionsBuilder {
|
|
110
|
+
this.hookFuncs.add(hookFunction);
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* This function takes a PostGraphilePlugin object and attaches it as a Postgraphile Hook when build() is called.
|
|
116
|
+
* @example
|
|
117
|
+
*
|
|
118
|
+
* ```ts
|
|
119
|
+
new PostgraphileOptionsBuilder()
|
|
120
|
+
.addHookPluginFunction(subscriptionAuthorizationHook)
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* @param hookPlugin This is a PostGraphilePlugin object.
|
|
124
|
+
* @returns
|
|
125
|
+
*/
|
|
126
|
+
public addHookPlugin(
|
|
127
|
+
hookPlugin: PostGraphilePlugin,
|
|
128
|
+
): PostgraphileOptionsBuilder {
|
|
129
|
+
this.pluginHooks.add(hookPlugin);
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Sets `pgSettings` property of postgraphile options object.
|
|
135
|
+
* Example:
|
|
136
|
+
*
|
|
137
|
+
* ```ts
|
|
138
|
+
new PostgraphileOptionsBuilder()
|
|
139
|
+
.setPgSettings(async req => {
|
|
140
|
+
const { subject } = await getManagementAuthenticationContext(req, idServiceParams);
|
|
141
|
+
return buildPgSettings(subject, config.dbGqlRole, config.serviceId);
|
|
142
|
+
})
|
|
143
|
+
* ```
|
|
144
|
+
*
|
|
145
|
+
* @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).
|
|
146
|
+
*/
|
|
147
|
+
public setPgSettings(
|
|
148
|
+
pgSettings: (req: Request) => Promise<Dict<mixed>> | Dict<mixed>,
|
|
149
|
+
): PostgraphileOptionsBuilder {
|
|
150
|
+
this.transformers.push((options) => {
|
|
151
|
+
options.pgSettings = pgSettings;
|
|
152
|
+
return options;
|
|
153
|
+
});
|
|
154
|
+
return this;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Sets `handleErrors` property of postgraphile options object.
|
|
159
|
+
* Example:
|
|
160
|
+
*
|
|
161
|
+
* ```ts
|
|
162
|
+
new PostgraphileOptionsBuilder()
|
|
163
|
+
.setErrorsHandler((errors, req) => {
|
|
164
|
+
return enhanceGraphqlErrors(
|
|
165
|
+
result.errors,
|
|
166
|
+
req.body.operationName,
|
|
167
|
+
customizeGraphQlErrorFields(defaultPgErrorMapper),
|
|
168
|
+
logGraphQlError(defaultWriteLogMapper, this.logger),
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
* ```
|
|
173
|
+
*
|
|
174
|
+
* @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.
|
|
175
|
+
*/
|
|
176
|
+
public setErrorsHandler(
|
|
177
|
+
handleErrors: (
|
|
178
|
+
errors: readonly GraphQLError[],
|
|
179
|
+
req: Request,
|
|
180
|
+
res: Response,
|
|
181
|
+
) => GraphQLErrorExtended[],
|
|
182
|
+
): PostgraphileOptionsBuilder {
|
|
183
|
+
this.transformers.push((options) => {
|
|
184
|
+
options.handleErrors = handleErrors;
|
|
185
|
+
return options;
|
|
186
|
+
});
|
|
187
|
+
return this;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Sets properties of postgraphile options object to be used in without any conditions. Additional calls will override and add properties.
|
|
192
|
+
* Example:
|
|
193
|
+
*
|
|
194
|
+
* ```ts
|
|
195
|
+
new PostgraphileOptionsBuilder()
|
|
196
|
+
.setProperties({
|
|
197
|
+
enableCors: true,
|
|
198
|
+
dynamicJson: true,
|
|
199
|
+
ignoreRBAC: false,
|
|
200
|
+
legacyRelations: 'omit',
|
|
201
|
+
setofFunctionsContainNulls: false,
|
|
202
|
+
})
|
|
203
|
+
* ```
|
|
204
|
+
* @param properties An object containing postgraphile options.
|
|
205
|
+
*/
|
|
206
|
+
public setProperties(
|
|
207
|
+
properties: PostGraphileOptions<Request, Response>,
|
|
208
|
+
): PostgraphileOptionsBuilder {
|
|
209
|
+
this.transformers.push((options) => {
|
|
210
|
+
return { ...options, ...properties };
|
|
211
|
+
});
|
|
212
|
+
return this;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Sets properties of postgraphile options object to be used based on some
|
|
217
|
+
* condition (use if true, omit if false). Additional calls will override and
|
|
218
|
+
* add properties.
|
|
219
|
+
* Example:
|
|
220
|
+
*
|
|
221
|
+
* ```ts
|
|
222
|
+
new PostgraphileOptionsBuilder()
|
|
223
|
+
.setConditionalProperties(config.isDev, {
|
|
224
|
+
exportGqlSchemaPath: './src/generated/schema.graphql',
|
|
225
|
+
watchPg: true,
|
|
226
|
+
graphiql: true,
|
|
227
|
+
enhanceGraphiql: true,
|
|
228
|
+
allowExplain: true,
|
|
229
|
+
})
|
|
230
|
+
* ```
|
|
231
|
+
* @param properties An object containing postgraphile options.
|
|
232
|
+
*/
|
|
233
|
+
public setConditionalProperties(
|
|
234
|
+
condition: boolean,
|
|
235
|
+
properties: PostGraphileOptions<Request, Response>,
|
|
236
|
+
): PostgraphileOptionsBuilder {
|
|
237
|
+
if (condition) {
|
|
238
|
+
this.transformers.push((options) => {
|
|
239
|
+
return { ...options, ...properties };
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
return this;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Sets all necessary postgraphile properties to enable subscriptions.
|
|
247
|
+
* Example:
|
|
248
|
+
*
|
|
249
|
+
* ```ts
|
|
250
|
+
new PostgraphileOptionsBuilder()
|
|
251
|
+
.enableSubscriptions({
|
|
252
|
+
plugin: SubscriptionsPlugin,
|
|
253
|
+
websocketMiddlewares: middlewares,
|
|
254
|
+
hookFactory: subscriptionAuthorizationHookFactory
|
|
255
|
+
})
|
|
256
|
+
* ```
|
|
257
|
+
*
|
|
258
|
+
* Results in something like this:
|
|
259
|
+
*
|
|
260
|
+
* ```ts
|
|
261
|
+
import PgPubsub from '@graphile/pg-pubsub';
|
|
262
|
+
import { makePluginHook } from 'postgraphile';
|
|
263
|
+
//.........
|
|
264
|
+
{
|
|
265
|
+
appendPlugins: [SubscriptionsPlugin], // Other plugins will be included if they were added using other methods
|
|
266
|
+
pluginHook: makePluginHook([PgPubsub, subscriptionAuthorizationHook]),
|
|
267
|
+
subscriptions: true,
|
|
268
|
+
websocketMiddlewares, // Empty array if websocketMiddlewares are not specified
|
|
269
|
+
}
|
|
270
|
+
* ```
|
|
271
|
+
*
|
|
272
|
+
* To make sure that Postgraphile is able to correctly establish WebSockets
|
|
273
|
+
* connection, additional changes should be done by using
|
|
274
|
+
* `enhanceHttpServerWithSubscriptions`.
|
|
275
|
+
*
|
|
276
|
+
* See more here: https://www.graphile.org/postgraphile/subscriptions/#advanced-setup
|
|
277
|
+
*
|
|
278
|
+
* @param plugin Graphile engine plugin that adds one or multiple subscription
|
|
279
|
+
* endpoints. Can be added separately.
|
|
280
|
+
* @param websocketMiddlewares If you're using websockets (subscriptions ||
|
|
281
|
+
* live) then you may want to authenticate your users using sessions or
|
|
282
|
+
* similar. You can pass some simple middlewares here that will be executed
|
|
283
|
+
* against the websocket connection in order to perform authentication.
|
|
284
|
+
* @param hookFactory A factory function for a Postgraphile hook plugin.
|
|
285
|
+
*/
|
|
286
|
+
public enableSubscriptions({
|
|
287
|
+
plugin,
|
|
288
|
+
websocketMiddlewares,
|
|
289
|
+
hookFactory,
|
|
290
|
+
}: {
|
|
291
|
+
plugin: Plugin;
|
|
292
|
+
websocketMiddlewares?: Middleware<Request, Response>[];
|
|
293
|
+
hookFactory?: HookPluginFactory;
|
|
294
|
+
}): PostgraphileOptionsBuilder {
|
|
295
|
+
this.transformers.push((options) => {
|
|
296
|
+
options.subscriptions = true;
|
|
297
|
+
options.appendPlugins = [
|
|
298
|
+
...new Set<Plugin>([...(options.appendPlugins ?? []), plugin]),
|
|
299
|
+
];
|
|
300
|
+
|
|
301
|
+
// Add the pub/sub realtime provider
|
|
302
|
+
// This is for PostGraphile server plugins: https://www.graphile.org/postgraphile/plugins/
|
|
303
|
+
this.pluginHooks.add(PgPubsub);
|
|
304
|
+
|
|
305
|
+
if (websocketMiddlewares) {
|
|
306
|
+
options.websocketMiddlewares = [
|
|
307
|
+
...new Set<Middleware<Request, Response>>([
|
|
308
|
+
...(options.websocketMiddlewares ?? []),
|
|
309
|
+
...websocketMiddlewares,
|
|
310
|
+
]),
|
|
311
|
+
];
|
|
312
|
+
} else if (!options.websocketMiddlewares) {
|
|
313
|
+
options.websocketMiddlewares = [];
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (hookFactory) {
|
|
317
|
+
this.hookFuncs.add(hookFactory);
|
|
318
|
+
}
|
|
319
|
+
return options;
|
|
320
|
+
});
|
|
321
|
+
return this;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* 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.
|
|
326
|
+
* Example:
|
|
327
|
+
*
|
|
328
|
+
```ts
|
|
329
|
+
new PostgraphileOptionsBuilder()
|
|
330
|
+
.addPlugins(
|
|
331
|
+
PgSimplifyInflectorPlugin,
|
|
332
|
+
ScalarTypesPlugin,
|
|
333
|
+
SmartTagsPlugin,
|
|
334
|
+
ValidationDirectivesPlugin,
|
|
335
|
+
)
|
|
336
|
+
```
|
|
337
|
+
* @param plugins Comma-separated list of Graphile Engine schema plugins to load after the default plugins.
|
|
338
|
+
*/
|
|
339
|
+
public addPlugins(...plugins: Plugin[]): PostgraphileOptionsBuilder {
|
|
340
|
+
this.transformers.push((options) => {
|
|
341
|
+
options.appendPlugins = [
|
|
342
|
+
...new Set<Plugin>([...(options.appendPlugins ?? []), ...plugins]),
|
|
343
|
+
];
|
|
344
|
+
return options;
|
|
345
|
+
});
|
|
346
|
+
return this;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Adds multiple graphile engine plugins to `appendPlugins` array of postgraphile options object.
|
|
351
|
+
* `setAdditionalGraphQLContextFromRequest` and `addGraphileBuildOptions` methods can be used to pass parameters to said plugins.
|
|
352
|
+
* Explicit condition parameter is used to make a decision if plugins should be used or not (use if true, omit if false).
|
|
353
|
+
* Example:
|
|
354
|
+
*
|
|
355
|
+
```ts
|
|
356
|
+
new PostgraphileOptionsBuilder()
|
|
357
|
+
.addConditionalPlugins(config.isDev || config.enablePopulatePlugins, PopulateMoviesPlugin, PopulateTvshowsPlugin)
|
|
358
|
+
```
|
|
359
|
+
* @param plugins Comma-separated list of Graphile Engine schema plugins to load after the default plugins.
|
|
360
|
+
*/
|
|
361
|
+
public addConditionalPlugins(
|
|
362
|
+
condition: boolean,
|
|
363
|
+
...plugins: Plugin[]
|
|
364
|
+
): PostgraphileOptionsBuilder {
|
|
365
|
+
if (condition) {
|
|
366
|
+
this.addPlugins(...plugins);
|
|
367
|
+
}
|
|
368
|
+
return this;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Skips multiple default postgraphile plugins by adding them to `skipPlugins` array of postgraphile options object .
|
|
373
|
+
* Example:
|
|
374
|
+
*
|
|
375
|
+
```ts
|
|
376
|
+
new PostgraphileOptionsBuilder()
|
|
377
|
+
.skipPlugins(NodePlugin, MutationPayloadQueryPlugin)
|
|
378
|
+
```
|
|
379
|
+
* @param plugins Comma-separated list of Graphile Engine schema plugins to skip.
|
|
380
|
+
*/
|
|
381
|
+
public skipPlugins(...plugins: Plugin[]): PostgraphileOptionsBuilder {
|
|
382
|
+
this.transformers.push((options) => {
|
|
383
|
+
options.skipPlugins = [
|
|
384
|
+
...new Set<Plugin>([...(options.skipPlugins ?? []), ...plugins]),
|
|
385
|
+
];
|
|
386
|
+
return options;
|
|
387
|
+
});
|
|
388
|
+
return this;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Adds properties to resulting `graphileBuildOptions` property of postgraphile options object. Useful for modifying default postgraphile plugin settings.
|
|
393
|
+
* Example:
|
|
394
|
+
*
|
|
395
|
+
```ts
|
|
396
|
+
new PostgraphileOptionsBuilder()
|
|
397
|
+
.addGraphileBuildOptions({ pgSkipInstallingWatchFixtures: true })
|
|
398
|
+
```
|
|
399
|
+
* @param graphileBuildOptions Additional Options to pass through into the graphile schema building system (received via the second argument of a plugin).
|
|
400
|
+
*/
|
|
401
|
+
public addGraphileBuildOptions(
|
|
402
|
+
graphileBuildOptions: Partial<Options>,
|
|
403
|
+
): PostgraphileOptionsBuilder {
|
|
404
|
+
this.transformers.push((options) => {
|
|
405
|
+
options.graphileBuildOptions = {
|
|
406
|
+
...(options.graphileBuildOptions ?? {}),
|
|
407
|
+
...graphileBuildOptions,
|
|
408
|
+
};
|
|
409
|
+
return options;
|
|
410
|
+
});
|
|
411
|
+
return this;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Adds `additionalGraphQLContextFromRequest` property to postgraphile options object. Define only once. Multiple calls will not expand previous calls, but will override them.
|
|
416
|
+
*
|
|
417
|
+
* Example:
|
|
418
|
+
```ts
|
|
419
|
+
new PostgraphileOptionsBuilder()
|
|
420
|
+
.setAdditionalGraphQLContextFromRequest(async req => {
|
|
421
|
+
const { subject } = await getManagementAuthenticationContext(req);
|
|
422
|
+
return { subject, serviceId: config.serviceId };
|
|
423
|
+
})
|
|
424
|
+
```
|
|
425
|
+
* @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.
|
|
426
|
+
*/
|
|
427
|
+
public setAdditionalGraphQLContextFromRequest(
|
|
428
|
+
callback: (req: Request, res: Response) => Promise<Record<string, any>>,
|
|
429
|
+
): PostgraphileOptionsBuilder {
|
|
430
|
+
this.transformers.push((options) => {
|
|
431
|
+
options.additionalGraphQLContextFromRequest = callback;
|
|
432
|
+
return options;
|
|
433
|
+
});
|
|
434
|
+
return this;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Enables or disables graphiql GUI depending on a passed condition parameter. Enable if true, disable if false.
|
|
439
|
+
* Sets 3 postgraphile properties: `graphiql`, `enhanceGraphiql` and `allowExplain`.
|
|
440
|
+
* Example:
|
|
441
|
+
*
|
|
442
|
+
* ```ts
|
|
443
|
+
new PostgraphileOptionsBuilder()
|
|
444
|
+
.setGraphiql(config.graphqlGuiEnabled)
|
|
445
|
+
* ```
|
|
446
|
+
*/
|
|
447
|
+
public setGraphiql(condition: boolean): PostgraphileOptionsBuilder {
|
|
448
|
+
this.transformers.push((options) => {
|
|
449
|
+
return {
|
|
450
|
+
...options,
|
|
451
|
+
...{
|
|
452
|
+
graphiql: condition,
|
|
453
|
+
enhanceGraphiql: condition,
|
|
454
|
+
allowExplain: condition,
|
|
455
|
+
},
|
|
456
|
+
};
|
|
457
|
+
});
|
|
458
|
+
return this;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Sets a response header for each GraphQL request.
|
|
463
|
+
*
|
|
464
|
+
* @param name header name
|
|
465
|
+
* @param value header value
|
|
466
|
+
*/
|
|
467
|
+
public setHeader(
|
|
468
|
+
name: string,
|
|
469
|
+
value: string | number | string[],
|
|
470
|
+
): PostgraphileOptionsBuilder {
|
|
471
|
+
this.headers[name] = value;
|
|
472
|
+
return this;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Sets default values for properties that we see as best practices which most
|
|
477
|
+
* services should start from. This includes the default GraphQL errors handler,
|
|
478
|
+
* suggested default properties, development properties in dev-environments,
|
|
479
|
+
* and excludes the 'NodePlugin'.
|
|
480
|
+
*/
|
|
481
|
+
public setDefaultSettings(
|
|
482
|
+
isDev: boolean,
|
|
483
|
+
graphqlGuiEnabled: boolean,
|
|
484
|
+
): PostgraphileOptionsBuilder {
|
|
485
|
+
this.setErrorsHandler((errors) => {
|
|
486
|
+
return enhanceGraphqlErrors(errors);
|
|
487
|
+
})
|
|
488
|
+
.setProperties({
|
|
489
|
+
enableCors: true,
|
|
490
|
+
dynamicJson: true,
|
|
491
|
+
ignoreRBAC: false,
|
|
492
|
+
legacyRelations: 'omit',
|
|
493
|
+
setofFunctionsContainNulls: false,
|
|
494
|
+
|
|
495
|
+
watchPg: false,
|
|
496
|
+
disableQueryLog: true,
|
|
497
|
+
retryOnInitFail: true,
|
|
498
|
+
})
|
|
499
|
+
.setConditionalProperties(isDev, {
|
|
500
|
+
exportGqlSchemaPath: './src/generated/graphql/schema.graphql',
|
|
501
|
+
watchPg: true,
|
|
502
|
+
disableQueryLog: false,
|
|
503
|
+
retryOnInitFail: false,
|
|
504
|
+
})
|
|
505
|
+
.setGraphiql(graphqlGuiEnabled)
|
|
506
|
+
// removes some nodeId and byNodeId endpoints, but still leaves support for cursor pagination
|
|
507
|
+
.skipPlugins(NodePlugin);
|
|
508
|
+
return this;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Request } from 'express';
|
|
2
|
+
import { WebSocket } from 'graphql-ws';
|
|
3
|
+
|
|
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
|
+
export const getWebsocketFromRequest = (
|
|
12
|
+
req: Request,
|
|
13
|
+
): WebSocket | undefined => {
|
|
14
|
+
const websocketSymbol = Object.getOwnPropertySymbols(req.socket).filter(
|
|
15
|
+
(sym) => sym.description === 'websocket',
|
|
16
|
+
)[0];
|
|
17
|
+
const websocket = (req as any).socket[websocketSymbol];
|
|
18
|
+
return websocket as WebSocket;
|
|
19
|
+
};
|