Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New version of webpack creating GraphQL schema conflicts on NestJS app when optimization is true #12208

Closed
EByrdS opened this issue Sep 23, 2022 · 21 comments
Assignees
Labels
outdated scope: node Issues related to Node, Express, NestJS support for Nx scope: react Issues related to React support for Nx type: bug

Comments

@EByrdS
Copy link

EByrdS commented Sep 23, 2022

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. <3

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.
@AgentEnder AgentEnder added blocked: retry with latest Retry with latest release or head. scope: react Issues related to React support for Nx scope: node Issues related to Node, Express, NestJS support for Nx and removed blocked: retry with latest Retry with latest release or head. labels Sep 23, 2022
@SmoshySmosh
Copy link
Contributor

+1. I am also experiencing this issue.

@exsesx
Copy link

exsesx commented Oct 3, 2022

+1

@exsesx
Copy link

exsesx commented Oct 3, 2022

Has anyone found a workaround yet?

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

@scomans
Copy link

scomans commented Oct 4, 2022

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;
};

@maikknebel
Copy link

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
    }
  })
}

@sgomanf
Copy link

sgomanf commented Oct 9, 2022

same issue, even whit nx serve projecname --prod

@synerp
Copy link

synerp commented Oct 10, 2022

@jaysoo Hi there, I think this is tagged incorrectly as Scope: React. This is NestJS not NextJS.

Mine was user error. I had a shared library that had @ObjectType in it 😄

@EByrdS
Copy link
Author

EByrdS commented Oct 12, 2022

I think we will probably need to change @nrwl/node to @nrwl/webpack.

Its documentation is not ready yet: https://nx.dev/packages/webpack

@jeffminsungkim
Copy link

This is so annoying. I don't really wanna touch the config file(workaround) in the middle of the first deployment of my app. I believe the Nx team should take care of this issue.

@EByrdS
Copy link
Author

EByrdS commented Oct 27, 2022

Any updates on this issue? I have tried the default configuration when creating a new app, using @nrwl/webpack and it is still failing:

    "build": {
      "executor": "@nrwl/webpack:webpack",  // <- it was originally @nrwl/node:webpack
      "outputs": ["{options.outputPath}"],
      "options": {
        "target": "node",
        "compiler": "tsc",
        "outputPath": "dist/packages/backend",
        "main": "<path-to-main>",
        "tsConfig": "<path-to-tsconfig>",
        "assets": ["<path-to-assets>"],
        "generatePackageJson": true
      },
      "configurations": {
        "production": {
          "optimization": true,
          "extractLicenses": true,
          "inspect": false
        }
      }
    },

Currently tested with all @nrwl/ and nx packages with version 15.0.3.

@sgomanf
Copy link

sgomanf commented Oct 27, 2022

changing "optimization" to false work for me

@SmoshySmosh
Copy link
Contributor

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.

@craigdg
Copy link

craigdg commented Nov 4, 2022

Changing optimisation to false works but it's not a long term solution, do we have an idea of when a fix might be made for this? Downgrading @nrwl/node to 14.7.5 also fixes whilst letting you retain "optimization": true, but still not ideal.

@EByrdS
Copy link
Author

EByrdS commented Nov 4, 2022

Small update: Still happening on latest version 15.0.10 - EByrdS/nx-webpack-bug#2

@synerp
Copy link

synerp commented Nov 9, 2022

Whats up @nartc ?

@EByrdS
Copy link
Author

EByrdS commented Nov 9, 2022

I think it is working fine on versions 15.0.13 of nx and @nrwl/webpack. Can anyone else confirm? If it is working we could close this issue.

@synerp
Copy link

synerp commented Jan 16, 2023

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.

@jaysoo
Copy link
Member

jaysoo commented Jan 17, 2023

We are doing some work around Node server frameworks, and the recommended default is esbuild (using @nrwl/esbuild).

If you are using webpack, you can try compiler: 'swc' in the project build options.

@jaysoo
Copy link
Member

jaysoo commented Jan 17, 2023

This PR makes it so by default, Node apps do not optimize through Terser (which is slower): #14431.

You can pass --optimization=true|false to turn it on/off. Will be in the next patch release this week or next week

@jaysoo
Copy link
Member

jaysoo commented Jan 27, 2023

Closing this issue as it is fixed.

@jaysoo jaysoo closed this as completed Jan 27, 2023
@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: node Issues related to Node, Express, NestJS support for Nx scope: react Issues related to React support for Nx type: bug
Projects
None yet
Development

No branches or pull requests