{"id":15457720,"url":"https://github.com/yassineldeeb/prisma-tgql-types-gen","last_synced_at":"2025-04-14T03:34:01.330Z","repository":{"id":37789164,"uuid":"428056962","full_name":"YassinEldeeb/prisma-tgql-types-gen","owner":"YassinEldeeb","description":"◭ Prisma generator for generating TypeGraphQL class types and enums with allowing to edit the generated output without being overwritten 💪","archived":false,"fork":false,"pushed_at":"2023-03-06T22:09:45.000Z","size":1675,"stargazers_count":53,"open_issues_count":16,"forks_count":6,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-12T12:42:58.455Z","etag":null,"topics":["codegen","generator","graphql","prisma","type-graphql","typegraphql","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/prisma-typegraphql-types-generator","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/YassinEldeeb.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-11-14T22:32:56.000Z","updated_at":"2024-04-08T21:40:55.000Z","dependencies_parsed_at":"2024-06-20T23:35:25.919Z","dependency_job_id":null,"html_url":"https://github.com/YassinEldeeb/prisma-tgql-types-gen","commit_stats":{"total_commits":208,"total_committers":4,"mean_commits":52.0,"dds":"0.14423076923076927","last_synced_commit":"7899b48ae21350455b515db72184e216db7c300c"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YassinEldeeb%2Fprisma-tgql-types-gen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YassinEldeeb%2Fprisma-tgql-types-gen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YassinEldeeb%2Fprisma-tgql-types-gen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YassinEldeeb%2Fprisma-tgql-types-gen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/YassinEldeeb","download_url":"https://codeload.github.com/YassinEldeeb/prisma-tgql-types-gen/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248816282,"owners_count":21166053,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codegen","generator","graphql","prisma","type-graphql","typegraphql","typescript"],"created_at":"2024-10-01T22:50:21.822Z","updated_at":"2025-04-14T03:34:01.293Z","avatar_url":"https://github.com/YassinEldeeb.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Prisma TypeGraphQL Types Generator\n\n![Banner](https://github.com/YassinEldeeb/Prisma-TypeGraphQL-Types-Generator/blob/main/images/Banner.png)\n\n## Prisma\n\n\u003e [Prisma](https://www.prisma.io/) is Database ORM Library for Node.js, Typescript.\n\nPrisma basically generate each model type definition defined in [`schema.prisma`](https://www.prisma.io/docs/concepts/components/prisma-schema).\nTherefore, it's different from other ORMs so It does not require any additional entry classes or repository layers to model your data.\n\nHowever, there are limitations to prisma solution, if you're building GraphQL APIs with TypegraphQL and Prisma you've to write the same types as classes and enums in TypeGraphQL and maintain both of Prisma definitions and TypegraphQL classes and enums to be synced as you edit them and that's not very cool in my opinion.\n\nSo I created a Prisma generator to help us with generating all of the TypegraphQL models and enums by introspecting the type definitions in `prisma.schema` file and do all of the work for you, so you don't have to constantly go back and forth between your TypegraphQL class types and `prisma.schema` file when you decide to make changes.\n\n## How this differs from [`typegraphql-prisma`](https://github.com/MichalLytek/typegraphql-prisma) by the legend himself @MichalLytek?\n\n### Features\n\n- Doesn't generate CRUD resolvers as [`typegraphql-prisma`](https://github.com/MichalLytek/typegraphql-prisma) does.\n- Generates TypegraphQL class types and enums from your `prisma.schema` file.\n- It searches for a prettier config starting from your working directory and if it was found, It uses it for formatting the generated output.\n- The Generated output is very human readable and doesn't look like generated code what so ever.\n- The Generated output can be edited so you can edit the generated output and the next generation won't overwrite your changes but sustain them.\n- change whatever you want in the generated files and when you mess up the generator will correct you.\n- make the fields be skipped or nullable(require authentication).\n- specify the locations to tell where do you want to output the models and the enums.\n- only installs [`graphql-scalars`](https://github.com/Urigo/graphql-scalars) automatically if any custom scalar types were used in `prisma.schema` like Json or Bytes\n- option to use `yarn` for installing [`graphql-scalars`](https://github.com/Urigo/graphql-scalars) (default is npm).\n- add prefix or suffix to exported class names and enums.\n\n## Usage\n\nDefine Generator in `schema.prisma` and **that's it**\n\n```prisma\ngenerator PrismaTypeGraphQLTypesGenerator {\n  provider     = \"npx prisma-typegraphql-types-generator\"\n  modelsOutput = \"./src/models\" // Optional defaults to \"./src/generated/models\"\n  enumsOutput  = \"./src/types/enums\" // Optional defaults to \"./src/generated/enums\"\n  useYarn      = true // Optional if you want `graphql-scalars` installation to be done via yarn defaults to \"npm\"\n  exportedNameSuffix = \"GQL\" // Optional if you want to add a suffix to the end of your exported class names and enums\n  exportedNamePrefix = \"TYPE\" // Optional if you want to prefix your exported class names and enums\n}\n```\n\nIf this is the `prisma.schema` 👇\n\n\u003e ℹ you can set the `modelsOutput` and the `enumsOutput` paths to be wherever you want and the class models files will figure out their way to get to the enums path **no problem**.\n\n```prisma\ngenerator client {\n  provider = \"prisma-client-js\"\n}\n\ngenerator PrismaTypeGraphQLTypesGenerator {\n  provider     = \"npx prisma-typegraphql-types-generator\"\n  modelsOutput = \"./src/models\"\n  enumsOutput  = \"./src/types/enums\"\n  useYarn      = true\n}\n\ndatasource db {\n  provider = \"postgresql\"\n  url      = env(\"DATABASE_URL\")\n}\n\nmodel User {\n  id                            String                 @id @default(cuid())\n  name                          String                 @db.VarChar(255)\n  // @nullable\n  email                         String                 @unique\n  // @skip\n  password                      String?\n  bio                           String?                @db.VarChar(160)\n  // @skip\n  tokenVersion                  Int                    @default(0)\n  // @skip\n  confirmed                     Boolean                @default(false)\n  profilePic                    String?\n  posts                         Post[]\n  githubId                      String?                @unique\n  lastTimelineVisit             DateTime?\n  createdAt                     DateTime               @default(now())\n  updatedAt                     DateTime               @updatedAt\n}\n\nmodel Post {\n  id             String    @id @default(cuid())\n  title          String\n  body           String\n  tags           String[]\n  published      Boolean   @default(false)\n  // @skip\n  authorId       String\n  author         User      @relation(fields: [authorId], references: [id], onDelete: Cascade)\n  readingTimeTxt String\n  readingTimeMin Float\n  coverImg       String?\n  createdAt      DateTime  @default(now())\n  updatedAt      DateTime  @updatedAt\n}\n```\n\nThe generated output will be like this 😎\n\n```typescript\n// src/models/User.ts\nimport { Field, ID, ObjectType } from 'type-graphql'\nimport { Post } from './Post'\n\n@ObjectType()\nexport class User {\n  @Field((_type) =\u003e ID)\n  id: string\n\n  @Field()\n  name: string\n\n  @Field({ nullable: true })\n  email: string\n\n  @Field({ nullable: true })\n  bio?: string\n\n  @Field({ nullable: true })\n  profilePic?: string\n\n  @Field((_type) =\u003e [Post])\n  posts: Post[]\n\n  @Field({ nullable: true })\n  githubId?: string\n\n  @Field({ nullable: true })\n  lastTimelineVisit?: Date\n\n  @Field()\n  createdAt: Date\n\n  @Field()\n  updatedAt: Date\n\n  // skip overwrite 👇\n}\n```\n\n```typescript\n// src/models/Post.ts\nimport { Field, ID, ObjectType, Float } from 'type-graphql'\nimport { User } from './User'\nimport { Language } from '../src/types/enums/Language'\n\n@ObjectType()\nexport class Post {\n  @Field((_type) =\u003e ID)\n  id: string\n\n  @Field()\n  title: string\n\n  @Field()\n  body: string\n\n  @Field((_type) =\u003e [String])\n  tags: string[]\n\n  @Field()\n  published: boolean\n\n  @Field((_type) =\u003e User)\n  author: User\n\n  @Field()\n  readingTimeTxt: string\n\n  @Field((_type) =\u003e Float)\n  readingTimeMin: number\n\n  @Field({ nullable: true })\n  coverImg?: string\n\n  @Field((_type) =\u003e Language)\n  language: Language\n\n  @Field()\n  createdAt: Date\n\n  @Field()\n  updatedAt: Date\n\n  // skip overwrite 👇\n}\n```\n\n```typescript\n// src/types/enums/Language.ts\nimport { registerEnumType } from 'type-graphql'\n\nexport enum Language {\n  Typescript = 'Typescript',\n  Javascript = 'Javascript',\n  Go = 'Go',\n  Rust = 'Rust',\n  Python = 'Python',\n  Java = 'Java',\n  Swift = 'Swift',\n}\nregisterEnumType(Language, {\n  name: 'Language',\n})\n```\n\n## What's the `// @skip` and `// @nullable` do in `prisma.schema` file?\n\n`// @skip` before a field in your `prisma.schema` file means you're telling the generator that this field is just specific for backend stuff and won't be queryable by graphql clients, so it skips adding it to the class type.\n\n`// @nullable` before a field in your `prisma.schema` file means you're telling the generator that this field can be queryable but it depends on who's asking, so an email as an example won't be exposed to anyone just authenticated user can show his email, so It marks that field as `nullable: true` to assure that you won't get the email of the user if you're not that user himself.\n\n## How to edit the Generated Code without being overwritten by the generator?\n\nYou've probably noticed the `// skip overwrite 👇` comment at the very bottom of any generated class model and this's a part of what I like to call **Safe Areas** where you can write code without being overwritten by the generator.\n\n### So there're 4 **Safe Areas**:\n\n1- above the class where you can add your own logic here and import other files/libraries\n\n\u003e ℹ when you try messing up by removing imports that a class needs, the generator will correct you and add it again\n\n```diff\n// src/models/User.ts\nimport { Field, ID, ObjectType } from 'type-graphql'\nimport { Post } from './Post'\n+ import { addTwoNumbers } from '../utils/sillyStuff'\n+\n+ console.log(addTwoNumbers(1, 4))\n+\n@ObjectType()\nexport class User {\n  @Field((_type) =\u003e ID)\n  id: string\n  ...\n```\n\n2- Field Config object\n\n```diff\n// src/models/User.ts\n@ObjectType()\nexport class User {\n  ...\n- @Field({ nullable: true })\n+ @Field({\n+   nullable: true\n+   description: 'This field is looking kinda sussy',\n+   simple: true,\n+   complexity: 5,\n+ })\n  username?: string\n  ...\n```\n\n3- below the `// skip overwrite 👇` comment\n\n```diff\n// src/models/User.ts\n@ObjectType()\nexport class User {\n  ...\n  // skip overwrite 👇\n+ @Field()\n+ sayHello: string\n}\n```\n\n4- after the class\n```diff\n// src/models/User.ts\n@ObjectType()\nexport class User {\n  ...\n  // skip overwrite 👇\n}\n\n+ @ObjectType()\n+ export class UserPayload {\n+   @Field((_type) =\u003e User)\n+   data: User\n+\n+   @Field()\n+   token: string\n+ }\n```\n\n## Real World Example\n[Blogs/Podcasts-Platform](https://github.com/YassinEldeeb/Blogs-Podcasts-Platform/tree/master/packages/server)\n\n## Known Issues\n\nBasically the only times you'll see the TypeGraphQL file introspecter fails is caused by the leaky implementation I did to grab the object from the field decorator.\n\n1- Can't use the object shorthand syntax in the field config object.\n\nWon't work 👇\n\n```diff\n...\nimport { complexity } from '../shared/complexity'\n\n@ObjectType()\nexport class User {\n+ @Field((_type) =\u003e ID, { complexity })\n  id: string\n  ...\n}\n```\n\n2- Using commas in any string in the field config object confuses the introspecter to make it think that this comma is the end of defining a certain field in this object, so currently the solution is to add the string to a variable outside of the class and use it bellow.\n\nWon't work 👇\n\n```diff\n...\nimport { complexity } from '../shared/complexity'\n\n@ObjectType()\nexport class User {\n+ @Field((_type) =\u003e ID, { name: \",\", complexity: 1 })\n  id: string\n  ...\n}\n```\nWill work 👇\n\n```diff\n...\nimport { complexity } from '../shared/complexity'\n\nconst IDFieldName = \",\"\n\n@ObjectType()\nexport class User {\n+ @Field((_type) =\u003e ID, { name: IDFieldName, complexity: 1 })\n  id: string\n  ...\n}\n```\n\n# Contributing\nWe'll be very thankful for all your contributions, whether it's for helping us find issues in our code, highlighting features that're missing, writing tests for uncovered cases, or contributing to the codebase.\n\nRead the [Contributing guide](https://github.com/YassinEldeeb/prisma-tgql-types-gen/blob/main/CONTRIBUTING.md) to get started.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyassineldeeb%2Fprisma-tgql-types-gen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyassineldeeb%2Fprisma-tgql-types-gen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyassineldeeb%2Fprisma-tgql-types-gen/lists"}