graphql-modules 3.1.2-alpha-20260121024117-e03d9c26de1cfd3971986f82b995963e1cc77056 → 3.1.2-alpha-20260121025807-95ab4fb1994c3cf68c88a7cf234efd8f99f92ed1
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/async-context.browser.mjs +7 -0
- package/cjs/async-context.node.cjs +14 -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 +9 -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/async-context.browser.mjs +7 -0
- package/esm/async-context.node.cjs +14 -0
- package/esm/di/decorators.js +72 -0
- package/esm/di/errors.js +79 -0
- package/esm/di/forward-ref.js +22 -0
- package/esm/di/index.js +4 -0
- package/esm/di/injector.js +168 -0
- package/esm/di/metadata.js +17 -0
- package/esm/di/providers.js +50 -0
- package/esm/di/registry.js +40 -0
- package/esm/di/resolution.js +159 -0
- package/esm/di/utils.js +36 -0
- package/esm/index.js +16 -0
- package/esm/module/factory.js +68 -0
- package/esm/module/metadata.js +107 -0
- package/esm/module/module.js +24 -0
- package/esm/module/resolvers.js +337 -0
- package/esm/module/tokens.js +18 -0
- package/esm/module/type-defs.js +21 -0
- package/esm/module/types.js +0 -0
- package/esm/shared/di.js +0 -0
- package/esm/shared/errors.js +69 -0
- package/esm/shared/gql.js +9 -0
- package/esm/shared/middleware.js +103 -0
- package/esm/shared/types.js +0 -0
- package/esm/shared/utils.js +101 -0
- package/esm/testing/di.js +6 -0
- package/esm/testing/graphql.js +7 -0
- package/esm/testing/index.js +14 -0
- package/esm/testing/test-application.js +62 -0
- package/esm/testing/test-injector.js +18 -0
- package/esm/testing/test-module.js +266 -0
- package/package.json +29 -9
- package/typings/application/apollo.d.ts +22 -0
- package/typings/application/application.d.ts +32 -0
- package/typings/application/context.d.ts +24 -0
- package/typings/application/di.d.ts +22 -0
- package/typings/application/execution.d.ts +8 -0
- package/typings/application/operation-controller.d.ts +5 -0
- package/typings/application/subscription.d.ts +8 -0
- package/typings/application/tokens.d.ts +17 -0
- package/typings/application/types.d.ts +130 -0
- package/typings/di/decorators.d.ts +11 -0
- package/typings/di/errors.d.ts +16 -0
- package/typings/di/forward-ref.d.ts +7 -0
- package/typings/di/index.d.ts +5 -0
- package/typings/di/injector.d.ts +50 -0
- package/typings/di/metadata.d.ts +12 -0
- package/typings/di/providers.d.ts +44 -0
- package/typings/di/registry.d.ts +11 -0
- package/typings/di/resolution.d.ts +63 -0
- package/typings/di/utils.d.ts +8 -0
- package/typings/index.d.ts +13 -0
- package/typings/module/factory.d.ts +16 -0
- package/typings/module/metadata.d.ts +12 -0
- package/typings/module/module.d.ts +22 -0
- package/typings/module/resolvers.d.ts +13 -0
- package/typings/module/tokens.d.ts +18 -0
- package/typings/module/type-defs.d.ts +7 -0
- package/typings/module/types.d.ts +51 -0
- package/typings/shared/di.d.ts +3 -0
- package/typings/shared/errors.d.ts +36 -0
- package/typings/shared/gql.d.ts +2 -0
- package/typings/shared/middleware.d.ts +21 -0
- package/typings/shared/types.d.ts +22 -0
- package/typings/shared/utils.d.ts +12 -0
- package/typings/testing/di.d.ts +2 -0
- package/typings/testing/graphql.d.ts +14 -0
- package/typings/testing/index.d.ts +14 -0
- package/typings/testing/test-application.d.ts +2 -0
- package/typings/testing/test-injector.d.ts +4 -0
- package/typings/testing/test-module.d.ts +10 -0
- package/LICENSE.md +0 -21
- package/index.js +0 -2359
- package/index.mjs +0 -2344
- /package/{application/apollo.d.ts → typings/application/apollo.d.cts} +0 -0
- /package/{application/application.d.ts → typings/application/application.d.cts} +0 -0
- /package/{application/context.d.ts → typings/application/context.d.cts} +0 -0
- /package/{application/di.d.ts → typings/application/di.d.cts} +0 -0
- /package/{application/execution.d.ts → typings/application/execution.d.cts} +0 -0
- /package/{application/operation-controller.d.ts → typings/application/operation-controller.d.cts} +0 -0
- /package/{application/subscription.d.ts → typings/application/subscription.d.cts} +0 -0
- /package/{application/tokens.d.ts → typings/application/tokens.d.cts} +0 -0
- /package/{application/types.d.ts → typings/application/types.d.cts} +0 -0
- /package/{di/decorators.d.ts → typings/di/decorators.d.cts} +0 -0
- /package/{di/errors.d.ts → typings/di/errors.d.cts} +0 -0
- /package/{di/forward-ref.d.ts → typings/di/forward-ref.d.cts} +0 -0
- /package/{di/index.d.ts → typings/di/index.d.cts} +0 -0
- /package/{di/injector.d.ts → typings/di/injector.d.cts} +0 -0
- /package/{di/metadata.d.ts → typings/di/metadata.d.cts} +0 -0
- /package/{di/providers.d.ts → typings/di/providers.d.cts} +0 -0
- /package/{di/registry.d.ts → typings/di/registry.d.cts} +0 -0
- /package/{di/resolution.d.ts → typings/di/resolution.d.cts} +0 -0
- /package/{di/utils.d.ts → typings/di/utils.d.cts} +0 -0
- /package/{index.d.ts → typings/index.d.cts} +0 -0
- /package/{module/factory.d.ts → typings/module/factory.d.cts} +0 -0
- /package/{module/metadata.d.ts → typings/module/metadata.d.cts} +0 -0
- /package/{module/module.d.ts → typings/module/module.d.cts} +0 -0
- /package/{module/resolvers.d.ts → typings/module/resolvers.d.cts} +0 -0
- /package/{module/tokens.d.ts → typings/module/tokens.d.cts} +0 -0
- /package/{module/type-defs.d.ts → typings/module/type-defs.d.cts} +0 -0
- /package/{module/types.d.ts → typings/module/types.d.cts} +0 -0
- /package/{shared/di.d.ts → typings/shared/di.d.cts} +0 -0
- /package/{shared/errors.d.ts → typings/shared/errors.d.cts} +0 -0
- /package/{shared/gql.d.ts → typings/shared/gql.d.cts} +0 -0
- /package/{shared/middleware.d.ts → typings/shared/middleware.d.cts} +0 -0
- /package/{shared/types.d.ts → typings/shared/types.d.cts} +0 -0
- /package/{shared/utils.d.ts → typings/shared/utils.d.cts} +0 -0
- /package/{testing/di.d.ts → typings/testing/di.d.cts} +0 -0
- /package/{testing/graphql.d.ts → typings/testing/graphql.d.cts} +0 -0
- /package/{testing/index.d.ts → typings/testing/index.d.cts} +0 -0
- /package/{testing/test-application.d.ts → typings/testing/test-application.d.cts} +0 -0
- /package/{testing/test-injector.d.ts → typings/testing/test-injector.d.cts} +0 -0
- /package/{testing/test-module.d.ts → typings/testing/test-module.d.cts} +0 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { Type, isClassProvider, isFactoryProvider, } from './providers';
|
|
2
|
+
import { invalidProviderError, noAnnotationError } from './errors';
|
|
3
|
+
import { Key } from './registry';
|
|
4
|
+
import { resolveForwardRef } from './forward-ref';
|
|
5
|
+
import { readInjectableMetadata } from './metadata';
|
|
6
|
+
const _EMPTY_LIST = [];
|
|
7
|
+
export class ResolvedProvider {
|
|
8
|
+
constructor(key, factory) {
|
|
9
|
+
this.key = key;
|
|
10
|
+
this.factory = factory;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export class ResolvedFactory {
|
|
14
|
+
constructor(
|
|
15
|
+
/**
|
|
16
|
+
* Factory function which can return an instance of an object represented by a key.
|
|
17
|
+
*/
|
|
18
|
+
factory,
|
|
19
|
+
/**
|
|
20
|
+
* Arguments (dependencies) to the `factory` function.
|
|
21
|
+
*/
|
|
22
|
+
dependencies,
|
|
23
|
+
/**
|
|
24
|
+
* Methods invoked within ExecutionContext.
|
|
25
|
+
*/
|
|
26
|
+
executionContextIn,
|
|
27
|
+
/**
|
|
28
|
+
* Has onDestroy hook
|
|
29
|
+
*/
|
|
30
|
+
hasOnDestroyHook,
|
|
31
|
+
/**
|
|
32
|
+
* Is Global
|
|
33
|
+
*/
|
|
34
|
+
isGlobal) {
|
|
35
|
+
this.factory = factory;
|
|
36
|
+
this.dependencies = dependencies;
|
|
37
|
+
this.executionContextIn = executionContextIn;
|
|
38
|
+
this.hasOnDestroyHook = hasOnDestroyHook;
|
|
39
|
+
this.isGlobal = isGlobal;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export class Dependency {
|
|
43
|
+
constructor(key, optional) {
|
|
44
|
+
this.key = key;
|
|
45
|
+
this.optional = optional;
|
|
46
|
+
}
|
|
47
|
+
static fromKey(key) {
|
|
48
|
+
return new Dependency(key, false);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export function resolveProviders(providers) {
|
|
52
|
+
const normalized = normalizeProviders(providers, []);
|
|
53
|
+
const resolved = normalized.map(resolveProvider);
|
|
54
|
+
const resolvedProviderMap = mergeResolvedProviders(resolved, new Map());
|
|
55
|
+
return Array.from(resolvedProviderMap.values());
|
|
56
|
+
}
|
|
57
|
+
function resolveProvider(provider) {
|
|
58
|
+
return new ResolvedProvider(Key.get(provider.provide), resolveFactory(provider));
|
|
59
|
+
}
|
|
60
|
+
function mergeResolvedProviders(providers, normalizedProvidersMap) {
|
|
61
|
+
for (let i = 0; i < providers.length; i++) {
|
|
62
|
+
const provider = providers[i];
|
|
63
|
+
normalizedProvidersMap.set(provider.key.id, provider);
|
|
64
|
+
}
|
|
65
|
+
return normalizedProvidersMap;
|
|
66
|
+
}
|
|
67
|
+
function normalizeProviders(providers, res) {
|
|
68
|
+
providers.forEach((token) => {
|
|
69
|
+
if (token instanceof Type) {
|
|
70
|
+
res.push({ provide: token, useClass: token });
|
|
71
|
+
}
|
|
72
|
+
else if (token &&
|
|
73
|
+
typeof token === 'object' &&
|
|
74
|
+
token.provide !== undefined) {
|
|
75
|
+
res.push(token);
|
|
76
|
+
}
|
|
77
|
+
else if (token instanceof Array) {
|
|
78
|
+
normalizeProviders(token, res);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
throw invalidProviderError(token);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
return res;
|
|
85
|
+
}
|
|
86
|
+
function resolveFactory(provider) {
|
|
87
|
+
let factoryFn;
|
|
88
|
+
let resolvedDeps = _EMPTY_LIST;
|
|
89
|
+
let executionContextIn = _EMPTY_LIST;
|
|
90
|
+
let hasOnDestroyHook = false;
|
|
91
|
+
let isGlobal;
|
|
92
|
+
if (isClassProvider(provider)) {
|
|
93
|
+
const useClass = resolveForwardRef(provider.useClass);
|
|
94
|
+
factoryFn = makeFactory(useClass);
|
|
95
|
+
resolvedDeps = dependenciesFor(useClass);
|
|
96
|
+
executionContextIn = executionContextInFor(useClass);
|
|
97
|
+
isGlobal = globalFor(useClass);
|
|
98
|
+
hasOnDestroyHook = typeof useClass.prototype.onDestroy === 'function';
|
|
99
|
+
}
|
|
100
|
+
else if (isFactoryProvider(provider)) {
|
|
101
|
+
factoryFn = provider.useFactory;
|
|
102
|
+
resolvedDeps = constructDependencies(provider.useFactory, provider.deps || []);
|
|
103
|
+
isGlobal = provider.global;
|
|
104
|
+
if (provider.executionContextIn) {
|
|
105
|
+
executionContextIn = provider.executionContextIn;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
factoryFn = () => provider.useValue;
|
|
110
|
+
resolvedDeps = _EMPTY_LIST;
|
|
111
|
+
isGlobal = provider.global;
|
|
112
|
+
}
|
|
113
|
+
return new ResolvedFactory(factoryFn, resolvedDeps, executionContextIn, hasOnDestroyHook, isGlobal !== null && isGlobal !== void 0 ? isGlobal : false);
|
|
114
|
+
}
|
|
115
|
+
function dependenciesFor(type) {
|
|
116
|
+
const { params } = readInjectableMetadata(type, true);
|
|
117
|
+
if (!params) {
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
if (params.some((p) => p.type == null)) {
|
|
121
|
+
throw noAnnotationError(type, params);
|
|
122
|
+
}
|
|
123
|
+
return params.map((p) => extractToken(p, params));
|
|
124
|
+
}
|
|
125
|
+
function executionContextInFor(type) {
|
|
126
|
+
const { options } = readInjectableMetadata(type, true);
|
|
127
|
+
if ((options === null || options === void 0 ? void 0 : options.executionContextIn) &&
|
|
128
|
+
options.executionContextIn !== _EMPTY_LIST) {
|
|
129
|
+
return options === null || options === void 0 ? void 0 : options.executionContextIn;
|
|
130
|
+
}
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
function globalFor(type) {
|
|
134
|
+
var _a;
|
|
135
|
+
const { options } = readInjectableMetadata(type);
|
|
136
|
+
return (_a = options === null || options === void 0 ? void 0 : options.global) !== null && _a !== void 0 ? _a : false;
|
|
137
|
+
}
|
|
138
|
+
function constructDependencies(typeOrFunc, dependencies) {
|
|
139
|
+
if (!dependencies) {
|
|
140
|
+
return dependenciesFor(typeOrFunc);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
const params = dependencies.map((d) => ({ type: d, optional: false }));
|
|
144
|
+
return params.map((t) => extractToken(t, params));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function extractToken(param, params) {
|
|
148
|
+
const token = resolveForwardRef(param.type);
|
|
149
|
+
if (token) {
|
|
150
|
+
return createDependency(token, param.optional);
|
|
151
|
+
}
|
|
152
|
+
throw noAnnotationError(param.type, params);
|
|
153
|
+
}
|
|
154
|
+
function createDependency(token, optional) {
|
|
155
|
+
return new Dependency(Key.get(token), optional);
|
|
156
|
+
}
|
|
157
|
+
function makeFactory(t) {
|
|
158
|
+
return (...args) => new t(...args);
|
|
159
|
+
}
|
package/esm/di/utils.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export const ERROR_TYPE = 'diType';
|
|
2
|
+
export const ERROR_ORIGINAL_ERROR = 'diOriginalError';
|
|
3
|
+
export const ERROR_LOGGER = 'diErrorLogger';
|
|
4
|
+
export function getType(error) {
|
|
5
|
+
return error[ERROR_TYPE];
|
|
6
|
+
}
|
|
7
|
+
export function getOriginalError(error) {
|
|
8
|
+
return error[ERROR_ORIGINAL_ERROR];
|
|
9
|
+
}
|
|
10
|
+
function defaultErrorLogger(console, ...values) {
|
|
11
|
+
// eslint-disable-next-line no-console
|
|
12
|
+
console.error(...values);
|
|
13
|
+
}
|
|
14
|
+
export function getErrorLogger(error) {
|
|
15
|
+
return error[ERROR_LOGGER] || defaultErrorLogger;
|
|
16
|
+
}
|
|
17
|
+
export function wrappedError(message, originalError) {
|
|
18
|
+
const msg = `${message} caused by: ${originalError instanceof Error ? originalError.message : originalError}`;
|
|
19
|
+
const error = Error(msg);
|
|
20
|
+
error[ERROR_ORIGINAL_ERROR] = originalError;
|
|
21
|
+
return error;
|
|
22
|
+
}
|
|
23
|
+
export function stringify(token) {
|
|
24
|
+
if (typeof token === 'string') {
|
|
25
|
+
return token;
|
|
26
|
+
}
|
|
27
|
+
if (token == null) {
|
|
28
|
+
return '' + token;
|
|
29
|
+
}
|
|
30
|
+
if (token.name) {
|
|
31
|
+
return `${token.name}`;
|
|
32
|
+
}
|
|
33
|
+
const res = token.toString();
|
|
34
|
+
const newLineIndex = res.indexOf('\n');
|
|
35
|
+
return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
|
|
36
|
+
}
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// application
|
|
2
|
+
export { createApplication } from './application/application';
|
|
3
|
+
export * from './application/tokens';
|
|
4
|
+
export * from './application/types';
|
|
5
|
+
// modules
|
|
6
|
+
export { createModule } from './module/module';
|
|
7
|
+
export * from './module/types';
|
|
8
|
+
export * from './module/metadata';
|
|
9
|
+
export * from './module/tokens';
|
|
10
|
+
// di
|
|
11
|
+
export { Injector, Inject, Injectable, Optional, ExecutionContext, forwardRef, InjectionToken, Scope, } from './di';
|
|
12
|
+
import './shared/types';
|
|
13
|
+
export { gql } from './shared/gql';
|
|
14
|
+
export * from './shared/di';
|
|
15
|
+
// testing
|
|
16
|
+
export * from './testing';
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { metadataFactory } from './metadata';
|
|
2
|
+
import { createResolvers } from './resolvers';
|
|
3
|
+
import { createTypeDefs } from './type-defs';
|
|
4
|
+
import { MODULE_ID } from './tokens';
|
|
5
|
+
import { ReflectiveInjector, onlySingletonProviders, onlyOperationProviders, } from '../di';
|
|
6
|
+
import { resolveProviders } from './../di/resolution';
|
|
7
|
+
function lazy(getter) {
|
|
8
|
+
let called = false;
|
|
9
|
+
let computedValue;
|
|
10
|
+
return {
|
|
11
|
+
get value() {
|
|
12
|
+
if (!called) {
|
|
13
|
+
called = true;
|
|
14
|
+
computedValue = getter();
|
|
15
|
+
}
|
|
16
|
+
return computedValue;
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export function moduleFactory(config) {
|
|
21
|
+
const typeDefs = createTypeDefs(config);
|
|
22
|
+
const metadata = metadataFactory(typeDefs, config);
|
|
23
|
+
const providers = lazy(() => typeof config.providers === 'function'
|
|
24
|
+
? config.providers()
|
|
25
|
+
: config.providers);
|
|
26
|
+
// Filter providers and keep them this way
|
|
27
|
+
// so we don't do this filtering multiple times.
|
|
28
|
+
// Providers don't change over time, so it's safe to do it.
|
|
29
|
+
const operationProviders = lazy(() => ReflectiveInjector.resolve(onlyOperationProviders(providers.value)));
|
|
30
|
+
const singletonProviders = lazy(() => ReflectiveInjector.resolve(onlySingletonProviders(providers.value)));
|
|
31
|
+
const mod = {
|
|
32
|
+
id: config.id,
|
|
33
|
+
config,
|
|
34
|
+
metadata,
|
|
35
|
+
typeDefs,
|
|
36
|
+
// Factory is called once on application creation,
|
|
37
|
+
// before we even handle GraphQL Operation
|
|
38
|
+
factory(app) {
|
|
39
|
+
const resolvedModule = mod;
|
|
40
|
+
resolvedModule.singletonProviders = singletonProviders.value;
|
|
41
|
+
resolvedModule.operationProviders = operationProviders.value;
|
|
42
|
+
// Create a module-level Singleton injector
|
|
43
|
+
const injector = ReflectiveInjector.createFromResolved({
|
|
44
|
+
name: `Module "${config.id}" (Singleton Scope)`,
|
|
45
|
+
providers: resolvedModule.singletonProviders.concat(resolveProviders([
|
|
46
|
+
{
|
|
47
|
+
// with module's id, useful in Logging and stuff
|
|
48
|
+
provide: MODULE_ID,
|
|
49
|
+
useValue: config.id,
|
|
50
|
+
},
|
|
51
|
+
])),
|
|
52
|
+
parent: app.injector,
|
|
53
|
+
});
|
|
54
|
+
// We attach injector property to existing `mod` object
|
|
55
|
+
// because we want to keep references
|
|
56
|
+
// that are later on used in testing utils
|
|
57
|
+
resolvedModule.injector = injector;
|
|
58
|
+
// Create resolvers object based on module's config
|
|
59
|
+
// It involves wrapping a resolver with middlewares
|
|
60
|
+
// and other things like validation
|
|
61
|
+
resolvedModule.resolvers = createResolvers(config, metadata, {
|
|
62
|
+
middlewareMap: app.middlewares,
|
|
63
|
+
});
|
|
64
|
+
return resolvedModule;
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
return mod;
|
|
68
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { visit, Kind, } from 'graphql';
|
|
2
|
+
export function metadataFactory(typeDefs, config) {
|
|
3
|
+
const implemented = {};
|
|
4
|
+
const extended = {};
|
|
5
|
+
function collectObjectDefinition(node) {
|
|
6
|
+
if (!implemented[node.name.value]) {
|
|
7
|
+
implemented[node.name.value] = [];
|
|
8
|
+
}
|
|
9
|
+
if (node.fields && node.fields.length > 0) {
|
|
10
|
+
implemented[node.name.value].push(...node.fields.map((field) => field.name.value));
|
|
11
|
+
}
|
|
12
|
+
if (node.kind === Kind.OBJECT_TYPE_DEFINITION) {
|
|
13
|
+
implemented[node.name.value].push('__isTypeOf');
|
|
14
|
+
}
|
|
15
|
+
if (node.kind === Kind.OBJECT_TYPE_DEFINITION) {
|
|
16
|
+
implemented[node.name.value].push('__resolveReference');
|
|
17
|
+
implemented[node.name.value].push('__resolveObject');
|
|
18
|
+
}
|
|
19
|
+
if (node.kind === Kind.INTERFACE_TYPE_DEFINITION) {
|
|
20
|
+
implemented[node.name.value].push('__resolveType');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function collectObjectExtension(node) {
|
|
24
|
+
if (node.fields) {
|
|
25
|
+
if (!extended[node.name.value]) {
|
|
26
|
+
extended[node.name.value] = [];
|
|
27
|
+
}
|
|
28
|
+
node.fields.forEach((field) => {
|
|
29
|
+
extended[node.name.value].push(field.name.value);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
for (const doc of typeDefs) {
|
|
34
|
+
visit(doc, {
|
|
35
|
+
// Object
|
|
36
|
+
ObjectTypeDefinition(node) {
|
|
37
|
+
collectObjectDefinition(node);
|
|
38
|
+
},
|
|
39
|
+
ObjectTypeExtension(node) {
|
|
40
|
+
collectObjectExtension(node);
|
|
41
|
+
},
|
|
42
|
+
// Interface
|
|
43
|
+
InterfaceTypeDefinition(node) {
|
|
44
|
+
collectObjectDefinition(node);
|
|
45
|
+
},
|
|
46
|
+
InterfaceTypeExtension(node) {
|
|
47
|
+
collectObjectExtension(node);
|
|
48
|
+
},
|
|
49
|
+
// Union
|
|
50
|
+
UnionTypeDefinition(node) {
|
|
51
|
+
if (!implemented[node.name.value]) {
|
|
52
|
+
implemented[node.name.value] = [];
|
|
53
|
+
}
|
|
54
|
+
if (node.types) {
|
|
55
|
+
implemented[node.name.value].push(...node.types.map((type) => type.name.value));
|
|
56
|
+
}
|
|
57
|
+
implemented[node.name.value].push('__resolveType');
|
|
58
|
+
},
|
|
59
|
+
UnionTypeExtension(node) {
|
|
60
|
+
if (node.types) {
|
|
61
|
+
if (!extended[node.name.value]) {
|
|
62
|
+
extended[node.name.value] = [];
|
|
63
|
+
}
|
|
64
|
+
extended[node.name.value].push(...node.types.map((type) => type.name.value));
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
// Input
|
|
68
|
+
InputObjectTypeDefinition(node) {
|
|
69
|
+
collectObjectDefinition(node);
|
|
70
|
+
},
|
|
71
|
+
InputObjectTypeExtension(node) {
|
|
72
|
+
collectObjectExtension(node);
|
|
73
|
+
},
|
|
74
|
+
// Enum
|
|
75
|
+
EnumTypeDefinition(node) {
|
|
76
|
+
if (node.values) {
|
|
77
|
+
if (!implemented[node.name.value]) {
|
|
78
|
+
implemented[node.name.value] = [];
|
|
79
|
+
}
|
|
80
|
+
implemented[node.name.value].push(...node.values.map((value) => value.name.value));
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
EnumTypeExtension(node) {
|
|
84
|
+
if (node.values) {
|
|
85
|
+
if (!extended[node.name.value]) {
|
|
86
|
+
extended[node.name.value] = [];
|
|
87
|
+
}
|
|
88
|
+
extended[node.name.value].push(...node.values.map((value) => value.name.value));
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
// Scalar
|
|
92
|
+
ScalarTypeDefinition(node) {
|
|
93
|
+
if (!implemented.__scalars) {
|
|
94
|
+
implemented.__scalars = [];
|
|
95
|
+
}
|
|
96
|
+
implemented.__scalars.push(node.name.value);
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
id: config.id,
|
|
102
|
+
typeDefs,
|
|
103
|
+
implements: implemented,
|
|
104
|
+
extends: extended,
|
|
105
|
+
dirname: config.dirname,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { moduleFactory } from './factory';
|
|
2
|
+
/**
|
|
3
|
+
* @api
|
|
4
|
+
* Creates a Module, an element used by Application. Accepts `ModuleConfig`.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { createModule, gql } from 'graphql-modules';
|
|
10
|
+
*
|
|
11
|
+
* export const usersModule = createModule({
|
|
12
|
+
* id: 'users',
|
|
13
|
+
* typeDefs: gql`
|
|
14
|
+
* // GraphQL SDL
|
|
15
|
+
* `,
|
|
16
|
+
* resolvers: {
|
|
17
|
+
* // ...
|
|
18
|
+
* }
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function createModule(config) {
|
|
23
|
+
return moduleFactory(config);
|
|
24
|
+
}
|