MichalLytek / typegraphql-prisma

Prisma generator to emit TypeGraphQL types and CRUD resolvers from your Prisma schema
https://prisma.typegraphql.com
MIT License
887 stars 114 forks source link

EMFILE: too many open files #202

Open cre8tiv opened 2 years ago

cre8tiv commented 2 years ago

Describe the Bug I have a prisma.schema with 202 tables, when running npx prisma generate I get the message: Error: EMFILE: too many open files. I then modified the schema to have 153 tables to test and ran the command again and it failed. I then tried with 100 tables and it failed. I tried it with 57 tables, and it failed. My schema is generated from a prisma db pull to an existing MS SQL server database.

sagarsneh commented 2 years ago

I am also facing the same issue after adding a few models to the schema.

MichalLytek commented 2 years ago

@cre8tiv @sagarsneh Please try the "source code" generation mode:

generator typegraphql {
  provider           = "typegraphql-prisma"
  output             = "../prisma/generated/typegraphql-prisma"
  emitTranspiledCode = false
}

ts-morph is not good at compiling the generated TS files into *.js and *.d.ts files.

cre8tiv commented 2 years ago

I added this to my schema.prisma file and then attempted to run the generate command with 153 models. I still got the same error message. When I checked the output, I had the expected number of files in the models folder, but the error message indicated that it seemed to fail in files for \resolvers\outputs. I commented out many of the models in my schema and was able to get it to run successfully with 57 models.

jlavric commented 2 years ago

Hi I am having the same issue. My schema is not that big. schema.zip Without emitTranspiledCode = false flag I am getting 'too many open files error', but when I add emitTranspiledCode = false generation succedes. When server is started I got this error:

import * as crudResolversImport from "./resolvers/crud/resolvers-crud.index";
^^^^^^
SyntaxError: Cannot use import statement outside a module

I guess there must be some circular references or similar. Any any help will do, library is just perfect!!

MichalLytek commented 2 years ago

@jlavric

Cannot use import statement outside a module

You need to emit those source files into your src directory, not node_modules. They have to be compiled by TypeScript.

jlavric commented 2 years ago

@MichalLytek thank you for quick response. Yes of course I forgot that, my mistake. With generated files in my src directory and properly imported I was getting this error: RangeError: Maximum call stack size exceeded. at ... \src\generated\type-graphql\resolvers\inputs\ComponentCreateNestedOneWithoutUcsInput.ts:1:1) That is why I am thinking about circular references.

jlavric commented 2 years ago

I have done a lot of testing and research and finally it works for me. I just have to change some steps. I can confirm that with emitTranspiledCode = false switch in schema typegraphql-prisma generation works as it should. I can't run my project now with ts-node because of RangeError: Maximum call stack size exceeded. So I had to compile wiht tsc and add generated code. With that I got graphql server with generated resolvers and it works on debian, rhel (CentOS) and windows servers.

Part of my package.json "dependencies": { "@prisma/client": "^3.4.2", "apollo-server-express": "^3.5.0", "class-validator": "^0.13.1", "colors": "^1.4.0", "cors": "^2.8.5", "crypto-js": "^4.1.1", "formidable": "^2.0.1", "fs-extra": "^10.0.0", "graphql": "^15.7.2", "graphql-fields": "^2.0.3", "graphql-scalars": "1.10", "jsonwebtoken": "^8.5.1", "moment": "^2.29.1", "pdfmake": "^0.2.4", "reflect-metadata": "^0.1.13", "ts-node": "^10.4.0", "type-graphql": "^1.1.1" }

GreenOn commented 1 year ago

Hi, our model has nearly 120 tables and we just got this same error. Any update on this issue?

GreenOn commented 1 year ago

Splitting the schema is hard since there are so many relations and especially prisma asks us to tie all these tables with FKs.

GreenOn commented 1 year ago

Update: I switched to a high performance linux machine and I was able to run it to completion.

mmmeff commented 1 year ago

Could this be fixed by switching usage of fs to graceful-fs?

I was able to work around this by monkeypatching typegraphql-prisma globally: https://github.com/isaacs/node-graceful-fs#global-patching

MichalLytek commented 1 year ago

@mmmeff Apart from tests, I use fs only to remove and create the output dir. Please ask on ts-morph repo about this issue, I don't know if underlying tsc is not using fs directly.

caseyDev422 commented 1 year ago

Has there been any type of resolution or workaround for this? Running into this now and my team is trying to solve this issue

UPDATE: We fixed the issue by setting emitTranspilerCode = false and compiling with tsc (npx tsc) with a tsconfig.json file.

IvanYu327 commented 2 months ago

@caseyDev422 Can you elaborate more on how you fixed this issue, what you described it not working for me.