Andre Gustavo
07/03/2020, 5:04 AMsrc/user/user.resolver.ts:11:5 - error TS2322: Type 'import("/NestJS/librarydash-prisma/node_modules/.prisma/client/index").User[]' is not assignable to type 'import("/NestJS/librarydash-prisma/src/models/user.model").User[]'.
Type 'User' is missing the following properties from type 'User': createdAt, updatedAt, books
11 return await this.prisma.user.findMany();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[01:51:27] Found 1 error. Watching for file changes.
schema.prisma
// This is your Prisma schema file,
// learn more about it in the docs: <https://pris.ly/d/prisma-schema>
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
role RoleUser @default(DEFAULT)
firstName String
lastName String?
email String @unique
password String
books Book[]
}
model Book {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String @unique
author String[]
publishedYear Int
publishingCompany String
copies Int
availability Boolean
status String
coverImage String[]
categories Category[] @relation(references: [id])
createdBy User @relation(fields: [userId], references: [id])
userId Int
}
model Category {
id Int @id @default(autoincrement())
name String
books Book[] @relation(references: [id])
}
enum RoleUser {
ADMIN
DEFAULT
MANAGER
}
user.model.ts
import { ObjectType, Field, Int, HideField } from '@nestjs/graphql';
import { Book } from './book.model';
import { RoleUser } from './../types';
@ObjectType('User')
export class User {
@Field(type => Int)
id: number;
@Field(type => Date)
createdAt: Date;
@Field(type => Date)
updatedAt: Date;
@Field(type => RoleUser)
role: RoleUser;
@Field(type => String)
firstName: string;
@Field(type => String)
lastName?: string;
@Field(type => String)
email: string;
@HideField()
password: string;
@Field(type => [Book])
books: Book[];
}
book.model.ts
import { ObjectType, Field, Int } from '@nestjs/graphql';
import { Category } from './category.model';
import { User } from './user.model';
@ObjectType()
export class Book {
@Field(type => Int)
id: number;
@Field(type => Date)
createdAt: string;
@Field(type => Date)
updatedAt: string;
@Field()
title: string;
@Field(type => [String])
author: string[];
@Field()
publishedYear: number;
@Field()
publishingCompany: string;
@Field()
copies: number;
@Field()
availability: boolean;
@Field()
status: string;
@Field(type => [String])
coverImage: string[];
@Field(type => [Category])
categories: Category;
@Field(type => User)
createdBy: User;
}
category.model.ts
import { ObjectType, Field, Int } from '@nestjs/graphql';
import { Book } from './book.model';
@ObjectType()
export class Category {
@Field(type => Int)
id: number;
@Field()
name: string;
@Field(type => [Book])
books: Book[];
}
user.resolver.ts
import { Resolver, Query, ResolveField, Parent } from '@nestjs/graphql';
import { PrismaService } from './../prisma/prisma.service';
import { User } from './../models';
@Resolver(of => User)
export class UserResolver {
constructor(private prisma: PrismaService) {}
@Query(returns => [User])
async users(): Promise<User[]> {
return await this.prisma.user.findMany();
}
}
How to fix it????
Thank you!!!!Ryan
07/03/2020, 7:45 AMAndre Gustavo
07/03/2020, 8:15 AM