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

nestJS + graphql code first + ncc = Error: Generating schema error #377

Closed
jogelin opened this issue May 8, 2019 · 20 comments
Closed

nestJS + graphql code first + ncc = Error: Generating schema error #377

jogelin opened this issue May 8, 2019 · 20 comments

Comments

@jogelin
Copy link

jogelin commented May 8, 2019

I would like to use ncc to reduce my docker image size by removing the dependency to node_modules but I have a runtime issue :

[Nest] 19257   - 05/08/2019, 10:44 AM   [InstanceLoader] ConfigModule dependencies initialized +18ms
[Nest] 19257   - 05/08/2019, 10:44 AM   [InstanceLoader] ApiModule dependencies initialized +1ms
[Nest] 19257   - 05/08/2019, 10:44 AM   [InstanceLoader] GraphQLModule dependencies initialized +1ms
[Nest] 19257   - 05/08/2019, 10:44 AM   [RoutesResolver] ConfigController {/api/config}: +4ms
[Nest] 19257   - 05/08/2019, 10:44 AM   [RouterExplorer] Mapped {/, GET} route +2ms
(node:19257) UnhandledPromiseRejectionWarning: Error: Generating schema error
    at Function.<anonymous> (/home/**index.js:287962:27)
    at Generator.next (<anonymous>)
    at fulfilled (/home/**/index.js:116122:62)
    at internalTickCallback (internal/process/next_tick.js:77:7)
    at process._tickCallback (internal/process/next_tick.js:47:5)
    at Function.Module.runMain (internal/modules/cjs/loader.js:778:11)
    at startup (internal/bootstrap/node.js:300:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:826:3)
(node:19257) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:19257) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

It seems there is still a dependency to the node_modules because it works with it...

I have some compilation issue too but I don't know if it related:

➜  start-on-time git:(cleanup-ci-configs) ✗ npx ncc build ./dist/apps/api/main.js -o ./dist/apps/api
ncc: Version 0.18.3
ncc: Compiling file index.js
ncc: Module directory "/home/**/node_modules/chokidar/lib" attempted to require "fsevents" but could not be resolved, assuming external.
ncc: Module directory "/home/**/node_modules/ws/lib" attempted to require "utf-8-validate" but could not be resolved, assuming external.
ncc: Module directory "/home/**/node_modules/ws/lib" attempted to require "bufferutil" but could not be resolved, assuming external.
ncc: Module directory "/home/**/node_modules/@nestjs/core" attempted to require "@nestjs/microservices" but could not be resolved, assuming external.
ncc: Module directory "/home/**/node_modules/@nestjs/core" attempted to require "@nestjs/microservices" but could not be resolved, assuming external.

Main libraries version:

  • nest : 6.1
  • ncc: 0.18.2
  • graphql: 14.2
  • graphql-type: 1.1
@darklam
Copy link

darklam commented May 17, 2019

I have the same issue. Did you manage to get it working somehow?

@jogelin
Copy link
Author

jogelin commented May 17, 2019

@darklam not for the moment, it is not so bad to have a docker image = 943mo :D

@cryptiklemur
Copy link

cryptiklemur commented May 17, 2019

Are you using type-graphql? If you are, @nestjs/graphql uses the optional package, which prevents static analysis of require calls, that causes some issues.

nestjs/nest#2213
https://github.com/zeit/now-builders/issues/508

Might be solved by: #383

@darklam
Copy link

darklam commented May 20, 2019

@aequasi I tried the latest beta and the build still throws the same error. Do you know of any workaround until it is fixed?

@cryptiklemur
Copy link

Unfortunately, I do not. I'm hoping that nestjs drops the usage of the optional package.... but that might be a pipe-dream.

@styfle
Copy link
Member

styfle commented May 21, 2019

The optional package is working properly in ncc 0.18.5

@jogelin Can you confirm this is fixed for you?

@jogelin
Copy link
Author

jogelin commented May 21, 2019

@styfle the optional package seems to work

I just meet another issue now but I don't know what it is, I'll have to investigate:

