Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplication between prisma schema and models #427

Closed
Skona27 opened this issue Nov 2, 2020 · 2 comments
Closed

Duplication between prisma schema and models #427

Skona27 opened this issue Nov 2, 2020 · 2 comments

Comments

@Skona27
Copy link

Skona27 commented Nov 2, 2020

Hi!
First of all thanks for this starter. It is really awesome 馃槂

I've noticed that some of the code is duplicated between prisma schema definitions and nest js models.
In prisma.schema we have a User definition, which looks like below:

model User {
  id        String   @id @default(cuid())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  email     String   @unique
  password  String
  firstname String?
  lastname  String?
  posts     Post[]
  role      Role
}

And also in user.model we define the same 'shape' but with typescript:

@ObjectType()
export class User extends BaseModel {
  email: string;
  firstname?: string;
  lastname?: string;
  role: Role;
  @HideField()
  password: string;
}

Why is that necessary? Can't we somehow generate schema from models, or other way around?

@marcjulian
Copy link
Member

marcjulian commented Nov 4, 2020

@Skona27 Hi, thanks 馃憤 I hope I can answer your question.

Yes currently the code requires duplicates between the prisma schema and the graphql schema.

  1. Database schema: schema.prisma describes the model and thus the table created for this model.
  2. GraphQL schema: user.model.ts and any other models describes the response value of your user type for your GraphQL queries

How to resolve the duplicates for now?
It is currently necessary to have duplicate code once in schema.prisma and once for your GraphQL schema.

One idea would be to implement User from prisma client for your graphql model to always keep those prisma and graphql models in sync.

import { User } from '@prisma/client'; 
import { PostModel } from '../post/post.model.ts';

@ObjectType('User')
export class UserModel extends BaseModel implements User {
  email: string;
  @Field({ nullable: true })
  firstname: string;
  @Field({ nullable: true })
  lastname: string;
  role: Role;
  @HideField()
  password: string;
  posts: PostModel[];
}

Another option might be in the feature typegraphql-prisma

And in the near feature - when Prisma SDK will be ready - the typegraphql-prisma integration will also allow to use a code-first approach to build a schema.prisma and GraphQL schema at once, using classes with decorators as a single source of truth. Stay tuned! 馃挭

There is also an typegraphql integration for nestjs typegraphql-nestjs with some caveats.

@Skona27
Copy link
Author

Skona27 commented Nov 4, 2020

@marcjulian
Thanks for that answer! Everything is clear now

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants