graphql-nexus / nexus-prisma

Prisma plugin for Nexus
https://graphql-nexus.github.io/nexus-prisma
MIT License
564 stars 45 forks source link

Error: NEXUS__UNKNOWN__TYPE was already defined and imported as a type, check the docs for extending types #216

Closed T-D3V closed 2 years ago

T-D3V commented 2 years ago

I have a nextjs project which uses graphql as a middleware api, following error occurs:

Error

Server Error
Error: NEXUS__UNKNOWN__TYPE was already defined and imported as a type, check the docs for extending types

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
extendError
file:///workspace/node_modules/nexus/dist/builder.js (1123:12)
SchemaBuilder.addType
file:///workspace/node_modules/nexus/dist/builder.js (156:27)
SchemaBuilder.missingType
file:///workspace/node_modules/nexus/dist/builder.js (704:14)
SchemaBuilder.getOrBuildType
file:///workspace/node_modules/nexus/dist/builder.js (938:21)
SchemaBuilder.getOutputType
file:///workspace/node_modules/nexus/dist/builder.js (876:34)
SchemaBuilder.buildOutputField
file:///workspace/node_modules/nexus/dist/builder.js (798:60)
<unknown>
file:///workspace/node_modules/nexus/dist/builder.js (770:43)
Array.forEach
<anonymous>
SchemaBuilder.buildOutputFields
file:///workspace/node_modules/nexus/dist/builder.js (769:16)
fields
file:///workspace/node_modules/nexus/dist/builder.js (544:32)

package.json

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@prisma/client": "^3.14.0",
    "apollo-server-micro": "^3.7.0",
    "graphql": "^16.5.0",
    "micro": "^9.3.4",
    "micro-cors": "^0.1.1",
    "next": "12.1.6",
    "nexus": "^1.3.0",
    "nexus-prisma": "^0.35.0",
    "react": "18.1.0",
    "react-dom": "18.1.0"
  },
  "devDependencies": {
    "@types/micro-cors": "^0.1.2",
    "@types/node": "17.0.33",
    "@types/react": "18.0.9",
    "@types/react-dom": "18.0.4",
    "autoprefixer": "^10.4.7",
    "eslint": "8.15.0",
    "eslint-config-next": "12.1.6",
    "postcss": "^8.4.13",
    "prisma": "^3.14.0",
    "tailwindcss": "^3.0.24",
    "typescript": "4.6.4"
  }
}

Schema.prisma

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

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

generator nexusPrisma {
  provider = "nexus-prisma"
}

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

///User model for application Users
model Users {
  ///UUID for every User String
  id          String      @id @default(uuid())
  ///createdAt for creation date DateTime
  createdAt   DateTime    @default(now())
  ///updatedAt for update date DateTime
  updatedAt   DateTime    @updatedAt
  ///email for Users is unique
  email       String      @unique
  ///password hash for Users
  password    String
  ///Roles for application from Enum Roles
  role        Roles       @default(USER)
  ///Relation with contact n to n
  contact     Contacts?   @relation(fields: [contactId], references: [id])
  ///Relation contactId for Contact relation n to n
  contactId   String?
}

enum Roles {
  USER
  ADMIN
}

model Contacts {
  id          String    @id @default(uuid())
  createdAt   DateTime  @default(now())
  updatedAt   DateTime  @updatedAt
  firstName   String
  lastName    String
  email       String    @unique
  phone       String
  addressLine String
  city        String
  zipCode     Int
  note        String
  user       Users?
  events      ContactsOnEvents[]  @relation(name: "guest")
  guests      ContactsOnEvents[]  @relation(name: "guestfrom")
}

model Events {
  id          String      @id @default(uuid())
  createdAt   DateTime    @default(now())
  updatedAt   DateTime    @updatedAt
  name        String
  startDate   DateTime
  EndDate     DateTime
  type        EventTypes  @default(HUNT)
  contacts    ContactsOnEvents[]
}

enum EventTypes {
  HUNT
  EVENT
}

model ContactsOnEvents {
  guest       Contacts  @relation(fields: [guestId], references: [id], name: "guest")
  guestId     String
  guestFrom   Contacts  @relation(fields: [guestFromId], references: [id], name:"guestfrom")
  guestFromId String
  event       Events    @relation(fields: [eventId], references: [id])
  eventId     String
  assignedAt  DateTime  @default(now())
  type        Types[]   

  @@id([guestId, eventId])
}

enum Types {
  HUNTER
  DRIVER
  SEEKER
}

image

image

Here's my file structure so you know what file to ask for if you need additional code.

So if you need aditional code feel free to ask me, but since I don't get why this error is happening. I don't even know what code to provide.

Thamks for the help.

willpots commented 2 years ago

Do you have your nexus object definitions handy? I'm seeing this issue too when I try and create a field on the nexus schema mapping to another collection.

model User {
  id     String   @id @default(auto()) @map("_id") @db.ObjectId
  homes  Home[]
}

model Home {
  id       String   @id @default(auto()) @map("_id") @db.ObjectId
  userId   String   @db.ObjectId
  user     User     @relation(fields: [userId], references: [id])
}
import {User as PrismaUser} from 'nexus-prisma';

const User = objectType({
  name: 'User', 
  definition: (t) {
    t.field(PrismaUser.id);
    t.field(PrismaUser.homes);
  }
}
/Users/potter/workspace/taxes/server/node_modules/nexus/src/builder.ts:1743
  return new Error(`${name} was already defined and imported as a type, check the docs for extending types`)
         ^
Error: NEXUS__UNKNOWN__TYPE was already defined and imported as a type, check the docs for extending types
simonjoom commented 2 years ago

I found this issue when upgrading prisma you need to look for type ${yourmodel}OrderByInput in your graphql code generated

and change it to ${yourmodel}OrderByWithRelationInput

because OrderByInput not exist for the last prisma then you can restart your app and have a try

baconcheese113 commented 2 years ago

Getting same thing after attempting to migrate to nexus-prisma from nexus-prisma-plugin, not sure where to look

AhmedElywa commented 2 years ago

This bug means you are using a type that does not exist.

Frosty21 commented 2 years ago

Can we close this issue @T-D3V or any update on this? What do you define in your object types? @AhmedElywa is right this shouldn't be a bug its most likely you've define that object type more than once. comb through your code for a copy of name: '{model}' to find the one that is a duplicate.

T-D3V commented 2 years ago

Yep I'll close it I mean this was from a few months ago and a lot has changed so no need to keep this issue open.