Open MichalLytek opened 3 years ago
Are you using CommonJS as your module type in your tsconfig?
Originally posted by @wSedlacek in https://github.com/MichalLytek/typegraphql-prisma/issues/1#issuecomment-697496611
Yes, I am. Here is my tsconfig
{
"compilerOptions": {
"target": "es2018",
"lib": [
"dom",
"dom.iterable",
"es2018",
"esnext.asynciterable"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"baseUrl": "./",
"paths": {
"~types/*": [
"./@types/*"
],
"~graphql/*": [
"./pages/api/graphql/*"
],
"~views/*": [
"./src/views/*"
],
"~viewsUi/*": [
"./src/views/ui/*"
],
"~viewsComp/*": [
"./src/views/components/*"
],
"~viewsLay/*": [
"./src/views/layouts/*"
],
"~styles/*": [
"./src/styles/*"
],
"~server/*": [
"./server/*"
],
"~lib/*": [
"./src/lib/*"
],
"~assets/*": [
"./assets/*"
],
"~static/*": [
"./public/static/*"
],
"~generated/*": [
"./src/prisma/generated/*"
],
"~context/*": [
"./src/context/*"
],
"~services/*": [
"./src/services/*"
]
},
},
"exclude": [
"node_modules"
],
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
]
}
Here is my babel config
{
"presets": ["next/babel", "@babel/preset-typescript"],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
"@babel/plugin-transform-runtime",
"babel-plugin-transform-typescript-metadata",
["@babel/plugin-proposal-class-properties", { "loose": true }],
[
"styled-components",
{
"ssr": true,
"displayName": true,
"preprocess": false
}
]
]
}
Originally posted by @j718 in https://github.com/MichalLytek/typegraphql-prisma/issues/1#issuecomment-697562870
I confirm this bug in a simple Book (author) -> User
relation inside a Next.js (v10.0.1) boilerplate integration test.
tsconfig.json
{
"compilerOptions": {
"target": "es2018",
"lib": ["dom", "dom.iterable", "es2018", "esnext.asynciterable"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"baseUrl": "."
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
generator typegraphql {
provider = "typegraphql-prisma"
output = "../src/generated/typegraphql-prisma"
}
enum Role {
USER
PLAYER
SHOP
ADMIN
}
model User {
id Int @id @default(autoincrement())
email String @unique
firstName String
lastName String
password String
role Role @default(USER)
count Int @default(0)
books Book[]
profile Profile
}
model Profile {
id Int @id @default(autoincrement())
bio String
user User @relation(fields: [userId], references: [id])
userId Int
}
model Book {
id Int @id @default(autoincrement())
title String
author User @relation(fields: [userId], references: [id])
userId Int
categories Category[]
}
model Category {
id Int @id @default(autoincrement())
books Book[]
}
schema builder
import { resolvers } from "src/generated/typegraphql-prisma";
import { buildSchema } from "type-graphql";
import path from "path";
export const doBuildSchema = async () => {
return await buildSchema({
resolvers,
emitSchemaFile: path.join(process.cwd(), "../generated/schema.gql"),
validate: false,
});
};
ReferenceError: Cannot access 'BookWhereInput' before initialization
at Module.eval [as BookWhereInput] (webpack-internal:///./src/generated/typegraphql-prisma/resolvers/inputs/BookWhereInput.ts:2:106)
at eval (webpack-internal:///./src/generated/typegraphql-prisma/resolvers/inputs/BookListRelationFilter.ts:24:103)
at Module../src/generated/typegraphql-prisma/resolvers/inputs/BookListRelationFilter.ts (/Users/fabio/Documents/LAVORI/PROGETTI/next10-api-graphql-prisma/.next/server/pages/api/graphql.js:1448:1)
at __webpack_require__ (/Users/fabio/Documents/LAVORI/PROGETTI/next10-api-graphql-prisma/.next/server/pages/api/graphql.js:23:31)
at eval (webpack-internal:///./src/generated/typegraphql-prisma/resolvers/inputs/CategoryWhereInput.ts:5:88)
at Module../src/generated/typegraphql-prisma/resolvers/inputs/CategoryWhereInput.ts (/Users/fabio/Documents/LAVORI/PROGETTI/next10-api-graphql-prisma/.next/server/pages/api/graphql.js:1808:1)
at __webpack_require__ (/Users/fabio/Documents/LAVORI/PROGETTI/next10-api-graphql-prisma/.next/server/pages/api/graphql.js:23:31)
at eval (webpack-internal:///./src/generated/typegraphql-prisma/resolvers/inputs/CategoryListRelationFilter.ts:5:84)
at Module../src/generated/typegraphql-prisma/resolvers/inputs/CategoryListRelationFilter.ts (/Users/fabio/Documents/LAVORI/PROGETTI/next10-api-graphql-prisma/.next/server/pages/api/graphql.js:1688:1)
at __webpack_require__ (/Users/fabio/Documents/LAVORI/PROGETTI/next10-api-graphql-prisma/.next/server/pages/api/graphql.js:23:31)
@MichalLytek Did you find a solution for this?
@KamauIan I don't think there's a solution for that.
NodeJS doesn't like single output file when you have circular references as it then cannot access before initialization. CommonJS modules can handle the references correctly.
The issue is with TypeScript reflection which is directly referencing the classes in the emited code, not in a () =>
function like TypeGraphQL.
So I decided to transpile the es6 to es5 with babel after compiling typescript. Not sure if this is a good practice but seems to work now.
I've been a full day trying to solve this problem and finally, I had to use "babel-plugin-transform-es2015-modules-commonjs" babel plugin.
I've been a full day trying to solve this problem and finally, I had to use "babel-plugin-transform-es2015-modules-commonjs" babel plugin.
When I try adding this to my project, I then get a 500 error for my graphql endpoint instead of the original error this thread is about. Any ideas how to then fix the 500 error for /api/graphql? GH repo here
I´ve encountered the same issue while trying to use apollo-server-micro on my nextjs project, typegraphql-prisma and prisma2 and still have not found a solution. If anyone knows a possible solution, please let me know. Thanks
@faustinozanetto I got this to work by installing the @babel/plugin-transform-modules-commonjs
Babel plugin.
I plan on uploading a minimal viable GitHub repo which includes everything (NextJS, TypeGraphQL, Prisma) over the next few days once I fully finish migrating my project over. So if you (or others) are still having problems, stay tuned to this space and I will hopefully have something soon :)
@faustinozanetto I got this to work by installing the
@babel/plugin-transform-modules-commonjs
Babel plugin.I plan on uploading a minimal viable GitHub repo which includes everything (NextJS, TypeGraphQL, Prisma) over the next few days once I fully finish migrating my project over. So if you (or others) are still having problems, stay tuned to this space and I will hopefully have something soon :)
But @babel/plugin-transform-modules-commonjs
will break the refresh behavior of next js. After apply this babel plugin, the whole page will be reloaded when I even update some simple plain text elements.
@jctaoo Hmm I hadn't noticed that before, but now I can see that problem happening. That's strange, not sure what could be causing that.
@jctaoo
Here's my minimally viable repo: https://github.com/SheaBelsky/nextjs-prisma-typegraphql
So far I'm not running into the issue where changing one component causes Next to completely reload everything. I'm not sure what triggers that, but would love to explore what's causing that!
@SheaBelsky
Next refresh behavior base on analyzing static import/export statements. But @babel/plugin-transform-modules-commonjs
transform these statements to something like:
// original
export default 42;
// transformed
Object.defineProperty(exports, "__esModule", {
value: true,
});
If we can use @babel/plugin-transform-modules-commonjs
on only files generated by typegraphql-prisma
, it will be good.
@babel/plugin-transform-modules-commonjs
prevents using ECMAScript-only features like top-level awaits... Is there any potential solution that does not require transforming to CommonJS?
Does async import help for this?
const { resolvers } = await import('../generated/type-graphql')
@MichalLytek Any updates on this issue, because I am facing the same problem, even after using commonjs as the module system for typescript compilation in a next.js project.
Any news?
Still currently having this issue on nextjs and prisma
can you share the BankAccountWhereInput.ts file I've run into this issue before. but i'm using only typeGraphql, not prisma in my project I had to change the tsconfig module from commonJs to esnext, and problems began to come!!. anyway, you need to double check that you don't have a circular dependency.
I solved it by using (import type) syntax you can check the import type here https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html
here is an example:
import type { Users as UsersType } from "../../../common/Entitites/Users.js";//use this for attributes/variable types
import { Users } from "../../../common/Entitites/Users";//use this as value
export class Passenger extends Audit {
@OneToOne(() => Users//<---used as value , (user) => user.passenger) psgrId: number; @Field(() => Int) user: UsersType ;//<---type
}
or you can skip import type and use typeof like below:
import { Users } from "../../../common/Entitites/Users"; export class Passenger extends Audit { @OneToOne(() => Users//<---used as value , (user) => user.passenger) psgrId: number;
@Field(() => Int) user: typeof Users ;//<---type
}
I have to omit a field when the problem occurs
model Product {
....
/// @TypeGraphQL.omit(output: true, input: true)
cartItems CartItem[]
}
Has anyone found a useable workaround for this?
Same boat. Same problem
@vanics The best solution I have so far is selectively ignoring the problem fields in the prisma schema and creating custom resolvers for the specific queries/mutations I need.
@MichalLytek
@KamauIan I don't think there's a solution for that. NodeJS doesn't like single output file when you have circular references as it then cannot access before initialization. CommonJS modules can handle the references correctly. The issue is with TypeScript reflection which is directly referencing the classes in the emited code, not in a
() =>
function like TypeGraphQL.
@MichalLytek Is there something that can be done at the library level, or do you have any ideas to address the limitations with TypeScript reflection? While selectively ignoring cyclic fields has been a useful workaround for compatibility with Next.js, this approach sometimes requires ignoring important fields, which can limit the functionality of GraphQL. Any insights or solutions to improve this would be greatly appreciated!
I keep getting errors about accessing inputs before initialization and wanted to get your input. The error is as follows:
I have the following schema.prisma file.
Here is my build schema command