graphql-modules 2.4.1-alpha-20241223151142-a8f6c9260884be8144dbc44aa9db54009b5f041d → 2.4.1-alpha-20241224130831-55ecc95f434893991314f7e466638fe302dc7739

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.
@@ -0,0 +1,6 @@
1
+ export interface AsyncContext {
2
+ getApplicationContext(): GraphQLModules.AppContext;
3
+ getModuleContext(moduleId: string): GraphQLModules.ModuleContext;
4
+ }
5
+ export declare function getAsyncContext(): AsyncContext | undefined;
6
+ export declare function runWithAsyncContext<R, TArgs extends any[]>(asyncContext: AsyncContext, callback: (...args: TArgs) => R, ...args: TArgs): R;
package/index.js CHANGED
@@ -2,9 +2,11 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
+
5
7
  const schema = require('@graphql-tools/schema');
6
8
  const graphql = require('graphql');
7
- const async_hooks = require('async_hooks');
9
+ const module$1 = _interopDefault(require('module'));
8
10
  const wrap = require('@graphql-tools/wrap');
9
11
  const ramda = require('ramda');
10
12
 
@@ -838,6 +840,23 @@ function duplicatedGlobalTokenError(provider, modules) {
838
840
  ].join(' '));
839
841
  }
840
842
 
843
+ let alc;
844
+ if (typeof process !== 'undefined') {
845
+ // probably nodejs runtime
846
+ const require = module$1.createRequire('file:///' /** path is not relevant since we're only loading a builtin */);
847
+ const hooks = require('async_hooks');
848
+ alc = new hooks.AsyncLocalStorage();
849
+ }
850
+ function getAsyncContext() {
851
+ return alc === null || alc === void 0 ? void 0 : alc.getStore();
852
+ }
853
+ function runWithAsyncContext(asyncContext, callback, ...args) {
854
+ if (!alc) {
855
+ return callback(...args);
856
+ }
857
+ return alc.run(asyncContext, callback, ...args);
858
+ }
859
+
841
860
  /**
842
861
  * @api
843
862
  * `CONTEXT` is an InjectionToken representing the provided `GraphQLModules.GlobalContext`
@@ -855,7 +874,6 @@ function duplicatedGlobalTokenError(provider, modules) {
855
874
  */
856
875
  const CONTEXT = new InjectionToken('context');
857
876
 
