nx: New version of webpack creating GraphQL schema conflicts on NestJS app when optimization is true

Current Behavior

Migrating @nrwl/node from version "14.7.5" to "14.7.10" or "14.7.11" creates a name collision when building a @nestjs/graphql app in production mode ("optimization": true).

Expected Behavior

Updating @nrwl/node from version "14.7.5" to version "14.7.11" should still build the NestJS app as previously.

Steps to Reproduce

Minimal example repo: nx-webpack-bug

Install all dependencies and run yarn nx serve backend --prod.

It will fail with a message: Error: Schema must contain uniquely named types but contains multiple types named "l".

Change the version of @nrwl/node to "14.7.5", then change the build executor of the apps/backend/project.json file from @nrwl/webpack:webpack to @nrwl/node:webpack. Running yarn nx serve backend --prod will succeed.

When optimization is true, some GraphQL names end up having the same name, and so the process fails. This was most likely introduced with the latest changes of moving the webpack plugin.

I have gotten far in creating a minimally reproducible example, so I feel close to the issue. If I get some minimal direction I would love to dig deeper and provide a PR to solve it. ❤️

Failure Logs

=> yarn nx serve backend --prod


yarn run v1.22.19
$ /Users/my-user/Code/example/debug/node_modules/.bin/nx serve backend --prod

> nx run backend:serve:production

chunk (runtime: main) main.js (main) 5.01 KiB [entry] [rendered]
webpack compiled successfully (eead8f67e9d7eaa9)
Debugger listening on ws://localhost:9229/e5718c63-071a-4300-ab4f-279625903eda
Debugger listening on ws://localhost:9229/e5718c63-071a-4300-ab4f-279625903eda
For help, see: https://nodejs.org/en/docs/inspector
[Nest] 50987  - 23/09/2022, 16:29:37     LOG [NestFactory] Starting Nest application...
[Nest] 50987  - 23/09/2022, 16:29:37     LOG [InstanceLoader] n dependencies initialized +18ms
[Nest] 50987  - 23/09/2022, 16:29:37     LOG [InstanceLoader] i dependencies initialized +0ms
[Nest] 50987  - 23/09/2022, 16:29:37     LOG [InstanceLoader] GraphQLSchemaBuilderModule dependencies initialized +1ms
[Nest] 50987  - 23/09/2022, 16:29:37     LOG [InstanceLoader] GraphQLModule dependencies initialized +0ms

/Users/my-user/Code/example/debug/node_modules/graphql/type/schema.js:219
        throw new Error(
              ^
Error: Schema must contain uniquely named types but contains multiple types named "l".
    at new GraphQLSchema (/Users/my-user/Code/example/debug/node_modules/graphql/type/schema.js:219:15)
    at GraphQLSchemaFactory.create (/Users/my-user/Code/example/debug/node_modules/@nestjs/graphql/dist/schema-builder/graphql-schema.factory.js:40:24)
    at GraphQLSchemaBuilder.generateSchema (/Users/my-user/Code/example/debug/node_modules/@nestjs/graphql/dist/graphql-schema.builder.js:35:52)
    at GraphQLSchemaBuilder.build (/Users/my-user/Code/example/debug/node_modules/@nestjs/graphql/dist/graphql-schema.builder.js:22:31)
    at GraphQLFactory.mergeWithSchema (/Users/my-user/Code/example/debug/node_modules/@nestjs/graphql/dist/graphql.factory.js:29:69)
    at ApolloDriver.start (/Users/my-user/Code/example/debug/node_modules/@nestjs/apollo/dist/drivers/apollo.driver.js:19:51)
    at GraphQLModule.onModuleInit (/Users/my-user/Code/example/debug/node_modules/@nestjs/graphql/dist/graphql.module.js:105:36)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at callModuleInitHook (/Users/my-user/Code/example/debug/node_modules/@nestjs/core/hooks/on-module-init.hook.js:51:9)
    at NestApplication.callInitHook (/Users/my-user/Code/example/debug/node_modules/@nestjs/core/nest-application-context.js:178:13)

Environment

MacOS Monterey v12.3.1 (Apple M1 Max)

node v16.16.0
yarn v1.22.19

"nx": "14.7.11"
"@nrwl/node": "14.7.11"
"@nrwl/nest": "14.7.11"
"@nestjs/apollo": "^10.1.0"
"@nestjs/graphql": "^10.1.2"
// See package.json for other dependencies
=> yarn nx report

yarn run v1.22.19
$ /Users/my-user/Code/example/debug/node_modules/.bin/nx report

 >  NX   Report complete - copy this into the issue template

   Node : 16.16.0
   OS   : darwin arm64
   yarn : 1.22.19
   
   nx : 14.7.11
   @nrwl/angular : Not Found
   @nrwl/cypress : Not Found
   @nrwl/detox : Not Found
   @nrwl/devkit : 14.7.11
   @nrwl/eslint-plugin-nx : 14.7.11
   @nrwl/expo : Not Found
   @nrwl/express : Not Found
   @nrwl/jest : 14.7.11
   @nrwl/js : 14.7.11
   @nrwl/linter : 14.7.11
   @nrwl/nest : 14.7.11
   @nrwl/next : Not Found
   @nrwl/node : 14.7.11
   @nrwl/nx-cloud : 14.6.2
   @nrwl/nx-plugin : Not Found
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : Not Found
   @nrwl/web : Not Found
   @nrwl/workspace : 14.7.11
   typescript : 4.8.3
   ---------------------------------------
   Local workspace plugins:
   ---------------------------------------
   Community plugins:

✨  Done in 0.42s.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 8
  • Comments: 21 (5 by maintainers)

Most upvoted comments

As a workaround you can use a custom webpack config for your nest projects and disable minimize:

module.exports = (config, context) => {
  config.optimization = { minimize: false };
  return config;
};

changing “optimization” to false work for me

Same for me, but it’s not a long term solution as it does not minify the production release.

I finally migrated to 15.5.1 and it did not cause an error - It is alot slower tho. Makes me want to try something other than webpack like vite or swc with nestjs.

I have the same problem in my project but with nestjs and mongoose. Once i build with for prod (nx build foobar --prod) and do node dist/apps/foobar/main.js the error appears. nx serve foobar works fine, as well as all test. The workarround from @scomans works. Here my implementation with TS:

// apps/foobar/project.json
{
  //...
  targets: {
    build: {
      executor: "@nrwl/webpack:webpack",
      // ...
      configurations: {
        production: {
           // ...
          webpackConfig: "apps/foobar/webpack.config.ts"
       }
     }
    }
  }
}
// apps/foobar/webpack.config.ts
import merge from "webpack-merge";

export default function(config, contex) {
  return merge(config, {
    optimization: {
      minimize: false
    }
  })
}

Has anyone found a workaround yet?

I think I’ll just downgrade to node:webpack executor for now.