14

I am using nestjs with prisma and swagger. When I use

import {PrismaGeneratedType} from '@prisma/client';

...

@ApiOkResponse({type: PrismaGeneratedType})

in a controller if gives error:

'PrismaGeneratedType' only refers to a type, but is being used as a value here

since prisma generate types and not class definitions. How can I use Prisma generated client (types) with swagger?

some-user
  • 3,888
  • 5
  • 19
  • 43
Ahmed Nawaz Khan
  • 1,451
  • 3
  • 20
  • 42
  • 3
    As of now, this isn't really possible. You'll have to manually write out the response types to match your Prisma Client. Please feel free to make a comment on the [existing issue](https://github.com/prisma/prisma/issues/8334) about this, so we can better track the demand for this usecase. – Tasin Ishmam Aug 16 '21 at 07:50
  • 3
    Update: it's now possible using prisma class generator https://www.prisma.io/docs/concepts/components/prisma-schema/generators – Antony Gibbs Feb 10 '23 at 19:40

1 Answers1

1

Actually I was looking for the use case of the DTO in nestjs when you have the prisma generated types

As we can't use the prisma generated type in the swagger decorator, you will have to use DTOs to make it work, as prisma doesn't provide auto generated DTOs

It's in discussion and it's a big subject, I don't know if they planned to add this feature in the future version, but actually to make your swagger work you will have to pass by DTOs

You can find some libs to generate this DTOs for you using your prisma schema as https://www.npmjs.com/package/tsoa

I didn't reviewed it myself as I m still looking for the one that could work properly, so you will probably have to make some search too

But to answer your main question, no you can't use prisma generated type for now :')

Valentin
  • 56
  • 5