[Nest] 2152   - 05/21/2019, 3:13 PM   [InstanceLoader] PassportModule dependencies initialized +36ms
[Nest] 2152   - 05/21/2019, 3:13 PM   [InstanceLoader] JwtModule dependencies initialized +2ms
[Nest] 2152   - 05/21/2019, 3:13 PM   [InstanceLoader] HttpModule dependencies initialized +0ms
[Nest] 2152   - 05/21/2019, 3:13 PM   [InstanceLoader] ConfigModule dependencies initialized +1ms
[Nest] 2152   - 05/21/2019, 3:13 PM   [InstanceLoader] GraphQLModule dependencies initialized +1ms
[Nest] 2152   - 05/21/2019, 3:13 PM   [InstanceLoader] ApiModule dependencies initialized +0ms
[Nest] 2152   - 05/21/2019, 3:13 PM   [ExceptionHandler] Request failed with status code 400 +235ms
Error: Request failed with status code 400
    at createError (/home/***/index.js:278610:15)
    at settle (/home/***/index.js:123121:12)
    at IncomingMessage.handleStreamEnd (/home/***/index.js:76947:11)
    at IncomingMessage.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1098:12)
    at process.internalTickCallback (internal/process/next_tick.js:72:19)
 1: 0x944930 node::Abort() [node]
 2: 0x9a2b09  [node]
 3: 0xb9367a  [node]
 4: 0xb94229 v8::internal::Builtin_HandleApiCall(int, v8::internal::Object**, v8::internal::Isolate*) [node]
 5: 0x3cfcb394fb5d 
[1]    2152 abort (core dumped)  node ./dist/apps/api/index.js

Not related to ncc

@jogelin
Copy link
Author

jogelin commented May 21, 2019

@styfle forget my previous error, but I still have the issue :

[Nest] 30751   - 05/21/2019, 3:40 PM   [InstanceLoader] GraphQLModule dependencies initialized +1ms
[Nest] 30751   - 05/21/2019, 3:40 PM   [InstanceLoader] ApiModule dependencies initialized +0ms
[Nest] 30751   - 05/21/2019, 3:40 PM   [InstanceLoader] AuthModule dependencies initialized +171ms
[Nest] 30751   - 05/21/2019, 3:40 PM   [RoutesResolver] ConfigController {/api/config}: +6ms
[Nest] 30751   - 05/21/2019, 3:40 PM   [RouterExplorer] Mapped {/, GET} route +2ms
(node:30751) UnhandledPromiseRejectionWarning: Error: Generating schema error
    at Function.<anonymous> (/home/**/index.js:92139:27)
    at Generator.next (<anonymous>)
    at fulfilled (/home/**/index.js:102336:62)
    at process.internalTickCallback (internal/process/next_tick.js:77:7)
(node:30751) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:30751) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

@styfle
Copy link
Member

styfle commented May 22, 2019

@jogelin Can you enable source maps so the stack trace shows the correct line number?

It's not clear which package is actually failing since nestjs consists of 20+ packages.

ncc build index.ts --source-map

@jogelin
Copy link
Author

jogelin commented May 23, 2019

@styfle

below, the list of commands that I am doing:

