ardatan / graphql-mesh

🕸️ GraphQL Federation Framework for any API services such as REST, OpenAPI, Swagger, SOAP, gRPC and more...
https://the-guild.dev/graphql/mesh
MIT License
3.27k stars 335 forks source link

Could Not Find Module for relative imports when mocking #6558

Open alexanderwanyoike opened 7 months ago

alexanderwanyoike commented 7 months ago

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.


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:

/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:

Additional context

williamrijksen commented 7 months ago

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

wurmr commented 4 months ago

Is there a useful workaround for this right now?

I've been creating resolvers that provide mock data and providing them with the additionalResolvers option. The limitation there is that I cannot set preserveResolvers: false in the mock settings, which I'd like to be able to do.

trylas commented 2 months ago

is there any update on this issue? we got the same experience

Stokes06 commented 1 month ago

I also have this issue: import in yaml does not work for the store and the mock custom.

To be more specific, I encounter this bug when using "mesh start" but not when using "mesh dev"