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,103 @@
|
|
|
1
|
+
import { mergeDeepWith } from 'ramda';
|
|
2
|
+
import { isDefined } from './utils';
|
|
3
|
+
import { ExtraMiddlewareError, useLocation } from './errors';
|
|
4
|
+
export function compose(middleware) {
|
|
5
|
+
if (!Array.isArray(middleware)) {
|
|
6
|
+
throw new TypeError('Middleware stack must be an array!');
|
|
7
|
+
}
|
|
8
|
+
for (const fn of middleware) {
|
|
9
|
+
if (typeof fn !== 'function') {
|
|
10
|
+
throw new TypeError('Middleware must be composed of functions!');
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return function composed(context, next) {
|
|
14
|
+
// last called middleware
|
|
15
|
+
let index = -1;
|
|
16
|
+
function dispatch(i) {
|
|
17
|
+
if (i <= index) {
|
|
18
|
+
return Promise.reject(new Error('next() called multiple times'));
|
|
19
|
+
}
|
|
20
|
+
index = i;
|
|
21
|
+
const fn = i === middleware.length ? next : middleware[i];
|
|
22
|
+
if (!fn) {
|
|
23
|
+
return Promise.resolve();
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
return Promise.resolve(fn(context, dispatch.bind(null, i + 1)));
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
return Promise.reject(err);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return dispatch(0);
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export function createMiddleware(path, middlewareMap) {
|
|
36
|
+
const middlewares = middlewareMap ? pickMiddlewares(path, middlewareMap) : [];
|
|
37
|
+
return compose(middlewares);
|
|
38
|
+
}
|
|
39
|
+
export function mergeMiddlewareMaps(app, mod) {
|
|
40
|
+
const merge = (left, right) => {
|
|
41
|
+
return mergeDeepWith((l, r) => {
|
|
42
|
+
if (Array.isArray(l)) {
|
|
43
|
+
return l.concat(r || []);
|
|
44
|
+
}
|
|
45
|
+
return merge(l, r);
|
|
46
|
+
}, left, right);
|
|
47
|
+
};
|
|
48
|
+
return merge(app, mod);
|
|
49
|
+
}
|
|
50
|
+
function pickMiddlewares(path, middlewareMap) {
|
|
51
|
+
var _a;
|
|
52
|
+
const middlewares = [];
|
|
53
|
+
const [type, field] = path;
|
|
54
|
+
if ((_a = middlewareMap['*']) === null || _a === void 0 ? void 0 : _a['*']) {
|
|
55
|
+
middlewares.push(...middlewareMap['*']['*']);
|
|
56
|
+
}
|
|
57
|
+
const typeMap = middlewareMap[type];
|
|
58
|
+
if (typeMap) {
|
|
59
|
+
if (typeMap['*']) {
|
|
60
|
+
middlewares.push(...typeMap['*']);
|
|
61
|
+
}
|
|
62
|
+
if (field && typeMap[field]) {
|
|
63
|
+
middlewares.push(...typeMap[field]);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return middlewares.filter(isDefined);
|
|
67
|
+
}
|
|
68
|
+
export function validateMiddlewareMap(middlewareMap, metadata) {
|
|
69
|
+
const exists = checkExistence(metadata);
|
|
70
|
+
for (const typeName in middlewareMap.types) {
|
|
71
|
+
if (middlewareMap.types.hasOwnProperty(typeName)) {
|
|
72
|
+
const typeMiddlewareMap = middlewareMap[typeName];
|
|
73
|
+
if (!exists.type(typeName)) {
|
|
74
|
+
throw new ExtraMiddlewareError(`Cannot apply a middleware to non existing "${typeName}" type`, useLocation({ dirname: metadata.dirname, id: metadata.id }));
|
|
75
|
+
}
|
|
76
|
+
for (const fieldName in typeMiddlewareMap[typeName]) {
|
|
77
|
+
if (typeMiddlewareMap[typeName].hasOwnProperty(fieldName)) {
|
|
78
|
+
if (!exists.field(typeName, fieldName)) {
|
|
79
|
+
throw new ExtraMiddlewareError(`Cannot apply a middleware to non existing "${typeName}.${fieldName}" type.field`, useLocation({ dirname: metadata.dirname, id: metadata.id }));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Helps to make sure a middleware has a corresponding type/field definition.
|
|
88
|
+
* We don't want to pass a module-level middlewares that are not related to the module.
|
|
89
|
+
* Not because it's dangerous but to prevent unused middlewares.
|
|
90
|
+
*/
|
|
91
|
+
function checkExistence(metadata) {
|
|
92
|
+
return {
|
|
93
|
+
type(name) {
|
|
94
|
+
var _a, _b;
|
|
95
|
+
return isDefined(((_a = metadata.implements) === null || _a === void 0 ? void 0 : _a[name]) || ((_b = metadata.extends) === null || _b === void 0 ? void 0 : _b[name]));
|
|
96
|
+
},
|
|
97
|
+
field(type, name) {
|
|
98
|
+
var _a, _b, _c, _d;
|
|
99
|
+
return isDefined(((_b = (_a = metadata.implements) === null || _a === void 0 ? void 0 : _a[type]) === null || _b === void 0 ? void 0 : _b.includes(name)) ||
|
|
100
|
+
((_d = (_c = metadata.extends) === null || _c === void 0 ? void 0 : _c[type]) === null || _d === void 0 ? void 0 : _d.includes(name)));
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { GraphQLSchema } from 'graphql';
|
|
2
|
+
export function flatten(arr) {
|
|
3
|
+
return Array.prototype.concat(...arr);
|
|
4
|
+
}
|
|
5
|
+
export function isDefined(val) {
|
|
6
|
+
return !isNil(val);
|
|
7
|
+
}
|
|
8
|
+
export function isNil(val) {
|
|
9
|
+
return val === null || typeof val === 'undefined';
|
|
10
|
+
}
|
|
11
|
+
export function isObject(val) {
|
|
12
|
+
return Object.prototype.toString.call(val) === '[object Object]';
|
|
13
|
+
}
|
|
14
|
+
export function isPrimitive(val) {
|
|
15
|
+
return ['number', 'string', 'boolean', 'symbol', 'bigint'].includes(typeof val);
|
|
16
|
+
}
|
|
17
|
+
export function isAsyncIterable(obj) {
|
|
18
|
+
return obj && typeof obj[Symbol.asyncIterator] === 'function';
|
|
19
|
+
}
|
|
20
|
+
export function tapAsyncIterator(iterable, doneCallback) {
|
|
21
|
+
const iteratorMethod = iterable[Symbol.asyncIterator];
|
|
22
|
+
const iterator = iteratorMethod.call(iterable);
|
|
23
|
+
function mapResult(result) {
|
|
24
|
+
if (result.done) {
|
|
25
|
+
doneCallback();
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
async next() {
|
|
31
|
+
try {
|
|
32
|
+
let result = await iterator.next();
|
|
33
|
+
return mapResult(result);
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
doneCallback();
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
async return(value) {
|
|
41
|
+
try {
|
|
42
|
+
const result = await iterator.return(value);
|
|
43
|
+
return mapResult(result);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
doneCallback();
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
throw(error) {
|
|
51
|
+
doneCallback();
|
|
52
|
+
return iterator.throw(error);
|
|
53
|
+
},
|
|
54
|
+
[Symbol.asyncIterator]() {
|
|
55
|
+
return this;
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export function once(cb) {
|
|
60
|
+
let called = false;
|
|
61
|
+
return () => {
|
|
62
|
+
if (!called) {
|
|
63
|
+
called = true;
|
|
64
|
+
cb();
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
export function share(factory) {
|
|
69
|
+
let cached = null;
|
|
70
|
+
return (arg) => {
|
|
71
|
+
if (!cached) {
|
|
72
|
+
cached = factory(arg);
|
|
73
|
+
}
|
|
74
|
+
return cached;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export function uniqueId(isNotUsed) {
|
|
78
|
+
let id;
|
|
79
|
+
while (!isNotUsed((id = Math.random().toString(16).substr(2)))) { }
|
|
80
|
+
return id;
|
|
81
|
+
}
|
|
82
|
+
export function isNotSchema(obj) {
|
|
83
|
+
return obj instanceof GraphQLSchema === false;
|
|
84
|
+
}
|
|
85
|
+
export function merge(source, target) {
|
|
86
|
+
const result = {
|
|
87
|
+
...source,
|
|
88
|
+
...target,
|
|
89
|
+
};
|
|
90
|
+
function attachSymbols(obj) {
|
|
91
|
+
const symbols = Object.getOwnPropertySymbols(obj);
|
|
92
|
+
for (const symbol of symbols) {
|
|
93
|
+
result[symbol] = obj[symbol];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (source) {
|
|
97
|
+
attachSymbols(source);
|
|
98
|
+
}
|
|
99
|
+
attachSymbols(target);
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { mockApplication } from './test-application';
|
|
2
|
+
import { testModule, mockModule } from './test-module';
|
|
3
|
+
import { testInjector, readProviderOptions } from './test-injector';
|
|
4
|
+
import { execute } from './graphql';
|
|
5
|
+
import { provideEmpty } from './di';
|
|
6
|
+
export const testkit = {
|
|
7
|
+
mockApplication,
|
|
8
|
+
mockModule,
|
|
9
|
+
testModule,
|
|
10
|
+
testInjector,
|
|
11
|
+
readProviderOptions,
|
|
12
|
+
provideEmpty,
|
|
13
|
+
execute,
|
|
14
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { share } from '../shared/utils';
|
|
2
|
+
export function mockApplication(app) {
|
|
3
|
+
function mockedFactory(newConfig) {
|
|
4
|
+
const sharedFactory = share(() => app.ɵfactory(newConfig));
|
|
5
|
+
return {
|
|
6
|
+
get typeDefs() {
|
|
7
|
+
return sharedFactory().typeDefs;
|
|
8
|
+
},
|
|
9
|
+
get resolvers() {
|
|
10
|
+
return sharedFactory().resolvers;
|
|
11
|
+
},
|
|
12
|
+
get schema() {
|
|
13
|
+
return sharedFactory().schema;
|
|
14
|
+
},
|
|
15
|
+
get injector() {
|
|
16
|
+
return sharedFactory().injector;
|
|
17
|
+
},
|
|
18
|
+
createOperationController(options) {
|
|
19
|
+
return sharedFactory().createOperationController(options);
|
|
20
|
+
},
|
|
21
|
+
createSubscription(options) {
|
|
22
|
+
return sharedFactory().createSubscription(options);
|
|
23
|
+
},
|
|
24
|
+
createExecution(options) {
|
|
25
|
+
return sharedFactory().createExecution(options);
|
|
26
|
+
},
|
|
27
|
+
createSchemaForApollo() {
|
|
28
|
+
return sharedFactory().createSchemaForApollo();
|
|
29
|
+
},
|
|
30
|
+
createApolloExecutor() {
|
|
31
|
+
return sharedFactory().createApolloExecutor();
|
|
32
|
+
},
|
|
33
|
+
get ɵfactory() {
|
|
34
|
+
return sharedFactory().ɵfactory;
|
|
35
|
+
},
|
|
36
|
+
get ɵconfig() {
|
|
37
|
+
return sharedFactory().ɵconfig;
|
|
38
|
+
},
|
|
39
|
+
replaceModule(newModule) {
|
|
40
|
+
const config = sharedFactory().ɵconfig;
|
|
41
|
+
return mockedFactory({
|
|
42
|
+
...config,
|
|
43
|
+
modules: config.modules.map((mod) => mod.id === newModule.ɵoriginalModule.id ? newModule : mod),
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
addProviders(newProviders) {
|
|
47
|
+
const config = sharedFactory().ɵconfig;
|
|
48
|
+
const existingProviders = typeof config.providers === 'function'
|
|
49
|
+
? config.providers()
|
|
50
|
+
: config.providers;
|
|
51
|
+
const providers = Array.isArray(existingProviders)
|
|
52
|
+
? existingProviders.concat(newProviders)
|
|
53
|
+
: newProviders;
|
|
54
|
+
return mockedFactory({
|
|
55
|
+
...config,
|
|
56
|
+
providers,
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
return mockedFactory();
|
|
62
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ReflectiveInjector } from '../di/injector';
|
|
2
|
+
import { CONTEXT } from '../application/tokens';
|
|
3
|
+
import { readInjectableMetadata } from '../di/metadata';
|
|
4
|
+
export function testInjector(providers) {
|
|
5
|
+
const resolvedProviders = ReflectiveInjector.resolve([
|
|
6
|
+
{ provide: CONTEXT, useValue: {} },
|
|
7
|
+
...providers,
|
|
8
|
+
]);
|
|
9
|
+
const injector = ReflectiveInjector.createFromResolved({
|
|
10
|
+
name: 'test',
|
|
11
|
+
providers: resolvedProviders,
|
|
12
|
+
});
|
|
13
|
+
injector.instantiateAll();
|
|
14
|
+
return injector;
|
|
15
|
+
}
|
|
16
|
+
export function readProviderOptions(provider) {
|
|
17
|
+
return readInjectableMetadata(provider, true).options;
|
|
18
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { visit, Kind, concatAST, } from 'graphql';
|
|
2
|
+
import { moduleFactory } from '../module/factory';
|
|
3
|
+
import { createApplication } from '../application/application';
|
|
4
|
+
import { createModule } from '../module/module';
|
|
5
|
+
export function mockModule(testedModule, overrideConfig) {
|
|
6
|
+
const sourceProviders = typeof testedModule.config.providers === 'function'
|
|
7
|
+
? testedModule.config.providers()
|
|
8
|
+
: testedModule.config.providers;
|
|
9
|
+
const overrideProviders = typeof overrideConfig.providers === 'function'
|
|
10
|
+
? overrideConfig.providers()
|
|
11
|
+
: overrideConfig.providers;
|
|
12
|
+
const newModule = createModule({
|
|
13
|
+
...testedModule.config,
|
|
14
|
+
providers: [...(sourceProviders || []), ...(overrideProviders || [])],
|
|
15
|
+
});
|
|
16
|
+
newModule['ɵoriginalModule'] = testedModule;
|
|
17
|
+
return newModule;
|
|
18
|
+
}
|
|
19
|
+
export function testModule(testedModule, config) {
|
|
20
|
+
var _a;
|
|
21
|
+
const mod = transformModule(testedModule, config);
|
|
22
|
+
const modules = [mod].concat((_a = config === null || config === void 0 ? void 0 : config.modules) !== null && _a !== void 0 ? _a : []);
|
|
23
|
+
return createApplication({
|
|
24
|
+
...(config || {}),
|
|
25
|
+
modules,
|
|
26
|
+
providers: config === null || config === void 0 ? void 0 : config.providers,
|
|
27
|
+
middlewares: config === null || config === void 0 ? void 0 : config.middlewares,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function transformModule(mod, config) {
|
|
31
|
+
const transforms = [];
|
|
32
|
+
if (config === null || config === void 0 ? void 0 : config.replaceExtensions) {
|
|
33
|
+
transforms.push((m) => moduleFactory({
|
|
34
|
+
...m.config,
|
|
35
|
+
typeDefs: replaceExtensions(m.typeDefs),
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
if (config === null || config === void 0 ? void 0 : config.typeDefs) {
|
|
39
|
+
transforms.push((m) => moduleFactory({
|
|
40
|
+
...m.config,
|
|
41
|
+
typeDefs: m.typeDefs.concat(config.typeDefs),
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
if (config === null || config === void 0 ? void 0 : config.inheritTypeDefs) {
|
|
45
|
+
transforms.push((m) => moduleFactory({
|
|
46
|
+
...m.config,
|
|
47
|
+
typeDefs: inheritTypeDefs(m.typeDefs, config.inheritTypeDefs),
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
if (config === null || config === void 0 ? void 0 : config.resolvers) {
|
|
51
|
+
transforms.push((m) => {
|
|
52
|
+
const resolvers = m.config.resolvers
|
|
53
|
+
? Array.isArray(m.config.resolvers)
|
|
54
|
+
? m.config.resolvers
|
|
55
|
+
: [m.config.resolvers]
|
|
56
|
+
: [];
|
|
57
|
+
return moduleFactory({
|
|
58
|
+
...m.config,
|
|
59
|
+
resolvers: resolvers.concat(config.resolvers),
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
if (config === null || config === void 0 ? void 0 : config.providers) {
|
|
64
|
+
transforms.push((m) => {
|
|
65
|
+
const sourceProviders = typeof m.config.providers === 'function'
|
|
66
|
+
? m.config.providers()
|
|
67
|
+
: m.config.providers;
|
|
68
|
+
const overrideProviders = typeof config.providers === 'function'
|
|
69
|
+
? config.providers()
|
|
70
|
+
: config.providers;
|
|
71
|
+
return moduleFactory({
|
|
72
|
+
...m.config,
|
|
73
|
+
providers: [...(sourceProviders || []), ...(overrideProviders || [])],
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
if (transforms) {
|
|
78
|
+
return transforms.reduce((m, transform) => transform(m), mod);
|
|
79
|
+
}
|
|
80
|
+
return mod;
|
|
81
|
+
}
|
|
82
|
+
function inheritTypeDefs(originalTypeDefs, modules) {
|
|
83
|
+
const original = concatAST(originalTypeDefs);
|
|
84
|
+
const typeDefs = treeshakeTypesDefs(original, modules.reduce((typeDefs, externalMod) => typeDefs.concat(externalMod.typeDefs), []));
|
|
85
|
+
return typeDefs;
|
|
86
|
+
}
|
|
87
|
+
function replaceExtensions(typeDefs) {
|
|
88
|
+
const types = [];
|
|
89
|
+
const extensions = [];
|
|
90
|
+
// List all object types
|
|
91
|
+
typeDefs.forEach((doc) => {
|
|
92
|
+
visit(doc, {
|
|
93
|
+
ObjectTypeDefinition(node) {
|
|
94
|
+
types.push(node.name.value);
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
// turn object type extensions into object types
|
|
99
|
+
return typeDefs.map((doc) => {
|
|
100
|
+
return visit(doc, {
|
|
101
|
+
ObjectTypeExtension(node) {
|
|
102
|
+
// only if object type doesn't exist
|
|
103
|
+
if (extensions.includes(node.name.value) ||
|
|
104
|
+
types.includes(node.name.value)) {
|
|
105
|
+
return node;
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
...node,
|
|
109
|
+
kind: Kind.OBJECT_TYPE_DEFINITION,
|
|
110
|
+
};
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
function treeshakeTypesDefs(originalSource, sources) {
|
|
116
|
+
const namedTypes = originalSource.definitions.filter(isNamedTypeDefinition);
|
|
117
|
+
const typesToVisit = namedTypes.map((def) => def.name.value);
|
|
118
|
+
const rootFields = namedTypes.reduce((acc, node) => {
|
|
119
|
+
const typeName = node.name.value;
|
|
120
|
+
if (isRootType(typeName) && hasFields(node)) {
|
|
121
|
+
if (!acc[typeName]) {
|
|
122
|
+
acc[typeName] = [];
|
|
123
|
+
}
|
|
124
|
+
node.fields.forEach((field) => {
|
|
125
|
+
acc[typeName].push(field.name.value);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return acc;
|
|
129
|
+
}, {});
|
|
130
|
+
const schema = concatAST([originalSource].concat(sources));
|
|
131
|
+
const involvedTypes = new Set(visitTypes(schema, typesToVisit, rootFields));
|
|
132
|
+
return {
|
|
133
|
+
kind: Kind.DOCUMENT,
|
|
134
|
+
definitions: schema.definitions.filter((def) => {
|
|
135
|
+
var _a, _b;
|
|
136
|
+
if (isNamedTypeDefinition(def)) {
|
|
137
|
+
const typeName = def.name.value;
|
|
138
|
+
if (!involvedTypes.has(def.name.value)) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
if ((_a = rootFields[typeName]) === null || _a === void 0 ? void 0 : _a.length) {
|
|
142
|
+
const rootType = def;
|
|
143
|
+
if ((_b = rootType.fields) === null || _b === void 0 ? void 0 : _b.every((field) => !rootFields[typeName].includes(field.name.value))) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return true;
|
|
149
|
+
}),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
function isNamedTypeDefinition(def) {
|
|
153
|
+
return (!!def &&
|
|
154
|
+
def.kind !== Kind.SCHEMA_DEFINITION &&
|
|
155
|
+
def.kind !== Kind.SCHEMA_EXTENSION);
|
|
156
|
+
}
|
|
157
|
+
function visitTypes(schema, types, rootFields) {
|
|
158
|
+
const visitedTypes = [];
|
|
159
|
+
const scalars = schema.definitions
|
|
160
|
+
.filter((def) => def.kind === Kind.SCALAR_TYPE_DEFINITION ||
|
|
161
|
+
def.kind === Kind.SCALAR_TYPE_EXTENSION)
|
|
162
|
+
.map((def) => def.name.value);
|
|
163
|
+
for (const typeName of types) {
|
|
164
|
+
collectType(typeName);
|
|
165
|
+
}
|
|
166
|
+
return visitedTypes;
|
|
167
|
+
function collectField(field, parentTypeName) {
|
|
168
|
+
var _a;
|
|
169
|
+
if (parentTypeName &&
|
|
170
|
+
isRootType(parentTypeName) &&
|
|
171
|
+
((_a = rootFields[parentTypeName]) === null || _a === void 0 ? void 0 : _a.length) &&
|
|
172
|
+
!rootFields[parentTypeName].includes(field.name.value)) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
collectType(resolveType(field.type));
|
|
176
|
+
if (field.arguments) {
|
|
177
|
+
field.arguments.forEach((arg) => {
|
|
178
|
+
collectType(resolveType(arg.type));
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
if (field.directives) {
|
|
182
|
+
field.directives.forEach((directive) => {
|
|
183
|
+
collectType(directive.name.value);
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function collectType(typeName) {
|
|
188
|
+
if (visitedTypes.includes(typeName)) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (isScalar(typeName)) {
|
|
192
|
+
visitedTypes.push(typeName);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const types = findTypes(typeName);
|
|
196
|
+
visitedTypes.push(typeName);
|
|
197
|
+
types.forEach((type) => {
|
|
198
|
+
if (hasFields(type)) {
|
|
199
|
+
type.fields.forEach((field) => {
|
|
200
|
+
collectField(field, typeName);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
if (hasTypes(type)) {
|
|
204
|
+
type.types.forEach((t) => {
|
|
205
|
+
collectType(resolveType(t));
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
if (hasInterfaces(type)) {
|
|
209
|
+
type.interfaces.forEach((i) => {
|
|
210
|
+
collectType(resolveType(i));
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
function resolveType(type) {
|
|
216
|
+
if (type.kind === 'ListType') {
|
|
217
|
+
return resolveType(type.type);
|
|
218
|
+
}
|
|
219
|
+
if (type.kind === 'NonNullType') {
|
|
220
|
+
return resolveType(type.type);
|
|
221
|
+
}
|
|
222
|
+
return type.name.value;
|
|
223
|
+
}
|
|
224
|
+
function isScalar(name) {
|
|
225
|
+
return scalars
|
|
226
|
+
.concat(['String', 'Boolean', 'Int', 'ID', 'Float'])
|
|
227
|
+
.includes(name);
|
|
228
|
+
}
|
|
229
|
+
function findTypes(typeName) {
|
|
230
|
+
const types = schema.definitions.filter((def) => isNamedTypeDefinition(def) && def.name.value === typeName);
|
|
231
|
+
if (!types.length) {
|
|
232
|
+
throw new Error(`Missing type "${typeName}"`);
|
|
233
|
+
}
|
|
234
|
+
return types;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function hasInterfaces(def) {
|
|
238
|
+
return (hasPropValue(def, 'interfaces') &&
|
|
239
|
+
[
|
|
240
|
+
Kind.OBJECT_TYPE_DEFINITION,
|
|
241
|
+
Kind.OBJECT_TYPE_EXTENSION,
|
|
242
|
+
Kind.INTERFACE_TYPE_DEFINITION,
|
|
243
|
+
Kind.INTERFACE_TYPE_EXTENSION,
|
|
244
|
+
].includes(def.kind));
|
|
245
|
+
}
|
|
246
|
+
function hasTypes(def) {
|
|
247
|
+
return ([Kind.UNION_TYPE_DEFINITION, Kind.UNION_TYPE_EXTENSION].includes(def.kind) && hasPropValue(def, 'types'));
|
|
248
|
+
}
|
|
249
|
+
function hasFields(def) {
|
|
250
|
+
return ([
|
|
251
|
+
Kind.OBJECT_TYPE_DEFINITION,
|
|
252
|
+
Kind.OBJECT_TYPE_EXTENSION,
|
|
253
|
+
Kind.INTERFACE_TYPE_DEFINITION,
|
|
254
|
+
Kind.INTERFACE_TYPE_EXTENSION,
|
|
255
|
+
Kind.INPUT_OBJECT_TYPE_DEFINITION,
|
|
256
|
+
Kind.INPUT_OBJECT_TYPE_EXTENSION,
|
|
257
|
+
].includes(def.kind) && hasPropValue(def, 'fields'));
|
|
258
|
+
}
|
|
259
|
+
function hasPropValue(obj, prop) {
|
|
260
|
+
return Object.prototype.hasOwnProperty.call(obj, prop) && obj[prop];
|
|
261
|
+
}
|
|
262
|
+
function isRootType(typeName) {
|
|
263
|
+
return (typeName === 'Query' ||
|
|
264
|
+
typeName === 'Mutation' ||
|
|
265
|
+
typeName === 'Subscription');
|
|
266
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graphql-modules",
|
|
3
|
-
"version": "3.1.2-alpha-
|
|
3
|
+
"version": "3.1.2-alpha-20260121024633-8af430e29a180a1d00c50c4bf74d342b3518267c",
|
|
4
4
|
"description": "Create reusable, maintainable, testable and extendable GraphQL modules",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"peerDependencies": {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"@graphql-tools/schema": "^10.0.0",
|
|
11
|
-
"@graphql-tools/wrap": "^
|
|
11
|
+
"@graphql-tools/wrap": "^10.0.0",
|
|
12
12
|
"@graphql-typed-document-node/core": "^3.1.0",
|
|
13
13
|
"ramda": "^0.31.0"
|
|
14
14
|
},
|
|
@@ -21,20 +21,40 @@
|
|
|
21
21
|
],
|
|
22
22
|
"author": "Kamil Kisiela",
|
|
23
23
|
"license": "MIT",
|
|
24
|
-
"main": "index.js",
|
|
25
|
-
"module": "index.
|
|
26
|
-
"typings": "index.d.ts",
|
|
24
|
+
"main": "cjs/index.js",
|
|
25
|
+
"module": "esm/index.js",
|
|
26
|
+
"typings": "typings/index.d.ts",
|
|
27
27
|
"typescript": {
|
|
28
|
-
"definition": "index.d.ts"
|
|
28
|
+
"definition": "typings/index.d.ts"
|
|
29
29
|
},
|
|
30
30
|
"exports": {
|
|
31
31
|
".": {
|
|
32
|
-
"require":
|
|
33
|
-
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./typings/index.d.cts",
|
|
34
|
+
"default": "./cjs/index.js"
|
|
35
|
+
},
|
|
36
|
+
"import": {
|
|
37
|
+
"types": "./typings/index.d.ts",
|
|
38
|
+
"default": "./esm/index.js"
|
|
39
|
+
},
|
|
40
|
+
"default": {
|
|
41
|
+
"types": "./typings/index.d.ts",
|
|
42
|
+
"default": "./esm/index.js"
|
|
43
|
+
}
|
|
34
44
|
},
|
|
35
45
|
"./*": {
|
|
36
|
-
"require":
|
|
37
|
-
|
|
46
|
+
"require": {
|
|
47
|
+
"types": "./typings/*.d.cts",
|
|
48
|
+
"default": "./cjs/*.js"
|
|
49
|
+
},
|
|
50
|
+
"import": {
|
|
51
|
+
"types": "./typings/*.d.ts",
|
|
52
|
+
"default": "./esm/*.js"
|
|
53
|
+
},
|
|
54
|
+
"default": {
|
|
55
|
+
"types": "./typings/*.d.ts",
|
|
56
|
+
"default": "./esm/*.js"
|
|
57
|
+
}
|
|
38
58
|
},
|
|
39
59
|
"./package.json": "./package.json"
|
|
40
60
|
},
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { DocumentNode, GraphQLSchema } from 'graphql';
|
|
2
|
+
import { ExecutionContextBuilder } from './context';
|
|
3
|
+
import { Application } from './types';
|
|
4
|
+
export interface ApolloRequestContext {
|
|
5
|
+
document: DocumentNode;
|
|
6
|
+
operationName?: string | null;
|
|
7
|
+
context?: any;
|
|
8
|
+
schema: GraphQLSchema;
|
|
9
|
+
request: {
|
|
10
|
+
variables?: {
|
|
11
|
+
[name: string]: any;
|
|
12
|
+
} | null;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export declare function apolloExecutorCreator({ createExecution, }: {
|
|
16
|
+
createExecution: Application['createExecution'];
|
|
17
|
+
}): Application['createApolloExecutor'];
|
|
18
|
+
export declare function apolloSchemaCreator({ createSubscription, contextBuilder, schema, }: {
|
|
19
|
+
createSubscription: Application['createSubscription'];
|
|
20
|
+
contextBuilder: ExecutionContextBuilder;
|
|
21
|
+
schema: GraphQLSchema;
|
|
22
|
+
}): () => GraphQLSchema;
|