858
- const alc = new async_hooks.AsyncLocalStorage();
859
877
  function createContextBuilder({ appInjector, modulesMap, appLevelOperationProviders, singletonGlobalProvidersMap, operationGlobalProvidersMap, }) {
860
878
  // This is very critical. It creates an execution context.
861
879
  // It has to run on every operation.
@@ -885,12 +903,12 @@ function createContextBuilder({ appInjector, modulesMap, appLevelOperationProvid
885
903
  });
886
904
  appInjector.setExecutionContextGetter(function executionContextGetter() {
887
905
  var _a;
888
- return ((_a = alc.getStore()) === null || _a === void 0 ? void 0 : _a.getApplicationContext()) || appContext;
906
+ return ((_a = getAsyncContext()) === null || _a === void 0 ? void 0 : _a.getApplicationContext()) || appContext;
889
907
  });
890
908
  function createModuleExecutionContextGetter(moduleId) {
891
909
  return function moduleExecutionContextGetter() {
892
910
  var _a;
893
- return (((_a = alc.getStore()) === null || _a === void 0 ? void 0 : _a.getModuleContext(moduleId)) ||
911
+ return (((_a = getAsyncContext()) === null || _a === void 0 ? void 0 : _a.getModuleContext(moduleId)) ||
894
912
  getModuleContext(moduleId, context));
895
913
  };
896
914
  }
@@ -978,7 +996,7 @@ function createContextBuilder({ appInjector, modulesMap, appLevelOperationProvid
978
996
  return {
979
997
  ...env,
980
998
  runWithContext(cb) {
981
- return alc.run({
999
+ return runWithAsyncContext({
982
1000
  getApplicationContext() {
983
1001
  return appContext;
984
1002
  },
package/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { makeExecutableSchema } from '@graphql-tools/schema';
2
2
  import { GraphQLSchema, execute as execute$1, subscribe, visit, Kind, GraphQLScalarType, concatAST, defaultFieldResolver, parse } from 'graphql';
3
- import { AsyncLocalStorage } from 'async_hooks';
3
+ import module$1 from 'module';
4
4
  import { wrapSchema } from '@graphql-tools/wrap';
5
5
  import { mergeDeepWith } from 'ramda';
6
6
 
@@ -835,6 +835,23 @@ function duplicatedGlobalTokenError(provider, modules) {
835
835
  ].join(' '));
836
836
  }
837
837
 
838
+ let alc;
839
+ if (typeof process !== 'undefined') {
840
+ // probably nodejs runtime
841
+ const require = module$1.createRequire('file:///' /** path is not relevant since we're only loading a builtin */);
842
+ const hooks = require('async_hooks');
843
+ alc = new hooks.AsyncLocalStorage();
844
+ }
845
+ function getAsyncContext() {
846
+ return alc === null || alc === void 0 ? void 0 : alc.getStore();
847
+ }
848
+ function runWithAsyncContext(asyncContext, callback, ...args) {
849
+ if (!alc) {
850
+ return callback(...args);
851
+ }
852
+ return alc.run(asyncContext, callback, ...args);
853
+ }
854
+
838
855
  /**
839
856
  * @api
840
857
  * `CONTEXT` is an InjectionToken representing the provided `GraphQLModules.GlobalContext`
@@ -852,7 +869,6 @@ function duplicatedGlobalTokenError(provider, modules) {
852
869
  */
853
870
  const CONTEXT = new InjectionToken('context');
854
871
 
855
- const alc = new AsyncLocalStorage();
856
872
  function createContextBuilder({ appInjector, modulesMap, appLevelOperationProviders, singletonGlobalProvidersMap, operationGlobalProvidersMap, }) {
857
873
  // This is very critical. It creates an execution context.
858
874
  // It has to run on every operation.
@@ -882,12 +898,12 @@ function createContextBuilder({ appInjector, modulesMap, appLevelOperationProvid
882
898
  });
883
899
  appInjector.setExecutionContextGetter(function executionContextGetter() {
884
900
  var _a;
885
- return ((_a = alc.getStore()) === null || _a === void 0 ? void 0 : _a.getApplicationContext()) || appContext;
901
+ return ((_a = getAsyncContext()) === null || _a === void 0 ? void 0 : _a.getApplicationContext()) || appContext;
886
902
  });
887
903
  function createModuleExecutionContextGetter(moduleId) {
888
904
  return function moduleExecutionContextGetter() {
889
905
  var _a;
890
- return (((_a = alc.getStore()) === null || _a === void 0 ? void 0 : _a.getModuleContext(moduleId)) ||
906
+ return (((_a = getAsyncContext()) === null || _a === void 0 ? void 0 : _a.getModuleContext(moduleId)) ||
891
907
  getModuleContext(moduleId, context));
892
908
  };
893
909
  }
@@ -975,7 +991,7 @@ function createContextBuilder({ appInjector, modulesMap, appLevelOperationProvid
975
991
  return {
976
992
  ...env,
977
993
  runWithContext(cb) {
978
- return alc.run({
994
+ return runWithAsyncContext({
979
995
  getApplicationContext() {
980
996
  return appContext;
981
997
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-modules",
3
- "version": "2.4.1-alpha-20241223151142-a8f6c9260884be8144dbc44aa9db54009b5f041d",
3
+ "version": "2.4.1-alpha-20241224130831-55ecc95f434893991314f7e466638fe302dc7739",
4
4
  "description": "Create reusable, maintainable, testable and extendable GraphQL modules",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {