graphql-modules 2.1.1-alpha-20220927212124-3ed53042 → 3.0.0-alpha-20220928142355-e1096a88

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.
@@ -16,6 +16,7 @@ export interface InternalAppContext {
16
16
  *
17
17
  * ```typescript
18
18
  * import { createApplication } from 'graphql-modules';
19
+ * import { createHook, executionAsyncId } from 'async_hooks';
19
20
  * import { usersModule } from './users';
20
21
  * import { postsModule } from './posts';
21
22
  * import { commentsModule } from './comments';
@@ -25,7 +26,8 @@ export interface InternalAppContext {
25
26
  * usersModule,
26
27
  * postsModule,
27
28
  * commentsModule
28
- * ]
29
+ * ],
30
+ * executionContext: { createHook, executionAsyncId },
29
31
  * })
30
32
  * ```
31
33
  */
@@ -1,3 +1,10 @@
1
+ export interface ExecutionContextConfig {
2
+ executionAsyncId: () => number;
3
+ createHook(config: {
4
+ init(asyncId: number, _: string, triggerAsyncId: number): void;
5
+ destroy(asyncId: number): void;
6
+ }): void;
7
+ }
1
8
  export interface ExecutionContextPicker {
2
9
  getModuleContext(moduleId: string): GraphQLModules.ModuleContext;
3
10
  getApplicationContext(): GraphQLModules.AppContext;
@@ -7,6 +14,7 @@ export declare const executionContext: {
7
14
  getModuleContext: ExecutionContextPicker['getModuleContext'];
8
15
  getApplicationContext: ExecutionContextPicker['getApplicationContext'];
9
16
  };
10
- export declare function enableExecutionContext(): void;
17
+ export declare function enableExecutionContext(config: ExecutionContextConfig): void;
18
+ export declare function assertExecutionContext(): void | never;
11
19
  export declare function getExecutionContextStore(): Map<number, ExecutionContextPicker>;
12
20
  export declare function getExecutionContextDependencyStore(): Map<number, Set<number>>;
@@ -2,6 +2,7 @@ import { execute, subscribe, DocumentNode, GraphQLSchema, ExecutionResult } from
2
2
  import type { Provider, Injector } from '../di';
3
3
  import type { Resolvers, Module, MockedModule } from '../module/types';
4
4
  import type { MiddlewareMap } from '../shared/middleware';
5
+ import type { ExecutionContextConfig } from './execution-context';
5
6
  import type { ApolloRequestContext } from './apollo';
6
7
  import type { Single } from '../shared/types';
7
8
  import type { InternalAppContext } from './application';
@@ -126,5 +127,20 @@ export interface ApplicationConfig {
126
127
  typeDefs: DocumentNode[];
127
128
  resolvers: Record<string, any>[];
128
129
  }): GraphQLSchema;
130
+ /**
131
+ * Enables ExecutionContext
132
+ *
133
+ * @example
134
+ *
135
+ * ```typescript
136
+ * import { createHook, executionAsyncId } from 'async_hooks';
137
+ *
138
+ * const app = createApplication({
139
+ * modules: [],
140
+ * executionContext: { createHook, executionAsyncId }
141
+ * });
142
+ * ```
143
+ */
144
+ executionContext?: ExecutionContextConfig;
129
145
  }
130
146
  export {};
package/index.js CHANGED
@@ -3,7 +3,6 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  const schema = require('@graphql-tools/schema');
6
- const async_hooks = require('async_hooks');
7
6
  const graphql = require('graphql');
8
7
  const wrap = require('@graphql-tools/wrap');
9
8
  const ramda = require('ramda');
@@ -175,62 +174,6 @@ function isFactoryProvider(provider) {
175
174
  return typeof provider.useFactory !== 'undefined';
176
175
  }
177
176
 
178
- const executionContextStore = new Map();
179
- const executionContextDependencyStore = new Map();
180
- const executionContextHook = async_hooks.createHook({
181
- init(asyncId, _, triggerAsyncId) {
182
- var _a;
183
- // Store same context data for child async resources
184
- const ctx = executionContextStore.get(triggerAsyncId);
185
- if (ctx) {
186
- const dependencies = (_a = executionContextDependencyStore.get(triggerAsyncId)) !== null && _a !== void 0 ? _a : executionContextDependencyStore
187
- .set(triggerAsyncId, new Set())
188
- .get(triggerAsyncId);
189
- dependencies.add(asyncId);
190
- executionContextStore.set(asyncId, ctx);
191
- }
192
- },
193
- destroy(asyncId) {
194
- if (executionContextStore.has(asyncId)) {
195
- executionContextStore.delete(asyncId);
196
- }
197
- },
198
- });
199
- function destroyContextAndItsChildren(id) {
200
- if (executionContextStore.has(id)) {
201
- executionContextStore.delete(id);
202
- }
203
- const deps = executionContextDependencyStore.get(id);
204
- if (deps) {
205
- for (const dep of deps) {
206
- destroyContextAndItsChildren(dep);
207
- }
208
- executionContextDependencyStore.delete(id);
209
- }
210
- }
211
- const executionContext = {
212
- create(picker) {
213
- const id = async_hooks.executionAsyncId();
214
- executionContextStore.set(id, picker);
215
- return function destroyContext() {
216
- destroyContextAndItsChildren(id);
217
- };
218
- },
219
- getModuleContext(moduleId) {
220
- const picker = executionContextStore.get(async_hooks.executionAsyncId());
221
- return picker.getModuleContext(moduleId);
222
- },
223
- getApplicationContext() {
224
- const picker = executionContextStore.get(async_hooks.executionAsyncId());
225
- return picker.getApplicationContext();
226
- },
227
- };
228
- function enableExecutionContext() {
229
- {
230
- executionContextHook.enable();
231
- }
232
- }
233
-
234
177
  function ensureReflect() {
235
178
  if (!(Reflect && Reflect.getOwnMetadata)) {
236
179
  throw 'reflect-metadata shim is required when using class decorators';
@@ -240,7 +183,6 @@ function Injectable(options) {
240
183
  return (target) => {
241
184
  var _a;
242
185
  ensureReflect();
243
- enableExecutionContext();
244
186
  const params = (Reflect.getMetadata('design:paramtypes', target) || []).map((param) => (isType(param) ? param : null));
245
187
  const existingMeta = readInjectableMetadata(target);
246
188
  const meta = {
@@ -912,6 +854,77 @@ function duplicatedGlobalTokenError(provider, modules) {
912
854
  */
913
855
  const CONTEXT = new InjectionToken('context');
914
856
 
857
+ const executionContextStore = new Map();
858
+ const executionContextDependencyStore = new Map();
859
+ let executionAsyncId = () => 0;
860
+ function destroyContextAndItsChildren(id) {
861
+ if (executionContextStore.has(id)) {
862
+ executionContextStore.delete(id);
863
+ }
864
+ const deps = executionContextDependencyStore.get(id);
865
+ if (deps) {
866
+ for (const dep of deps) {
867
+ destroyContextAndItsChildren(dep);
868
+ }
869
+ executionContextDependencyStore.delete(id);
870
+ }
871
+ }
872
+ let executionContextEnabled = false;
873
+ const executionContext = {
874
+ create(picker) {
875
+ if (!executionContextEnabled) {
876
+ return function destroyContextNoop() {
877
+ // noop
878
+ };
879
+ }
880
+ const id = executionAsyncId();
881
+ executionContextStore.set(id, picker);
882
+ return function destroyContext() {
883
+ destroyContextAndItsChildren(id);
884
+ };
885
+ },
886
+ getModuleContext(moduleId) {
887
+ assertExecutionContext();
888
+ const picker = executionContextStore.get(executionAsyncId());
889
+ return picker.getModuleContext(moduleId);
890
+ },
891
+ getApplicationContext() {
892
+ assertExecutionContext();
893
+ const picker = executionContextStore.get(executionAsyncId());
894
+ return picker.getApplicationContext();
895
+ },
896
+ };
897
+ function enableExecutionContext(config) {
898
+ if (!executionContextEnabled) {
899
+ config.createHook({
900
+ init(asyncId, _, triggerAsyncId) {
901
+ var _a;
902
+ // Store same context data for child async resources
903
+ const ctx = executionContextStore.get(triggerAsyncId);
904
+ if (ctx) {
905
+ const dependencies = (_a = executionContextDependencyStore.get(triggerAsyncId)) !== null && _a !== void 0 ? _a : executionContextDependencyStore
906
+ .set(triggerAsyncId, new Set())
907
+ .get(triggerAsyncId);
908
+ dependencies.add(asyncId);
909
+ executionContextStore.set(asyncId, ctx);
910
+ }
911
+ },
912
+ destroy(asyncId) {
913
+ if (executionContextStore.has(asyncId)) {
914
+ executionContextStore.delete(asyncId);
915
+ }
916
+ },
917
+ });
918
+ executionAsyncId = config.executionAsyncId;
919
+ executionContextEnabled = true;
920
+ }
921
+ }
922
+ function assertExecutionContext() {
923
+ if (!executionContextEnabled) {
924
+ throw new Error('Execution Context is not enabled. Please set `executionContext` option in `createApplication`');
925
+ }
926
+ }
927
+
915
928
  function createContextBuilder({ appInjector, modulesMap, appLevelOperationProviders, singletonGlobalProvidersMap, operationGlobalProvidersMap, }) {
916
929
  // This is very critical. It creates an execution context.
917
930
  // It has to run on every operation.
@@ -1218,6 +1231,7 @@ function operationControllerCreator(options) {
1218
1231
  *
1219
1232
  * ```typescript
1220
1233
  * import { createApplication } from 'graphql-modules';
1234
+ * import { createHook, executionAsyncId } from 'async_hooks';
1221
1235
  * import { usersModule } from './users';
1222
1236
  * import { postsModule } from './posts';
1223
1237
  * import { commentsModule } from './comments';
@@ -1227,13 +1241,17 @@ function operationControllerCreator(options) {
1227
1241
  * usersModule,
1228
1242
  * postsModule,
1229
1243
  * commentsModule
1230
- * ]
1244
+ * ],
1245
+ * executionContext: { createHook, executionAsyncId },
1231
1246
  * })
1232
1247
  * ```
1233
1248
  */
1234
1249
  function createApplication(applicationConfig) {
1235
1250
  function applicationFactory(cfg) {
1236
1251
  const config = cfg || applicationConfig;
1252
+ if (config.executionContext) {
1253
+ enableExecutionContext(config.executionContext);
1254
+ }
1237
1255
  const providers = config.providers && typeof config.providers === 'function'
1238
1256
  ? config.providers()
1239
1257
  : config.providers;
package/index.mjs CHANGED
@@ -1,5 +1,4 @@
1
1
  import { makeExecutableSchema } from '@graphql-tools/schema';
2
- import { createHook, executionAsyncId } from 'async_hooks';
3
2
  import { GraphQLSchema, execute as execute$1, subscribe, visit, Kind, GraphQLScalarType, concatAST, defaultFieldResolver, parse } from 'graphql';
4
3
  import { wrapSchema } from '@graphql-tools/wrap';
5
4
  import { mergeDeepWith } from 'ramda';
@@ -172,62 +171,6 @@ function isFactoryProvider(provider) {
172
171
  return typeof provider.useFactory !== 'undefined';
173
172
  }
174
173
 
175
- const executionContextStore = new Map();
176
- const executionContextDependencyStore = new Map();
177
- const executionContextHook = createHook({
178
- init(asyncId, _, triggerAsyncId) {
179
- var _a;
180
- // Store same context data for child async resources
181
- const ctx = executionContextStore.get(triggerAsyncId);
182
- if (ctx) {
183
- const dependencies = (_a = executionContextDependencyStore.get(triggerAsyncId)) !== null && _a !== void 0 ? _a : executionContextDependencyStore
184
- .set(triggerAsyncId, new Set())
185
- .get(triggerAsyncId);
186
- dependencies.add(asyncId);
187
- executionContextStore.set(asyncId, ctx);
188
- }
189
- },
190
- destroy(asyncId) {
191
- if (executionContextStore.has(asyncId)) {
192
- executionContextStore.delete(asyncId);
193
- }
194
- },
195
- });
196
- function destroyContextAndItsChildren(id) {
197
- if (executionContextStore.has(id)) {
198
- executionContextStore.delete(id);
199
- }
200
- const deps = executionContextDependencyStore.get(id);
201
- if (deps) {
202
- for (const dep of deps) {
203
- destroyContextAndItsChildren(dep);
204
- }
205
- executionContextDependencyStore.delete(id);
206
- }
207
- }
208
- const executionContext = {
209
- create(picker) {
210
- const id = executionAsyncId();
211
- executionContextStore.set(id, picker);
212
- return function destroyContext() {
213
- destroyContextAndItsChildren(id);
214
- };
215
- },
216
- getModuleContext(moduleId) {
217
- const picker = executionContextStore.get(executionAsyncId());
218
- return picker.getModuleContext(moduleId);
219
- },
220
- getApplicationContext() {
221
- const picker = executionContextStore.get(executionAsyncId());
222
- return picker.getApplicationContext();
223
- },
224
- };
225
- function enableExecutionContext() {
226
- {
227
- executionContextHook.enable();
228
- }
229
- }
230
-
231
174
  function ensureReflect() {
232
175
  if (!(Reflect && Reflect.getOwnMetadata)) {
233
176
  throw 'reflect-metadata shim is required when using class decorators';
@@ -237,7 +180,6 @@ function Injectable(options) {
237
180
  return (target) => {
238
181
  var _a;
239
182
  ensureReflect();
240
- enableExecutionContext();
241
183
  const params = (Reflect.getMetadata('design:paramtypes', target) || []).map((param) => (isType(param) ? param : null));
242
184
  const existingMeta = readInjectableMetadata(target);
243
185
  const meta = {
@@ -909,6 +851,77 @@ function duplicatedGlobalTokenError(provider, modules) {
909
851
  */
910
852
  const CONTEXT = new InjectionToken('context');
911
853
 
854
+ const executionContextStore = new Map();
855
+ const executionContextDependencyStore = new Map();
856
+ let executionAsyncId = () => 0;
857
+ function destroyContextAndItsChildren(id) {
858
+ if (executionContextStore.has(id)) {
859
+ executionContextStore.delete(id);
860
+ }
861
+ const deps = executionContextDependencyStore.get(id);
862
+ if (deps) {
863
+ for (const dep of deps) {
864
+ destroyContextAndItsChildren(dep);
865
+ }
866
+ executionContextDependencyStore.delete(id);
867
+ }
868
+ }
869
+ let executionContextEnabled = false;
870
+ const executionContext = {
871
+ create(picker) {
872
+ if (!executionContextEnabled) {
873
+ return function destroyContextNoop() {
874
+ // noop
875
+ };
876
+ }
877
+ const id = executionAsyncId();
878
+ executionContextStore.set(id, picker);
879
+ return function destroyContext() {
880
+ destroyContextAndItsChildren(id);
881
+ };
882
+ },
883
+ getModuleContext(moduleId) {
884
+ assertExecutionContext();
885
+ const picker = executionContextStore.get(executionAsyncId());
886
+ return picker.getModuleContext(moduleId);
887
+ },
888
+ getApplicationContext() {
889
+ assertExecutionContext();
890
+ const picker = executionContextStore.get(executionAsyncId());
891
+ return picker.getApplicationContext();
892
+ },
893
+ };
894
+ function enableExecutionContext(config) {
895
+ if (!executionContextEnabled) {
896
+ config.createHook({
897
+ init(asyncId, _, triggerAsyncId) {
898
+ var _a;
899
+ // Store same context data for child async resources
900
+ const ctx = executionContextStore.get(triggerAsyncId);
901
+ if (ctx) {
902
+ const dependencies = (_a = executionContextDependencyStore.get(triggerAsyncId)) !== null && _a !== void 0 ? _a : executionContextDependencyStore
903
+ .set(triggerAsyncId, new Set())
904
+ .get(triggerAsyncId);
905
+ dependencies.add(asyncId);
906
+ executionContextStore.set(asyncId, ctx);
907
+ }
908
+ },
909
+ destroy(asyncId) {
910
+ if (executionContextStore.has(asyncId)) {
911
+ executionContextStore.delete(asyncId);
912
+ }
913
+ },
914
+ });
915
+ executionAsyncId = config.executionAsyncId;
916
+ executionContextEnabled = true;
917
+ }
918
+ }
919
+ function assertExecutionContext() {
920
+ if (!executionContextEnabled) {
921
+ throw new Error('Execution Context is not enabled. Please set `executionContext` option in `createApplication`');
922
+ }
923
+ }
924
+
912
925
  function createContextBuilder({ appInjector, modulesMap, appLevelOperationProviders, singletonGlobalProvidersMap, operationGlobalProvidersMap, }) {
913
926
  // This is very critical. It creates an execution context.
914
927
  // It has to run on every operation.
@@ -1215,6 +1228,7 @@ function operationControllerCreator(options) {
1215
1228
  *
1216
1229
  * ```typescript
1217
1230
  * import { createApplication } from 'graphql-modules';
1231
+ * import { createHook, executionAsyncId } from 'async_hooks';
1218
1232
  * import { usersModule } from './users';
1219
1233
  * import { postsModule } from './posts';
1220
1234
  * import { commentsModule } from './comments';
@@ -1224,13 +1238,17 @@ function operationControllerCreator(options) {
1224
1238
  * usersModule,
1225
1239
  * postsModule,
1226
1240
  * commentsModule
1227
- * ]
1241
+ * ],
1242
+ * executionContext: { createHook, executionAsyncId },
1228
1243
  * })
1229
1244
  * ```
1230
1245
  */
1231
1246
  function createApplication(applicationConfig) {
1232
1247
  function applicationFactory(cfg) {
1233
1248
  const config = cfg || applicationConfig;
1249
+ if (config.executionContext) {
1250
+ enableExecutionContext(config.executionContext);
1251
+ }
1234
1252
  const providers = config.providers && typeof config.providers === 'function'
1235
1253
  ? config.providers()
1236
1254
  : config.providers;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-modules",
3
- "version": "2.1.1-alpha-20220927212124-3ed53042",
3
+ "version": "3.0.0-alpha-20220928142355-e1096a88",
4
4
  "description": "Create reusable, maintainable, testable and extendable GraphQL modules",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {