MichalLytek / MichalLytek/typegraphql-prisma
Problem with type conflicts when using doc lines to change field names
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 918
- Forks
- 130
- PR merge metrics
- No merged PRs in 30d
Description
I'm running into an issue with type conflicts when using doc lines in schema.prisma to change field names, where it looks like the type that prisma returns from create/find/etc doesn't match up with the type that is generated by typegraphql-prisma.
I am using typegraphql-prisma 0.8.0, prisma-cli 2.8.1 and prisma client 2.8.1
Here is my schema.prisma (I removed the unrelated models for brevity)
```
generator client {
provider = "prisma-client-js"
}
generator typegraphql {
provider = "typegraphql-prisma"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Course {
id Int @id @default(autoincrement())
title String
subject String
headline String
intro String
/// @TypeGraphQL.field(name: "creatorId")
creator_id Int
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
creator User @relation(fields: [creator_id], references: [id])
enrolledUsers CoursesUsers[]
lessons Lesson[]
@@map("course")
}
model CoursesUsers {
/// @TypeGraphQL.field(name: "courseId")
course_id Int
/// @TypeGraphQL.field(name: "userId")
user_id Int
course Course @relation(fields: [course_id], references: [id])
user User @relation(fields: [user_id], references: [id])
@@id([course_id, user_id])
@@map("courses_users")
}
model User {
id Int @id @default(autoincrement())
username String @unique
email String @unique
/// @TypeGraphQL.omit(output: true)
password String?
googleId String? @map("google_id")
facebookId String? @map("facebook_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
createdCourses Course[]
enrolledCourses CoursesUsers[]
lessons Lesson[]
notes Note[]
@@map("user")
}
```
And here is where I am trying to use the generated type but am running into a conflict (the service is just a function called by a controller which is the actual resolver)
```
import { User, Course } from "@generated/type-graphql";
import { PrismaClient } from "@prisma/client";
export const createCourseService = async (
prisma: PrismaClient,
createCourseInput: CreateCourseInput,
userId: number
): Promise => {
let err, newCourse: Course | undefined, foundUser;
const { course, user } = prisma;
[err, foundUser] = await to(user.findOne({ where: { id: userId } }));
if (err) throw new Error(err.message);
if (!foundUser)
throw new AuthenticationError("Could not find user to create course");
[err, newCourse] = await to(
course.create({
data: {
...createCourseInput,
creator: {
connect: { id: userId },
},
},
})
);
if (err) throw new Error(err.message);
if (!newCourse) {
throw new ApolloError("Internal server error. Could not create newCourse");
}
return newCourse;
};
```
So my error message says: Type 'import("c:/Users/Joel/Documents/webprojects/serious/Smarterish/code/server/node_modules/.prisma/client/index").Course | undefined' is not assignable to type 'import("c:/Users/Joel/Documents/webprojects/serious/Smarterish/code/server/node_modules/@generated/type-graphql/models/Course").Course | undefined'.
Property 'creatorId' is missing in type 'import("c:/Users/Joel/Documents/webprojects/serious/Smarterish/code/server/node_modules/.prisma/client/index").Course' but required in type 'import("c:/Users/Joel/Documents/webprojects/serious/Smarterish/code/server/node_modules/@generated/type-graphql/models/Course").Course'.
I'm not sure if it's an actual issue or if I should mark the return type as Prisma.Course rather than Course from typegraphql or what.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with schema.prisma and compare the generated @generated/type-graphql/models/Course type with the Prisma client Course returned by course.create. Then inspect the generated Course model and the service function shown in the issue; done means the documented field-name mapping produces compatible types or the expected return-type boundary is clearly established.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- postgresql, typescript
- Domain
- backend-api-design, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100