First I use nestJS with Nx so I transpile my app to JS using (I could use directly ncc with a tsconfig but it doesn't change the result):

$ yarn build api
yarn run v1.16.0
$ ng build api
Starting type checking service...
Using 10 workers with 2048MB memory limit
Hash: 3c6e583323409af64ce8
Version: webpack 4.29.0
Time: 6269ms
Built at: 05/23/2019 7:36:43 AM
      Asset      Size  Chunks             Chunk Names
    main.js   133 KiB    main  [emitted]  main
main.js.map  69.1 KiB    main  [emitted]  main
Entrypoint main = main.js main.js.map
[0] multi ./apps/api/src/main.ts 28 bytes {main} [built]
[./apps/api/src/app/api.module.ts] 866 bytes {main} [built]
[./apps/api/src/app/auth/auth.module.ts] 952 bytes {main} [built]
[./apps/api/src/app/auth/gqlauth.guard.ts] 495 bytes {main} [built]
[./apps/api/src/app/auth/jwt.strategy.ts] 1.51 KiB {main} [built]
[./apps/api/src/app/config/config.controller.ts] 1.03 KiB {main} [built]
[./apps/api/src/app/config/config.module.ts] 433 bytes {main} [built]
[./apps/api/src/app/resolvers/plan.resolver.ts] 11.8 KiB {main} [built]
[./apps/api/src/app/resolvers/traveler.resolver.ts] 6.92 KiB {main} [built]
[./apps/api/src/app/thapaas/thapaas.module.ts] 2.01 KiB {main} [built]
[./apps/api/src/main.ts] 565 bytes {main} [built]
[@nestjs/common] external "@nestjs/common" 42 bytes {main} [built]
[@nestjs/core] external "@nestjs/core" 42 bytes {main} [built]
[@nestjs/graphql] external "@nestjs/graphql" 42 bytes {main} [built]
[tslib] external "tslib" 42 bytes {main} [built]
    + 46 hidden modules
Done in 7.97s.

Then I would use ncc to package the app (the output will be outside of the project to be sure no node_modules can be used):

$ npx ncc build ./dist/apps/api/main.js -o ../../ncc --source-map
ncc: Version 0.18.5
ncc: Compiling file index.js
ncc: Module directory "/home/**/node_modules/@nestjs/graphql/node_modules/chokidar/lib" attempted to require "fsevents" but could not be resolved, assuming external.
ncc: Module directory "/home/**/node_modules/@nestjs/core" attempted to require "@nestjs/microservices" but could not be resolved, assuming external.
ncc: Module directory "/home/**/node_modules/@nestjs/core" attempted to require "@nestjs/microservices" but could not be resolved, assuming external.
ncc: Module directory "/home/**/node_modules/ws/lib" attempted to require "utf-8-validate" but could not be resolved, assuming external.
ncc: Module directory "/home/**/node_modules/ws/lib" attempted to require "bufferutil" but could not be resolved, assuming external.
    0kB  ../../ncc/lib/README.md
    1kB  ../../ncc/lib/lib.webworker.importscripts.d.ts
    1kB  ../../ncc/lib/lib.esnext.symbol.d.ts
    1kB  ../../ncc/lib/lib.esnext.intl.d.ts
    1kB  ../../ncc/lib/lib.esnext.full.d.ts
    1kB  ../../ncc/lib/lib.esnext.d.ts
    1kB  ../../ncc/lib/lib.es2019.symbol.d.ts
    1kB  ../../ncc/lib/lib.es2019.string.d.ts
    1kB  ../../ncc/lib/lib.es6.d.ts
    1kB  ../../ncc/lib/lib.es2019.full.d.ts
    1kB  ../../ncc/lib/lib.es2019.d.ts
    1kB  ../../ncc/lib/lib.es2018.regexp.d.ts
    1kB  ../../ncc/lib/lib.es2018.promise.d.ts
    1kB  ../../ncc/lib/lib.es2018.full.d.ts
    1kB  ../../ncc/lib/lib.es2018.d.ts
    1kB  ../../ncc/lib/lib.es2017.typedarrays.d.ts
    1kB  ../../ncc/lib/lib.es2017.intl.d.ts
    1kB  ../../ncc/lib/lib.es2017.full.d.ts
    1kB  ../../ncc/lib/lib.es2017.d.ts
    1kB  ../../ncc/lib/lib.es2016.d.ts
    1kB  ../../ncc/lib/lib.es2016.full.d.ts
    1kB  ../../ncc/lib/lib.es2015.d.ts
    1kB  ../../ncc/lib/lib.d.ts
    2kB  ../../ncc/lib/lib.esnext.asynciterable.d.ts
    2kB  ../../ncc/lib/lib.es2018.intl.d.ts
    2kB  ../../ncc/lib/lib.es2017.string.d.ts
    2kB  ../../ncc/lib/lib.es2017.object.d.ts
    2kB  ../../ncc/lib/lib.es2018.asynciterable.d.ts
    2kB  ../../ncc/lib/lib.es2015.symbol.d.ts
    2kB  ../../ncc/lib/lib.es2015.proxy.d.ts
    2kB  ../../ncc/lib/lib.es2015.reflect.d.ts
    2kB  ../../ncc/lib/lib.es2015.generator.d.ts
    3kB  ../../ncc/lib/lib.es2015.collection.d.ts
    5kB  ../../ncc/lib/lib.es2016.array.include.d.ts
    6kB  ../../ncc/lib/lib.es2017.sharedmemory.d.ts
    6kB  ../../ncc/lib/lib.dom.iterable.d.ts
    8kB  ../../ncc/lib/lib.esnext.array.d.ts
    8kB  ../../ncc/lib/lib.es2019.array.d.ts
   10kB  ../../ncc/lib/lib.scripthost.d.ts
   10kB  ../../ncc/lib/lib.es2015.symbol.wellknown.d.ts
   11kB  ../../ncc/lib/lib.es2015.promise.d.ts
   15kB  ../../ncc/lib/lib.es2015.iterable.d.ts
   16kB  ../../ncc/lib/typesMap.json
   20kB  ../../ncc/lib/lib.es2015.core.d.ts
   31kB  ../../ncc/lib/lib.esnext.bigint.d.ts
   85kB  ../../ncc/lib/protocol.d.ts
  121kB  ../../ncc/sourcemap-register.js
  142kB  ../../ncc/lib/diagnosticMessages.generated.json
  142kB  ../../ncc/lib/zh-tw/diagnosticMessages.generated.json
  144kB  ../../ncc/lib/zh-cn/diagnosticMessages.generated.json
  153kB  ../../ncc/lib/cs/diagnosticMessages.generated.json
  160kB  ../../ncc/lib/tr/diagnosticMessages.generated.json
  161kB  ../../ncc/lib/pt-br/diagnosticMessages.generated.json
  164kB  ../../ncc/lib/es/diagnosticMessages.generated.json
  165kB  ../../ncc/lib/pl/diagnosticMessages.generated.json
  165kB  ../../ncc/lib/it/diagnosticMessages.generated.json
  165kB  ../../ncc/lib/fr/diagnosticMessages.generated.json
  167kB  ../../ncc/lib/de/diagnosticMessages.generated.json
  169kB  ../../ncc/lib/ko/diagnosticMessages.generated.json
  181kB  ../../ncc/lib/lib.webworker.d.ts
  187kB  ../../ncc/lib/ja/diagnosticMessages.generated.json
  202kB  ../../ncc/lib/lib.es5.d.ts
  215kB  ../../ncc/lib/ru/diagnosticMessages.generated.json
  284kB  ../../ncc/lib/typescriptServices.d.ts
  284kB  ../../ncc/lib/typescript.d.ts
  408kB  ../../ncc/lib/tsserverlibrary.d.ts
  761kB  ../../ncc/lib/lib.dom.d.ts
18929kB  ../../ncc/index.js
21391kB  ../../ncc/index.js.map
23743kB  [4969ms] - ncc 0.18.5

Then I run my app:

$ node index.js
[Nest] 9002   - 05/23/2019, 7:36 AM   [NestFactory] Starting Nest application...
(node:9002) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
[Nest] 9002   - 05/23/2019, 7:36 AM   [InstanceLoader] PassportModule dependencies initialized +39ms
[Nest] 9002   - 05/23/2019, 7:36 AM   [InstanceLoader] JwtModule dependencies initialized +1ms
[Nest] 9002   - 05/23/2019, 7:36 AM   [InstanceLoader] HttpModule dependencies initialized +1ms
[Nest] 9002   - 05/23/2019, 7:36 AM   [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 9002   - 05/23/2019, 7:36 AM   [InstanceLoader] ThapaasModule dependencies initialized +1ms
[Nest] 9002   - 05/23/2019, 7:36 AM   [InstanceLoader] GraphQLModule dependencies initialized +1ms
[Nest] 9002   - 05/23/2019, 7:36 AM   [InstanceLoader] ApiModule dependencies initialized +0ms
[Nest] 9002   - 05/23/2019, 7:36 AM   [InstanceLoader] AuthModule dependencies initialized +214ms
[Nest] 9002   - 05/23/2019, 7:36 AM   [RoutesResolver] ConfigController {/api/config}: +4ms
[Nest] 9002   - 05/23/2019, 7:36 AM   [RouterExplorer] Mapped {/, GET} route +2ms
(node:9002) UnhandledPromiseRejectionWarning: Error: Generating schema error
    at Function.<anonymous> (/home/**/node_modules/type-graphql/dist/schema/schema-generator.js:20:1)
    at Generator.next (<anonymous>)
    at fulfilled (/home/**/node_modules/tslib/tslib.js:104:46)
    at process.internalTickCallback (internal/process/next_tick.js:77:7)
(node:9002) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:9002) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

To summarize a bit my env:

  • @nestjs : 6.2.3
  • @zeit/ncc: 0.18.5
  • graphql: 14.3.0
  • @nrwl/*: 7.8.6
  • graphql-type: 1.1.0

I am using nest grapqhql code first to generate my graphql schema and I believe this is the issue...

If I have time, I will create a small github project to reproduce my issue, thx for your help

@darklam
Copy link

darklam commented May 24, 2019

If it helps, the solution I found is to initialize an empty npm package in the generated dist folder from ncc and install graphql and type-graphql. It seems like the compiled file still depends on these packages, so it runs without problems now.

@jogelin
Copy link
Author

jogelin commented Jun 20, 2019

@darklam indeed...this is working !
But not a real solution I believe ...

@1busby
Copy link

1busby commented Jun 24, 2019

Just a heads up I ran into the same error after trying to recreate the sample code in the NestJS documentation website. I fixed the error by importing the RecipesModule into the app module. It seems an error is thrown if the app doesn't actually even have any endpoints to convert to the schema. Hope this helps someone!

@jogelin
Copy link
Author

jogelin commented Jun 24, 2019

Just a heads up I ran into the same error after trying to recreate the sample code in the NestJS documentation website. I fixed the error by importing the RecipesModule into the app module. It seems an error is thrown if the app doesn't actually even have any endpoints to convert to the schema. Hope this helps someone!

What do you mean by imported ? The RecipesModule is already imported in the ApplicationModule in this sample ?!

I don't know if it is related but after some code investigation, I discovered in nestJS that the type-graphql library is called dynamically in:

https://github.com/nestjs/graphql/blob/master/lib/graphql-schema-builder.ts

    const { buildSchema } = loadPackage('type-graphql', 'SchemaBuilder', () =>
      require('type-graphql'),
    );

Does ncc supports dynamic loading ?

@khawarizmus
Copy link

khawarizmus commented Jun 28, 2019

@aequasi is there a way to fix this issue by overcoming this PR?

Also, @jogelin or @darklam can you please explain to me the solution that @darklam has come up with I don't really get it.

@darklam
Copy link

darklam commented Jun 29, 2019

@gimyboya From my understanding, the issue is that the resulting "compiled" index.js file still has some external dependencies to graphql and type-graphql packages. So the way I fixed it is by taking the output dist from ncc, initializing an empty npm package and installing the graphql and type-graphql packages, so the dependencies are resolved correctly and everything works. Mind you, this was done in docker, so I am not sure how it will work with other environments. Also, I had problems compiling my ts nestjs server with the newer ncc releases, so I am currently still using the 0.18.5 version. In short the commands would look like:

  • cd dist
  • yarn init -y
  • yarn add graphql type-graphql

Then, the index file inside dist should run correctly. Hope this helps you, if you have any more questions feel free to reply.

EDIT: I just saw I said git repo in my previous reply, it was just my mind playing games, I actually meant empty npm package. Sorry if it caused any confusion.

@jogelin
Copy link
Author

jogelin commented Jul 16, 2019

I don't know if it is related but after some code investigation, I discovered in nestJS that the type-graphql library is called dynamically in:

https://github.com/nestjs/graphql/blob/master/lib/graphql-schema-builder.ts

    const { buildSchema } = loadPackage('type-graphql', 'SchemaBuilder', () =>
      require('type-graphql'),
    );

Does ncc supports dynamic loading ?

I think I can confirm that the issue concerning the fact that ncc ignore type-graphql library is related to the custom loadPackage() function from NestJS

I updated @nestjs/graphql from version 6.2.5 to 6.4.0 and I meet the same kind of issue concerning apollo-server-express:

[Nest] 5540   - 07/16/2019, 9:22 AM   [PackageLoader] The "apollo-server-express" package is missing. Please, make sure to install this library ($ npm install apollo-server-express) to take advantage of GraphQLModule.

Mainly this PR makes the code more generic to allow using apollo-server-fastify instead of apollo-server-express in @nestjs/graphql (check file lib/graphql.module.ts in PR nestjs/graphql@2e199be).

As you can see in line 9, the direct import to apollo-server-express is deleted and the dependency was added to the loadPackage() function in line 153 :

      const { ApolloServer } = loadPackage(
        'apollo-server-express',
        '@nestjs/graphql',
      );

Maybe @kamilmysliwiec or @styfle you have an idea how to solve this ?

@kamilmysliwiec
Copy link

Just published 6.4.1 fix that will call require in loadPackage() @jogelin

@jogelin
Copy link
Author

jogelin commented Jul 16, 2019

@kamilmysliwiec It works ! Thx !!

Just a little remark, do you load the library twice then ?

    const { ApolloServer } = loadPackage(
      'apollo-server-express',
      'GraphQLModule',
      () => require('apollo-server-express'),
    );

@jogelin jogelin closed this as completed Jul 16, 2019
@iamgabrielsoft
Copy link

@1busby is right nestjs is trying to look for an endpoint to query to. i solve the solution with that little hint, though this issue is closed just for reference purpose guys.

Your resolvers should provide endpoint for nest/grqphql should know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants