typegraphql-prisma: Errors about accessing inputs before initialization

_Originally posted by @j718 in https://github.com/MichalLytek/typegraphql-prisma/issues/1#issuecomment-697454730_

"type-graphql": "^1.0.0",
typegraphql-prisma": "^0.6.1",

I keep getting errors about accessing inputs before initialization and wanted to get your input. The error is as follows:

ReferenceError: Cannot access 'BankAccountWhereInput' before initialization
    at Module.BankAccountWhereInput (C:\Users\jacob\OneDrive\Documents\GitHub\tictactoe\.next\server\pages\api\graphql.js:11490:113)
    at Module../src/prisma/generated/type-graphql/resolvers/inputs/BankAccountListRelationFilter.ts (C:\Users\jacob\OneDrive\Documents\GitHub\tictactoe\.next\server\pages\api\graphql.js:10345:110)
    at __webpack_require__ (C:\Users\jacob\OneDrive\Documents\GitHub\tictactoe\.next\server\pages\api\graphql.js:23:31)     
    at Module../src/prisma/generated/type-graphql/resolvers/inputs/BankConnectionWhereInput.ts (C:\Users\jacob\OneDrive\Documents\GitHub\tictactoe\.next\server\pages\api\graphql.js:13159:95)
    at __webpack_require__ (C:\Users\jacob\OneDrive\Documents\GitHub\tictactoe\.next\server\pages\api\graphql.js:23:31)     
    at Module../src/prisma/generated/type-graphql/resolvers/inputs/BankAccountWhereInput.ts (C:\Users\jacob\OneDrive\Documents\GitHub\tictactoe\.next\server\pages\api\graphql.js:11499:90)
    at __webpack_require__ (C:\Users\jacob\OneDrive\Documents\GitHub\tictactoe\.next\server\pages\api\graphql.js:23:31)     
    at Module../src/prisma/generated/type-graphql/resolvers/crud/BankAccount/args/AggregateBankAccountArgs.ts (C:\Users\jacob\OneDrive\Documents\GitHub\tictactoe\.next\server\pages\api\graphql.js:3695:87)
    at __webpack_require__ (C:\Users\jacob\OneDrive\Documents\GitHub\tictactoe\.next\server\pages\api\graphql.js:23:31)     
    at Module../src/prisma/generated/type-graphql/resolvers/crud/BankAccount/BankAccountCrudResolver.ts (C:\Users\jacob\OneDrive\Documents\GitHub\tictactoe\.next\server\pages\api\graphql.js:3208:88)

I have the following schema.prisma file.

generator client {
  provider = "prisma-client-js"
}

generator typegraphql {
  provider = "node ../node_modules/typegraphql-prisma/generator.js"
  output   = "../prisma/generated/type-graphql"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Account {
  id                 Int       @default(autoincrement()) @id
  compoundId         String    @unique @map(name: "compound_id")
  userId             Int       @map(name: "user_id")
  providerType       String    @map(name: "provider_type")
  providerId         String    @map(name: "provider_id")
  providerAccountId  String    @map(name: "provider_account_id")
  refreshToken       String?   @map(name: "refresh_token")
  accessToken        String?   @map(name: "access_token")
  accessTokenExpires DateTime? @map(name: "access_token_expires")
  createdAt          DateTime  @default(now()) @map(name: "created_at")
  updatedAt          DateTime  @default(now()) @map(name: "updated_at")

  @@index([providerAccountId], name: "providerAccountId")
  @@index([providerId], name: "providerId")
  @@index([userId], name: "userId")

  @@map(name: "accounts")
}



model BankAccount {
  account_id        String         @id
  bankConnectionId  Int            @map(name: "bank_connection_id")
  balancesAvailable Float            @map(name: "balances_available")
  balancesCurrent   Float            @map(name: "balances_current")
  isoCurrencyCode   String         @map(name: "iso_currency_code")
  name              String
  officialName      String         @map(name: "official_name")
  subtype           String
  type              String
  bankConnection    BankConnection @relation(fields: [bankConnectionId], references: [id])

  @@map(name: "bank_account")

}

model BankConnection {
  id          Int           @default(autoincrement()) @id
  user        User          @relation(fields: [userId], references: [id])
  userId      Int           @map(name: "user_id")
  itemId      String        @map(name: "item_id")
  accessToken String        @map(name: "access_token")
  bankAccounts BankAccount[]


  @@map(name: "bank_connection")

}
model Session {
  id           Int      @default(autoincrement()) @id
  userId       Int      @map(name: "user_id")
  expires      DateTime
  sessionToken String   @unique @map(name: "session_token")
  accessToken  String   @unique @map(name: "access_token")
  createdAt    DateTime @default(now()) @map(name: "created_at")
  updatedAt    DateTime @default(now()) @map(name: "updated_at")
  user         User     @relation(fields: [userId], references: [id])


  @@map(name: "sessions")
}

model User {
  id              Int              @default(autoincrement()) @id
  name            String?
  email           String?          @unique
  emailVerified   DateTime?        @map(name: "email_verified")
  image           String?
  createdAt       DateTime         @default(now()) @map(name: "created_at")
  updatedAt       DateTime         @default(now()) @map(name: "updated_at")
  bankConnections BankConnection[]
  sessions        Session[]


  @@map(name: "users")
}

model VerificationRequest {
  id         Int      @default(autoincrement()) @id
  identifier String
  token      String   @unique
  expires    DateTime
  createdAt  DateTime @default(now()) @map(name: "created_at")
  updatedAt  DateTime @default(now()) @map(name: "updated_at")

  @@map(name: "verification_requests")
}

Here is my build schema command

    const schema = await buildSchema({
      // resolvers: [CreateLinkTokenResolver, UserCrudResolver, GameCrudResolver, UserRelationsResolver, GameRelationsResolver],
      resolvers: [LinkTokenResolver, UserRelationsResolver ],
      authChecker: customAuthChecker,
      validate: false,
    })

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 1
  • Comments: 25 (4 by maintainers)

Most upvoted comments

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

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

@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 😃

@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!

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