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