graphql-modules: Cannot read properties of undefined (reading 'getModuleContext') when using @ExecutionContext()

When using @ExecutionContext() in a provider I get the error:

TypeError: Cannot read properties of undefined (reading 'getModuleContext')
      at Object.getModuleContext (/Users/alexander/graphql-modules/node_modules/graphql-modules/index.js:221:23)
      at ReflectiveInjector.moduleExecutionContextGetter [as _executionContextGetter] (/Users/alexander/graphql-modules/node_modules/graphql-modules/index.js:945:41)
      at MyProvider.get (/Users/alexander/graphql-modules/node_modules/graphql-modules/index.js:660:41)
      at MyProvider.<anonymous> (/Users/alexander/graphql-modules/src/module.ts:9:22)
      at Generator.next (<anonymous>)
      at /Users/alexander/graphql-modules/src/module.ts:17:71
      at new Promise (<anonymous>)
      at __awaiter (/Users/alexander/graphql-modules/src/module.ts:13:12)
      at MyProvider.create (/Users/alexander/graphql-modules/src/module.ts:25:16)
      at hello (/Users/alexander/graphql-modules/src/module.ts:27:30),
  [Symbol(async_id_symbol)]: 75,
  [Symbol(trigger_async_id_symbol)]: 74

To Reproduce Steps to reproduce the behavior:

Check out the codesandbox to see the error: https://codesandbox.io/s/graphql-modules-jxwdrv?file=/src/module.ts

  • open a terminal
  • run npm start
  • open https://…io/graphql
  • run query { hello }

Expected behavior

Context should be available in provider.

Environment:

  • graphql-modules: 2.1.0
  • NodeJS: 18.7.0

Additional context

To me the problem seems to be that async_hooks.executionAsyncId() returns a different id in executionContext.getModuleContext than in executionContext.create.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 20 (6 by maintainers)

Most upvoted comments

Fixed in graphql-modules@2.2.1 with #2461. Please try it out!

@darkbasic rebased. New alpha versions will be available soon.

I confirm 2.3.0 fixes the issue, thanks! I’ve rebased the 3.0 PR on top of your work, can you please force push from darkbasic:apollo-v4 into apollo-v4 so that new 3.0 alpha packages get created?

hey @darkbasic, I am currently debugging this. I did what you said here, but I also updated the graphql-modules in graphql-server-typescript to 2.2.0 (latest stable release). Doing so, I got a different runtime error (in addition to a bunch of type errors I ignored) when trying to run the server:

$ node -r ts-node/register/transpile-only src/index.ts

/Users/enisdenjo/Develop/src/github.com/accounts-js/accounts/node_modules/graphql-modules/index.js:79
    const error = (originalError ? wrappedError('', originalError) : Error());
                                                                     ^
Error: Cannot instantiate cyclic dependency! (AccountsServer -> AccountsServer) - in Module "accounts-core" (Singleton Scope)
    at injectionError (/Users/enisdenjo/Develop/src/github.com/accounts-js/accounts/node_modules/graphql-modules/index.js:79:70)
    at cyclicDependencyError (/Users/enisdenjo/Develop/src/github.com/accounts-js/accounts/node_modules/graphql-modules/index.js:62:12)
    at ReflectiveInjector._new (/Users/enisdenjo/Develop/src/github.com/accounts-js/accounts/node_modules/graphql-modules/index.js:702:19)
    at ReflectiveInjector._getObjByKeyId (/Users/enisdenjo/Develop/src/github.com/accounts-js/accounts/node_modules/graphql-modules/index.js:645:42)
    at ReflectiveInjector._getObjByKeyId (/Users/enisdenjo/Develop/src/github.com/accounts-js/accounts/examples/graphql-server-typescript/node_modules/graphql-modules/index.js:637:103)
    at getObj (/Users/enisdenjo/Develop/src/github.com/accounts-js/accounts/examples/graphql-server-typescript/node_modules/graphql-modules/index.js:601:34)
    at ReflectiveInjector._getByKey (/Users/enisdenjo/Develop/src/github.com/accounts-js/accounts/examples/graphql-server-typescript/node_modules/graphql-modules/index.js:609:31)
    at ReflectiveInjector.get (/Users/enisdenjo/Develop/src/github.com/accounts-js/accounts/examples/graphql-server-typescript/node_modules/graphql-modules/index.js:591:21)
    at ReflectiveInjector._getByKey (/Users/enisdenjo/Develop/src/github.com/accounts-js/accounts/node_modules/graphql-modules/index.js:625:24)
    at ReflectiveInjector._getByDependency (/Users/enisdenjo/Develop/src/github.com/accounts-js/accounts/node_modules/graphql-modules/index.js:697:21) {
  addKey: [Function: addKey],
  keys: [
    Key { token: [Function], id: 1 },
    Key { token: [Function], id: 1 },
    Key { token: [Function], id: 1 },
    Key { token: [Function], id: 1 }
  ],
  constructResolvingMessage: [Function: wrappedConstructResolvingMessage],
  diOriginalError: undefined
}

Any chance you can try the latest stable 2.2.0 of graphql-modules yourself? The latest works in the repro here. Looking forward to your reply, thanks!

Same here, it works in some cases and doesn’t in other, for some reason executionContextStore is empty which is causing that error

 getModuleContext(moduleId) {
        const picker = executionContextStore.get(executionAsyncId());
       // picker is undefined  because executionContextStore is empty
        return picker.getModuleContext(moduleId);
    },

Has anyone figured it out yet ?