graphql-mesh icon indicating copy to clipboard operation
graphql-mesh copied to clipboard

Could Not Find Module for relative imports when mocking

Open alexanderwanyoike opened this issue 4 months ago • 1 comments

Issue workflow progress

Progress of the issue based on the Contributor Workflow

Make sure to fork this template and run yarn generate in the terminal.

Please make sure Mesh package versions under package.json matches yours.

  • [ ] 2. A failing test has been provided
  • [ ] 3. A local solution has been provided
  • [ ] 4. A pull request is pending review

Describe the bug

Currently trying to follow the example specified in the documentation to mock a GraphQL query. However, I ran into the following error when I try access the mesh after building it and starting the application. Things to note:

  • The mesh builds correctly
  • I am able to start the mesh
  • However when I access the mesh it fails with The following error
/Users/****/src/gateway/dist/apps/graphql-mesh/webpack:/nodejs-express-template/.mesh/index.ts:16882
      return Promise.reject(new Error(`Cannot find module '${relativeModuleId}'.`));
                            ^
Error: Cannot find module 'mocks/userMocks.ts'.
    at importFn (/Users/***/src/gateway/dist/apps/graphql-mesh/webpack:/nodejs-express-template/.mesh/index.ts:16882:29)
    at tryImport (/Users/***/src/gateway/node_modules/@graphql-mesh/utils/cjs/load-from-module-export-expression.js:26:20)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at loadFromModuleExportExpression (/Users/***/src/gateway/node_modules/@graphql-mesh/utils/cjs/load-from-module-export-expression.js:13:17)
Waiting for the debugger to disconnect...

It seems that mesh cannot find the module defined in the root directory ./mocks/userMocks.ts

To Reproduce Steps to reproduce the behavior:

  1. Create a mesh with the following mock configuration in the ./.meshrc
#... redacted

plugins:
  - mock:
      mocks:
        - apply: Query.user.events
          custom: './mocks/userMocks.ts#events'
  1. Create a mock file in the location ./mocks/userMocks.ts
export default {
  events: [
    {
      id: '1',
      title: 'Test Event',
    },
    {
      id: '2',
      title: 'Test Event 2',
    },
  ],
}
  1. Run mesh build
  2. Run mesh start
  3. Access the mesh endpoint localhost:4000/graphql

Expected behavior

  1. The mock file should be found as expected

Environment:

  • OS: Mac OSX Ventura 13.6
  • "@graphql-mesh/cli": "^0.88.1",
  • "@graphql-mesh/graphql": "^0.96.0",
  • "@graphql-mesh/plugin-hive": "^0.96.5",
  • "@graphql-mesh/plugin-mock": "^0.96.4",
  • "@graphql-mesh/transform-filter-schema": "^0.96.0",
  • "@graphql-mesh/transform-hoist-field": "^0.96.4",
  • "@graphql-mesh/transform-prefix": "^0.96.0",
  • "@graphql-mesh/transform-rename": "^0.96.0",
  • NodeJS: 18.17.1

Additional context

  • We are running our mesh as graphql handler from within a nestjs controller however I tried to run this without nestjs and got the same error

alexanderwanyoike avatar Feb 09 '24 16:02 alexanderwanyoike

We're experiencing the same issue, also when using the initializeStore functionality.

williamrijksen avatar Feb 19 '24 11:02 williamrijksen