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