-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathplugins.service.ts
More file actions
52 lines (47 loc) · 1.47 KB
/
plugins.service.ts
File metadata and controls
52 lines (47 loc) · 1.47 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
/* eslint-disable @typescript-eslint/no-unsafe-call */
import { ApolloPlugin } from '@apollo-annotation/common'
import { Inject, Injectable } from '@nestjs/common'
import { APOLLO_PLUGINS } from './plugins.constants.js'
@Injectable()
export class PluginsService {
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
extensionPoints = new Map<string, Function[]>()
constructor(@Inject(APOLLO_PLUGINS) private plugins: ApolloPlugin[]) {
for (const plugin of plugins) {
plugin.apolloInstall({
addToExtensionPoint: this.addToExtensionPoint.bind(this),
})
}
}
addToExtensionPoint<T>(
extensionPointName: string,
callback: (extendee: T, props: Record<string, unknown>) => T,
) {
let callbacks = this.extensionPoints.get(extensionPointName)
if (!callbacks) {
callbacks = []
this.extensionPoints.set(extensionPointName, callbacks)
}
callbacks.push(callback)
}
evaluateExtensionPoint<T>(
extensionPointName: string,
extendee: T,
props?: Record<string, unknown>,
) {
const callbacks = this.extensionPoints.get(extensionPointName)
let accumulator = extendee
if (!callbacks) {
return accumulator
}
for (const callback of callbacks) {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
accumulator = callback(accumulator, props)
} catch (error) {
console.error(error)
}
}
return accumulator
}
}