graphql-modules 3.1.2-alpha-20260121023952-c6236f99446644c787494ddb1a9a973180bab70e → 3.1.2-alpha-20260121024633-8af430e29a180a1d00c50c4bf74d342b3518267c
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/cjs/application/apollo.js +81 -0
- package/cjs/application/application.js +149 -0
- package/cjs/application/context.js +144 -0
- package/cjs/application/di.js +48 -0
- package/cjs/application/execution.js +42 -0
- package/cjs/application/operation-controller.js +16 -0
- package/cjs/application/subscription.js +54 -0
- package/cjs/application/tokens.js +20 -0
- package/cjs/application/types.js +0 -0
- package/cjs/di/decorators.js +78 -0
- package/cjs/di/errors.js +87 -0
- package/cjs/di/forward-ref.js +26 -0
- package/cjs/di/index.js +19 -0
- package/cjs/di/injector.js +173 -0
- package/cjs/di/metadata.js +22 -0
- package/cjs/di/providers.js +60 -0
- package/cjs/di/registry.js +44 -0
- package/cjs/di/resolution.js +166 -0
- package/cjs/di/utils.js +44 -0
- package/cjs/index.js +31 -0
- package/cjs/module/factory.js +71 -0
- package/cjs/module/metadata.js +110 -0
- package/cjs/module/module.js +27 -0
- package/cjs/module/resolvers.js +341 -0
- package/cjs/module/tokens.js +21 -0
- package/cjs/module/type-defs.js +24 -0
- package/cjs/module/types.js +0 -0
- package/cjs/package.json +1 -0
- package/cjs/shared/di.js +0 -0
- package/cjs/shared/errors.js +82 -0
- package/cjs/shared/gql.js +12 -0
- package/cjs/shared/middleware.js +109 -0
- package/cjs/shared/types.js +0 -0
- package/cjs/shared/utils.js +115 -0
- package/cjs/testing/di.js +9 -0
- package/cjs/testing/graphql.js +10 -0
- package/cjs/testing/index.js +17 -0
- package/cjs/testing/test-application.js +65 -0
- package/cjs/testing/test-injector.js +22 -0
- package/cjs/testing/test-module.js +270 -0
- package/esm/application/apollo.js +77 -0
- package/esm/application/application.js +146 -0
- package/esm/application/context.js +140 -0
- package/esm/application/di.js +42 -0
- package/esm/application/execution.js +39 -0
- package/esm/application/operation-controller.js +13 -0
- package/esm/application/subscription.js +51 -0
- package/esm/application/tokens.js +17 -0
- package/esm/application/types.js +0 -0
- package/esm/di/decorators.js +72 -0
- package/esm/di/errors.js +79 -0
- package/esm/di/forward-ref.js +22 -0
- package/esm/di/index.js +4 -0
- package/esm/di/injector.js +168 -0
- package/esm/di/metadata.js +17 -0
- package/esm/di/providers.js +50 -0
- package/esm/di/registry.js +40 -0
- package/esm/di/resolution.js +159 -0
- package/esm/di/utils.js +36 -0
- package/esm/index.js +16 -0
- package/esm/module/factory.js +68 -0
- package/esm/module/metadata.js +107 -0
- package/esm/module/module.js +24 -0
- package/esm/module/resolvers.js +337 -0
- package/esm/module/tokens.js +18 -0
- package/esm/module/type-defs.js +21 -0
- package/esm/module/types.js +0 -0
- package/esm/shared/di.js +0 -0
- package/esm/shared/errors.js +69 -0
- package/esm/shared/gql.js +9 -0
- package/esm/shared/middleware.js +103 -0
- package/esm/shared/types.js +0 -0
- package/esm/shared/utils.js +101 -0
- package/esm/testing/di.js +6 -0
- package/esm/testing/graphql.js +7 -0
- package/esm/testing/index.js +14 -0
- package/esm/testing/test-application.js +62 -0
- package/esm/testing/test-injector.js +18 -0
- package/esm/testing/test-module.js +266 -0
- package/package.json +29 -9
- package/typings/application/apollo.d.ts +22 -0
- package/typings/application/application.d.ts +32 -0
- package/typings/application/context.d.ts +24 -0
- package/typings/application/di.d.ts +22 -0
- package/typings/application/execution.d.ts +8 -0
- package/typings/application/operation-controller.d.ts +5 -0
- package/typings/application/subscription.d.ts +8 -0
- package/typings/application/tokens.d.ts +17 -0
- package/typings/application/types.d.ts +130 -0
- package/typings/di/decorators.d.ts +11 -0
- package/typings/di/errors.d.ts +16 -0
- package/typings/di/forward-ref.d.ts +7 -0
- package/typings/di/index.d.ts +5 -0
- package/typings/di/injector.d.ts +50 -0
- package/typings/di/metadata.d.ts +12 -0
- package/typings/di/providers.d.ts +44 -0
- package/typings/di/registry.d.ts +11 -0
- package/typings/di/resolution.d.ts +63 -0
- package/typings/di/utils.d.ts +8 -0
- package/typings/index.d.ts +13 -0
- package/typings/module/factory.d.ts +16 -0
- package/typings/module/metadata.d.ts +12 -0
- package/typings/module/module.d.ts +22 -0
- package/typings/module/resolvers.d.ts +13 -0
- package/typings/module/tokens.d.ts +18 -0
- package/typings/module/type-defs.d.ts +7 -0
- package/typings/module/types.d.ts +51 -0
- package/typings/shared/di.d.ts +3 -0
- package/typings/shared/errors.d.ts +36 -0
- package/typings/shared/gql.d.ts +2 -0
- package/typings/shared/middleware.d.ts +21 -0
- package/typings/shared/types.d.ts +22 -0
- package/typings/shared/utils.d.ts +12 -0
- package/typings/testing/di.d.ts +2 -0
- package/typings/testing/graphql.d.ts +14 -0
- package/typings/testing/index.d.ts +14 -0
- package/typings/testing/test-application.d.ts +2 -0
- package/typings/testing/test-injector.d.ts +4 -0
- package/typings/testing/test-module.d.ts +10 -0
- package/LICENSE.md +0 -21
- package/index.js +0 -2359
- package/index.mjs +0 -2344
- /package/{application/apollo.d.ts → typings/application/apollo.d.cts} +0 -0
- /package/{application/application.d.ts → typings/application/application.d.cts} +0 -0
- /package/{application/context.d.ts → typings/application/context.d.cts} +0 -0
- /package/{application/di.d.ts → typings/application/di.d.cts} +0 -0
- /package/{application/execution.d.ts → typings/application/execution.d.cts} +0 -0
- /package/{application/operation-controller.d.ts → typings/application/operation-controller.d.cts} +0 -0
- /package/{application/subscription.d.ts → typings/application/subscription.d.cts} +0 -0
- /package/{application/tokens.d.ts → typings/application/tokens.d.cts} +0 -0
- /package/{application/types.d.ts → typings/application/types.d.cts} +0 -0
- /package/{di/decorators.d.ts → typings/di/decorators.d.cts} +0 -0
- /package/{di/errors.d.ts → typings/di/errors.d.cts} +0 -0
- /package/{di/forward-ref.d.ts → typings/di/forward-ref.d.cts} +0 -0
- /package/{di/index.d.ts → typings/di/index.d.cts} +0 -0
- /package/{di/injector.d.ts → typings/di/injector.d.cts} +0 -0
- /package/{di/metadata.d.ts → typings/di/metadata.d.cts} +0 -0
- /package/{di/providers.d.ts → typings/di/providers.d.cts} +0 -0
- /package/{di/registry.d.ts → typings/di/registry.d.cts} +0 -0
- /package/{di/resolution.d.ts → typings/di/resolution.d.cts} +0 -0
- /package/{di/utils.d.ts → typings/di/utils.d.cts} +0 -0
- /package/{index.d.ts → typings/index.d.cts} +0 -0
- /package/{module/factory.d.ts → typings/module/factory.d.cts} +0 -0
- /package/{module/metadata.d.ts → typings/module/metadata.d.cts} +0 -0
- /package/{module/module.d.ts → typings/module/module.d.cts} +0 -0
- /package/{module/resolvers.d.ts → typings/module/resolvers.d.cts} +0 -0
- /package/{module/tokens.d.ts → typings/module/tokens.d.cts} +0 -0
- /package/{module/type-defs.d.ts → typings/module/type-defs.d.cts} +0 -0
- /package/{module/types.d.ts → typings/module/types.d.cts} +0 -0
- /package/{shared/di.d.ts → typings/shared/di.d.cts} +0 -0
- /package/{shared/errors.d.ts → typings/shared/errors.d.cts} +0 -0
- /package/{shared/gql.d.ts → typings/shared/gql.d.cts} +0 -0
- /package/{shared/middleware.d.ts → typings/shared/middleware.d.cts} +0 -0
- /package/{shared/types.d.ts → typings/shared/types.d.cts} +0 -0
- /package/{shared/utils.d.ts → typings/shared/utils.d.cts} +0 -0
- /package/{testing/di.d.ts → typings/testing/di.d.cts} +0 -0
- /package/{testing/graphql.d.ts → typings/testing/graphql.d.cts} +0 -0
- /package/{testing/index.d.ts → typings/testing/index.d.cts} +0 -0
- /package/{testing/test-application.d.ts → typings/testing/test-application.d.cts} +0 -0
- /package/{testing/test-injector.d.ts → typings/testing/test-injector.d.cts} +0 -0
- /package/{testing/test-module.d.ts → typings/testing/test-module.d.cts} +0 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { wrapSchema } from '@graphql-tools/wrap';
|
|
2
|
+
import { execute } from 'graphql';
|
|
3
|
+
import { uniqueId } from '../shared/utils';
|
|
4
|
+
const CONTEXT_ID = Symbol.for('context-id');
|
|
5
|
+
export function apolloExecutorCreator({ createExecution, }) {
|
|
6
|
+
return function createApolloExecutor(options) {
|
|
7
|
+
const executor = createExecution(options);
|
|
8
|
+
return async function executorAdapter(requestContext) {
|
|
9
|
+
return executor({
|
|
10
|
+
schema: requestContext.schema,
|
|
11
|
+
document: requestContext.document,
|
|
12
|
+
operationName: requestContext.operationName,
|
|
13
|
+
variableValues: requestContext.request.variables,
|
|
14
|
+
contextValue: requestContext.context,
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export function apolloSchemaCreator({ createSubscription, contextBuilder, schema, }) {
|
|
20
|
+
const createApolloSchema = () => {
|
|
21
|
+
const sessions = {};
|
|
22
|
+
const subscription = createSubscription();
|
|
23
|
+
function getSession(ctx, { context, ɵdestroy: destroy }) {
|
|
24
|
+
if (!ctx[CONTEXT_ID]) {
|
|
25
|
+
ctx[CONTEXT_ID] = uniqueId((id) => !sessions[id]);
|
|
26
|
+
sessions[ctx[CONTEXT_ID]] = {
|
|
27
|
+
count: 0,
|
|
28
|
+
session: {
|
|
29
|
+
context,
|
|
30
|
+
destroy() {
|
|
31
|
+
if (--sessions[ctx[CONTEXT_ID]].count === 0) {
|
|
32
|
+
destroy();
|
|
33
|
+
delete sessions[ctx[CONTEXT_ID]];
|
|
34
|
+
delete ctx[CONTEXT_ID];
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
sessions[ctx[CONTEXT_ID]].count++;
|
|
41
|
+
return sessions[ctx[CONTEXT_ID]].session;
|
|
42
|
+
}
|
|
43
|
+
return wrapSchema({
|
|
44
|
+
schema,
|
|
45
|
+
batch: true,
|
|
46
|
+
executor(input) {
|
|
47
|
+
if (input.operationType === 'subscription') {
|
|
48
|
+
return subscription({
|
|
49
|
+
schema,
|
|
50
|
+
document: input.document,
|
|
51
|
+
variableValues: input.variables,
|
|
52
|
+
contextValue: input.context,
|
|
53
|
+
rootValue: input.rootValue,
|
|
54
|
+
operationName: input.operationName,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
// Create an execution context and run within it
|
|
58
|
+
return contextBuilder(input.context).runWithContext((env) => {
|
|
59
|
+
const { context, destroy } = getSession(input.context, env);
|
|
60
|
+
// It's important to wrap the executeFn within a promise
|
|
61
|
+
// so we can easily control the end of execution (with finally)
|
|
62
|
+
return Promise.resolve()
|
|
63
|
+
.then(() => execute({
|
|
64
|
+
schema,
|
|
65
|
+
document: input.document,
|
|
66
|
+
contextValue: context,
|
|
67
|
+
variableValues: input.variables,
|
|
68
|
+
rootValue: input.rootValue,
|
|
69
|
+
operationName: input.operationName,
|
|
70
|
+
}))
|
|
71
|
+
.finally(destroy);
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
return createApolloSchema;
|
|
77
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { makeExecutableSchema } from '@graphql-tools/schema';
|
|
2
|
+
import { ReflectiveInjector, onlySingletonProviders, onlyOperationProviders, Scope, } from '../di';
|
|
3
|
+
import { ModuleDuplicatedError, ModuleNonUniqueIdError, } from '../shared/errors';
|
|
4
|
+
import { flatten, isDefined } from '../shared/utils';
|
|
5
|
+
import { createGlobalProvidersMap, attachGlobalProvidersMap, instantiateSingletonProviders, } from './di';
|
|
6
|
+
import { createContextBuilder } from './context';
|
|
7
|
+
import { executionCreator } from './execution';
|
|
8
|
+
import { subscriptionCreator } from './subscription';
|
|
9
|
+
import { apolloSchemaCreator, apolloExecutorCreator } from './apollo';
|
|
10
|
+
import { operationControllerCreator } from './operation-controller';
|
|
11
|
+
/**
|
|
12
|
+
* @api
|
|
13
|
+
* Creates Application out of Modules. Accepts `ApplicationConfig`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
*
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { createApplication } from 'graphql-modules';
|
|
19
|
+
* import { usersModule } from './users';
|
|
20
|
+
* import { postsModule } from './posts';
|
|
21
|
+
* import { commentsModule } from './comments';
|
|
22
|
+
*
|
|
23
|
+
* const app = createApplication({
|
|
24
|
+
* modules: [
|
|
25
|
+
* usersModule,
|
|
26
|
+
* postsModule,
|
|
27
|
+
* commentsModule
|
|
28
|
+
* ]
|
|
29
|
+
* })
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export function createApplication(applicationConfig) {
|
|
33
|
+
function applicationFactory(cfg) {
|
|
34
|
+
const config = cfg || applicationConfig;
|
|
35
|
+
const providers = config.providers && typeof config.providers === 'function'
|
|
36
|
+
? config.providers()
|
|
37
|
+
: config.providers;
|
|
38
|
+
// Creates an Injector with singleton classes at application level
|
|
39
|
+
const appSingletonProviders = ReflectiveInjector.resolve(onlySingletonProviders(providers));
|
|
40
|
+
const appInjector = ReflectiveInjector.createFromResolved({
|
|
41
|
+
name: 'App (Singleton Scope)',
|
|
42
|
+
providers: appSingletonProviders,
|
|
43
|
+
});
|
|
44
|
+
// Filter Operation-scoped providers, and keep it here
|
|
45
|
+
// so we don't do it over and over again
|
|
46
|
+
const appOperationProviders = ReflectiveInjector.resolve(onlyOperationProviders(providers));
|
|
47
|
+
const middlewareMap = config.middlewares || {};
|
|
48
|
+
// Validations
|
|
49
|
+
ensureModuleUniqueIds(config.modules);
|
|
50
|
+
// Create all modules
|
|
51
|
+
const modules = config.modules.map((mod) => mod.factory({
|
|
52
|
+
injector: appInjector,
|
|
53
|
+
middlewares: middlewareMap,
|
|
54
|
+
}));
|
|
55
|
+
const modulesMap = createModulesMap(modules);
|
|
56
|
+
const singletonGlobalProvidersMap = createGlobalProvidersMap({
|
|
57
|
+
modules,
|
|
58
|
+
scope: Scope.Singleton,
|
|
59
|
+
});
|
|
60
|
+
const operationGlobalProvidersMap = createGlobalProvidersMap({
|
|
61
|
+
modules,
|
|
62
|
+
scope: Scope.Operation,
|
|
63
|
+
});
|
|
64
|
+
attachGlobalProvidersMap({
|
|
65
|
+
injector: appInjector,
|
|
66
|
+
globalProvidersMap: singletonGlobalProvidersMap,
|
|
67
|
+
moduleInjectorGetter(moduleId) {
|
|
68
|
+
return modulesMap.get(moduleId).injector;
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
// Creating a schema, flattening the typedefs and resolvers
|
|
72
|
+
// is not expensive since it happens only once
|
|
73
|
+
const typeDefs = flatten(modules.map((mod) => mod.typeDefs));
|
|
74
|
+
const resolvers = modules.map((mod) => mod.resolvers).filter(isDefined);
|
|
75
|
+
const schema = (applicationConfig.schemaBuilder || makeExecutableSchema)({
|
|
76
|
+
typeDefs,
|
|
77
|
+
resolvers,
|
|
78
|
+
});
|
|
79
|
+
const contextBuilder = createContextBuilder({
|
|
80
|
+
appInjector,
|
|
81
|
+
appLevelOperationProviders: appOperationProviders,
|
|
82
|
+
modulesMap: modulesMap,
|
|
83
|
+
singletonGlobalProvidersMap,
|
|
84
|
+
operationGlobalProvidersMap,
|
|
85
|
+
});
|
|
86
|
+
const createOperationController = operationControllerCreator({
|
|
87
|
+
contextBuilder,
|
|
88
|
+
});
|
|
89
|
+
const createSubscription = subscriptionCreator({ contextBuilder });
|
|
90
|
+
const createExecution = executionCreator({ contextBuilder });
|
|
91
|
+
const createSchemaForApollo = apolloSchemaCreator({
|
|
92
|
+
createSubscription,
|
|
93
|
+
contextBuilder,
|
|
94
|
+
schema,
|
|
95
|
+
});
|
|
96
|
+
const createApolloExecutor = apolloExecutorCreator({
|
|
97
|
+
createExecution,
|
|
98
|
+
});
|
|
99
|
+
instantiateSingletonProviders({
|
|
100
|
+
appInjector,
|
|
101
|
+
modulesMap,
|
|
102
|
+
});
|
|
103
|
+
return {
|
|
104
|
+
typeDefs,
|
|
105
|
+
resolvers,
|
|
106
|
+
schema,
|
|
107
|
+
injector: appInjector,
|
|
108
|
+
createOperationController,
|
|
109
|
+
createSubscription,
|
|
110
|
+
createExecution,
|
|
111
|
+
createSchemaForApollo,
|
|
112
|
+
createApolloExecutor,
|
|
113
|
+
ɵfactory: applicationFactory,
|
|
114
|
+
ɵconfig: config,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
return applicationFactory();
|
|
118
|
+
}
|
|
119
|
+
function createModulesMap(modules) {
|
|
120
|
+
var _a;
|
|
121
|
+
const modulesMap = new Map();
|
|
122
|
+
for (const module of modules) {
|
|
123
|
+
if (modulesMap.has(module.id)) {
|
|
124
|
+
const location = module.metadata.dirname;
|
|
125
|
+
const existingLocation = (_a = modulesMap.get(module.id)) === null || _a === void 0 ? void 0 : _a.metadata.dirname;
|
|
126
|
+
const info = [];
|
|
127
|
+
if (existingLocation) {
|
|
128
|
+
info.push(`Already registered module located at: ${existingLocation}`);
|
|
129
|
+
}
|
|
130
|
+
if (location) {
|
|
131
|
+
info.push(`Duplicated module located at: ${location}`);
|
|
132
|
+
}
|
|
133
|
+
throw new ModuleDuplicatedError(`Module "${module.id}" already exists`, ...info);
|
|
134
|
+
}
|
|
135
|
+
modulesMap.set(module.id, module);
|
|
136
|
+
}
|
|
137
|
+
return modulesMap;
|
|
138
|
+
}
|
|
139
|
+
function ensureModuleUniqueIds(modules) {
|
|
140
|
+
const collisions = modules
|
|
141
|
+
.filter((mod, i, all) => i !== all.findIndex((m) => m.id === mod.id))
|
|
142
|
+
.map((m) => m.id);
|
|
143
|
+
if (collisions.length) {
|
|
144
|
+
throw new ModuleNonUniqueIdError(`Modules with non-unique ids: ${collisions.join(', ')}`, `All modules should have unique ids, please locate and fix them.`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { ReflectiveInjector } from '../di';
|
|
2
|
+
import { once, merge } from '../shared/utils';
|
|
3
|
+
import async_context from '#async-context';
|
|
4
|
+
import { attachGlobalProvidersMap } from './di';
|
|
5
|
+
import { CONTEXT } from './tokens';
|
|
6
|
+
export function createContextBuilder({ appInjector, modulesMap, appLevelOperationProviders, singletonGlobalProvidersMap, operationGlobalProvidersMap, }) {
|
|
7
|
+
// This is very critical. It creates an execution context.
|
|
8
|
+
// It has to run on every operation.
|
|
9
|
+
const contextBuilder = (context) => {
|
|
10
|
+
// Cache for context per module
|
|
11
|
+
let contextCache = {};
|
|
12
|
+
// A list of providers with OnDestroy hooks
|
|
13
|
+
// It's a tuple because we want to know which Injector controls the provider
|
|
14
|
+
// and we want to know if the provider was even instantiated.
|
|
15
|
+
let providersToDestroy = [];
|
|
16
|
+
function registerProvidersToDestroy(injector) {
|
|
17
|
+
injector._providers.forEach((provider) => {
|
|
18
|
+
if (provider.factory.hasOnDestroyHook) {
|
|
19
|
+
// keep provider key's id (it doesn't change over time)
|
|
20
|
+
// and related injector
|
|
21
|
+
providersToDestroy.push([injector, provider.key.id]);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
let appContext;
|
|
26
|
+
attachGlobalProvidersMap({
|
|
27
|
+
injector: appInjector,
|
|
28
|
+
globalProvidersMap: singletonGlobalProvidersMap,
|
|
29
|
+
moduleInjectorGetter(moduleId) {
|
|
30
|
+
return modulesMap.get(moduleId).injector;
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
appInjector.setExecutionContextGetter(function executionContextGetter() {
|
|
34
|
+
var _a;
|
|
35
|
+
return (((_a = async_context.getAsyncContext()) === null || _a === void 0 ? void 0 : _a.getApplicationContext()) || appContext);
|
|
36
|
+
});
|
|
37
|
+
function createModuleExecutionContextGetter(moduleId) {
|
|
38
|
+
return function moduleExecutionContextGetter() {
|
|
39
|
+
var _a;
|
|
40
|
+
return (((_a = async_context.getAsyncContext()) === null || _a === void 0 ? void 0 : _a.getModuleContext(moduleId)) ||
|
|
41
|
+
getModuleContext(moduleId, context));
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
modulesMap.forEach((mod, moduleId) => {
|
|
45
|
+
mod.injector.setExecutionContextGetter(createModuleExecutionContextGetter(moduleId));
|
|
46
|
+
});
|
|
47
|
+
// As the name of the Injector says, it's an Operation scoped Injector
|
|
48
|
+
// Application level
|
|
49
|
+
// Operation scoped - means it's created and destroyed on every GraphQL Operation
|
|
50
|
+
const operationAppInjector = ReflectiveInjector.createFromResolved({
|
|
51
|
+
name: 'App (Operation Scope)',
|
|
52
|
+
providers: appLevelOperationProviders.concat(ReflectiveInjector.resolve([
|
|
53
|
+
{
|
|
54
|
+
provide: CONTEXT,
|
|
55
|
+
useValue: context,
|
|
56
|
+
},
|
|
57
|
+
])),
|
|
58
|
+
parent: appInjector,
|
|
59
|
+
});
|
|
60
|
+
// Create a context for application-level ExecutionContext
|
|
61
|
+
appContext = merge(context, {
|
|
62
|
+
injector: operationAppInjector,
|
|
63
|
+
});
|
|
64
|
+
// Track Providers with OnDestroy hooks
|
|
65
|
+
registerProvidersToDestroy(operationAppInjector);
|
|
66
|
+
function getModuleContext(moduleId, ctx) {
|
|
67
|
+
var _a;
|
|
68
|
+
// Reuse a context or create if not available
|
|
69
|
+
if (!contextCache[moduleId]) {
|
|
70
|
+
// We're interested in operation-scoped providers only
|
|
71
|
+
const providers = (_a = modulesMap.get(moduleId)) === null || _a === void 0 ? void 0 : _a.operationProviders;
|
|
72
|
+
// Create module-level Operation-scoped Injector
|
|
73
|
+
const operationModuleInjector = ReflectiveInjector.createFromResolved({
|
|
74
|
+
name: `Module "${moduleId}" (Operation Scope)`,
|
|
75
|
+
providers: providers.concat(ReflectiveInjector.resolve([
|
|
76
|
+
{
|
|
77
|
+
provide: CONTEXT,
|
|
78
|
+
useFactory() {
|
|
79
|
+
return contextCache[moduleId];
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
])),
|
|
83
|
+
// This injector has a priority
|
|
84
|
+
parent: modulesMap.get(moduleId).injector,
|
|
85
|
+
// over this one
|
|
86
|
+
fallbackParent: operationAppInjector,
|
|
87
|
+
});
|
|
88
|
+
// Same as on application level, we need to collect providers with OnDestroy hooks
|
|
89
|
+
registerProvidersToDestroy(operationModuleInjector);
|
|
90
|
+
contextCache[moduleId] = merge(ctx, {
|
|
91
|
+
injector: operationModuleInjector,
|
|
92
|
+
moduleId,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return contextCache[moduleId];
|
|
96
|
+
}
|
|
97
|
+
const sharedContext = merge(
|
|
98
|
+
// We want to pass the received context
|
|
99
|
+
context || {}, {
|
|
100
|
+
// Here's something very crutial
|
|
101
|
+
// It's a function that is used in module's context creation
|
|
102
|
+
ɵgetModuleContext: getModuleContext,
|
|
103
|
+
});
|
|
104
|
+
attachGlobalProvidersMap({
|
|
105
|
+
injector: operationAppInjector,
|
|
106
|
+
globalProvidersMap: operationGlobalProvidersMap,
|
|
107
|
+
moduleInjectorGetter(moduleId) {
|
|
108
|
+
return getModuleContext(moduleId, sharedContext).injector;
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
const env = {
|
|
112
|
+
ɵdestroy: once(() => {
|
|
113
|
+
providersToDestroy.forEach(([injector, keyId]) => {
|
|
114
|
+
// If provider was instantiated
|
|
115
|
+
if (injector._isObjectDefinedByKeyId(keyId)) {
|
|
116
|
+
// call its OnDestroy hook
|
|
117
|
+
injector._getObjByKeyId(keyId).onDestroy();
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
contextCache = {};
|
|
121
|
+
}),
|
|
122
|
+
ɵinjector: operationAppInjector,
|
|
123
|
+
context: sharedContext,
|
|
124
|
+
};
|
|
125
|
+
return {
|
|
126
|
+
...env,
|
|
127
|
+
runWithContext(cb) {
|
|
128
|
+
return async_context.runWithAsyncContext({
|
|
129
|
+
getApplicationContext() {
|
|
130
|
+
return appContext;
|
|
131
|
+
},
|
|
132
|
+
getModuleContext(moduleId) {
|
|
133
|
+
return getModuleContext(moduleId, context);
|
|
134
|
+
},
|
|
135
|
+
}, cb, env);
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
return contextBuilder;
|
|
140
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Scope } from '../di';
|
|
2
|
+
export function instantiateSingletonProviders({ appInjector, modulesMap, }) {
|
|
3
|
+
appInjector.instantiateAll();
|
|
4
|
+
modulesMap.forEach((mod) => {
|
|
5
|
+
mod.injector.instantiateAll();
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
export function createGlobalProvidersMap({ modules, scope, }) {
|
|
9
|
+
const globalProvidersMap = {};
|
|
10
|
+
const propType = scope === Scope.Singleton ? 'singletonProviders' : 'operationProviders';
|
|
11
|
+
modules.forEach((mod) => {
|
|
12
|
+
mod[propType].forEach((provider) => {
|
|
13
|
+
if (provider.factory.isGlobal) {
|
|
14
|
+
const key = provider.key.id;
|
|
15
|
+
if (globalProvidersMap[key]) {
|
|
16
|
+
throw duplicatedGlobalTokenError(provider, [
|
|
17
|
+
mod.id,
|
|
18
|
+
globalProvidersMap[key],
|
|
19
|
+
]);
|
|
20
|
+
}
|
|
21
|
+
globalProvidersMap[key] = mod.id;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
return globalProvidersMap;
|
|
26
|
+
}
|
|
27
|
+
export function attachGlobalProvidersMap({ injector, globalProvidersMap, moduleInjectorGetter, }) {
|
|
28
|
+
injector._globalProvidersMap = {
|
|
29
|
+
has(key) {
|
|
30
|
+
return typeof globalProvidersMap[key] === 'string';
|
|
31
|
+
},
|
|
32
|
+
get(key) {
|
|
33
|
+
return moduleInjectorGetter(globalProvidersMap[key]);
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export function duplicatedGlobalTokenError(provider, modules) {
|
|
38
|
+
return Error([
|
|
39
|
+
`Failed to define '${provider.key.displayName}' token as global.`,
|
|
40
|
+
`Token provided by two modules: '${modules.join("', '")}'`,
|
|
41
|
+
].join(' '));
|
|
42
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { execute, } from 'graphql';
|
|
2
|
+
import { isNotSchema } from '../shared/utils';
|
|
3
|
+
export function executionCreator({ contextBuilder, }) {
|
|
4
|
+
const createExecution = (options) => {
|
|
5
|
+
// Custom or original execute function
|
|
6
|
+
const executeFn = (options === null || options === void 0 ? void 0 : options.execute) || execute;
|
|
7
|
+
return (argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) => {
|
|
8
|
+
function perform({ context, ɵdestroy: destroy, }) {
|
|
9
|
+
const executionArgs = isNotSchema(argsOrSchema)
|
|
10
|
+
? {
|
|
11
|
+
...argsOrSchema,
|
|
12
|
+
contextValue: context,
|
|
13
|
+
}
|
|
14
|
+
: {
|
|
15
|
+
schema: argsOrSchema,
|
|
16
|
+
document: document,
|
|
17
|
+
rootValue,
|
|
18
|
+
contextValue: context,
|
|
19
|
+
variableValues,
|
|
20
|
+
operationName,
|
|
21
|
+
fieldResolver,
|
|
22
|
+
typeResolver,
|
|
23
|
+
};
|
|
24
|
+
// It's important to wrap the executeFn within a promise
|
|
25
|
+
// so we can easily control the end of execution (with finally)
|
|
26
|
+
return Promise.resolve()
|
|
27
|
+
.then(() => executeFn(executionArgs))
|
|
28
|
+
.finally(destroy);
|
|
29
|
+
}
|
|
30
|
+
if (options === null || options === void 0 ? void 0 : options.controller) {
|
|
31
|
+
return perform(options.controller);
|
|
32
|
+
}
|
|
33
|
+
return contextBuilder(isNotSchema(argsOrSchema)
|
|
34
|
+
? argsOrSchema.contextValue
|
|
35
|
+
: contextValue).runWithContext(perform);
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
return createExecution;
|
|
39
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function operationControllerCreator(options) {
|
|
2
|
+
const { contextBuilder } = options;
|
|
3
|
+
return (input) => {
|
|
4
|
+
const operation = contextBuilder(input.context);
|
|
5
|
+
const ɵdestroy = input.autoDestroy ? operation.ɵdestroy : () => { };
|
|
6
|
+
return {
|
|
7
|
+
context: operation.context,
|
|
8
|
+
injector: operation.ɵinjector,
|
|
9
|
+
destroy: operation.ɵdestroy,
|
|
10
|
+
ɵdestroy,
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { subscribe, } from 'graphql';
|
|
2
|
+
import { tapAsyncIterator, isAsyncIterable, isNotSchema, } from '../shared/utils';
|
|
3
|
+
export function subscriptionCreator({ contextBuilder, }) {
|
|
4
|
+
const createSubscription = (options) => {
|
|
5
|
+
// Custom or original subscribe function
|
|
6
|
+
const subscribeFn = (options === null || options === void 0 ? void 0 : options.subscribe) || subscribe;
|
|
7
|
+
return (argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver) => {
|
|
8
|
+
function perform({ context, ɵdestroy: destroy, }) {
|
|
9
|
+
const subscriptionArgs = isNotSchema(argsOrSchema)
|
|
10
|
+
? {
|
|
11
|
+
...argsOrSchema,
|
|
12
|
+
contextValue: context,
|
|
13
|
+
}
|
|
14
|
+
: {
|
|
15
|
+
schema: argsOrSchema,
|
|
16
|
+
document: document,
|
|
17
|
+
rootValue,
|
|
18
|
+
contextValue: context,
|
|
19
|
+
variableValues,
|
|
20
|
+
operationName,
|
|
21
|
+
fieldResolver,
|
|
22
|
+
subscribeFieldResolver,
|
|
23
|
+
};
|
|
24
|
+
let isIterable = false;
|
|
25
|
+
// It's important to wrap the subscribeFn within a promise
|
|
26
|
+
// so we can easily control the end of subscription (with finally)
|
|
27
|
+
return Promise.resolve()
|
|
28
|
+
.then(() => subscribeFn(subscriptionArgs))
|
|
29
|
+
.then((sub) => {
|
|
30
|
+
if (isAsyncIterable(sub)) {
|
|
31
|
+
isIterable = true;
|
|
32
|
+
return tapAsyncIterator(sub, destroy);
|
|
33
|
+
}
|
|
34
|
+
return sub;
|
|
35
|
+
})
|
|
36
|
+
.finally(() => {
|
|
37
|
+
if (!isIterable) {
|
|
38
|
+
destroy();
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
if (options === null || options === void 0 ? void 0 : options.controller) {
|
|
43
|
+
return perform(options.controller);
|
|
44
|
+
}
|
|
45
|
+
return contextBuilder(isNotSchema(argsOrSchema)
|
|
46
|
+
? argsOrSchema.contextValue
|
|
47
|
+
: contextValue).runWithContext(perform);
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
return createSubscription;
|
|
51
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { InjectionToken } from '../di';
|
|
2
|
+
/**
|
|
3
|
+
* @api
|
|
4
|
+
* `CONTEXT` is an InjectionToken representing the provided `GraphQLModules.GlobalContext`
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { CONTEXT, Inject, Injectable } from 'graphql-modules';
|
|
10
|
+
*
|
|
11
|
+
* (A)Injectable()
|
|
12
|
+
* export class Data {
|
|
13
|
+
* constructor((A)Inject(CONTEXT) private context: GraphQLModules.GlobalContext) {}
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export const CONTEXT = new InjectionToken('context');
|
|
File without changes
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { isType } from './providers';
|
|
2
|
+
import { INJECTABLE, readInjectableMetadata, ensureInjectableMetadata, } from './metadata';
|
|
3
|
+
function ensureReflect() {
|
|
4
|
+
if (!(Reflect && Reflect.getOwnMetadata)) {
|
|
5
|
+
throw 'reflect-metadata shim is required when using class decorators';
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export function Injectable(options) {
|
|
9
|
+
return (target) => {
|
|
10
|
+
var _a;
|
|
11
|
+
ensureReflect();
|
|
12
|
+
const params = (Reflect.getMetadata('design:paramtypes', target) || []).map((param) => (isType(param) ? param : null));
|
|
13
|
+
const existingMeta = readInjectableMetadata(target);
|
|
14
|
+
const meta = {
|
|
15
|
+
params: ((_a = existingMeta === null || existingMeta === void 0 ? void 0 : existingMeta.params) === null || _a === void 0 ? void 0 : _a.length) > 0 && params.length === 0
|
|
16
|
+
? existingMeta === null || existingMeta === void 0 ? void 0 : existingMeta.params
|
|
17
|
+
: params.map((param, i) => {
|
|
18
|
+
var _a;
|
|
19
|
+
const existingParam = (_a = existingMeta === null || existingMeta === void 0 ? void 0 : existingMeta.params) === null || _a === void 0 ? void 0 : _a[i];
|
|
20
|
+
return {
|
|
21
|
+
type: (existingParam === null || existingParam === void 0 ? void 0 : existingParam.type) || param,
|
|
22
|
+
optional: typeof (existingParam === null || existingParam === void 0 ? void 0 : existingParam.optional) === 'boolean'
|
|
23
|
+
? existingParam.optional
|
|
24
|
+
: false,
|
|
25
|
+
};
|
|
26
|
+
}),
|
|
27
|
+
options: {
|
|
28
|
+
...((existingMeta === null || existingMeta === void 0 ? void 0 : existingMeta.options) || {}),
|
|
29
|
+
...(options || {}),
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
target[INJECTABLE] = meta;
|
|
33
|
+
return target;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export function Optional() {
|
|
37
|
+
return (target, _, index) => {
|
|
38
|
+
ensureReflect();
|
|
39
|
+
ensureInjectableMetadata(target);
|
|
40
|
+
const meta = readInjectableMetadata(target);
|
|
41
|
+
meta.params[index] = {
|
|
42
|
+
...meta.params[index],
|
|
43
|
+
optional: true,
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export function Inject(type) {
|
|
48
|
+
return (target, _, index) => {
|
|
49
|
+
ensureReflect();
|
|
50
|
+
ensureInjectableMetadata(target);
|
|
51
|
+
const meta = readInjectableMetadata(target);
|
|
52
|
+
meta.params[index] = {
|
|
53
|
+
type,
|
|
54
|
+
optional: false,
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export function ExecutionContext() {
|
|
59
|
+
return (obj, propertyKey) => {
|
|
60
|
+
ensureReflect();
|
|
61
|
+
const target = obj.constructor;
|
|
62
|
+
ensureInjectableMetadata(target);
|
|
63
|
+
const meta = readInjectableMetadata(target);
|
|
64
|
+
if (!meta.options) {
|
|
65
|
+
meta.options = {};
|
|
66
|
+
}
|
|
67
|
+
if (!meta.options.executionContextIn) {
|
|
68
|
+
meta.options.executionContextIn = [];
|
|
69
|
+
}
|
|
70
|
+
meta.options.executionContextIn.push(propertyKey);
|
|
71
|
+
};
|
|
72
|
+
}
|