Flutter Ferry Graphql pointing to the Entitiy not the dto - flutter

I have an entity is Nestjs
#Entity({ name: 'recipes' })
#ObjectType()
export class Recipe {
#PrimaryGeneratedColumn()
#Field(() => Int)
id: number;
#Column()
#Field()
videoUrl: string;
#Column()
#Field()
description: string;
}
I also have a create recipe dto
#InputType()
export class CreateRecipeInput {
#Field()
videoUrl: string;
#Field()
description: string;
#Field(() => [String])
ingredientNames: string[];
#Field(() => [String])
instructionNames: string[];
}
in my ferry Graphql I have this
mutation CreateRecipe($createRecipeInput: CreateRecipeInput!) {
createRecipe(createRecipeInput: $createRecipeInput) {
videoUrl
description
ingredientNames
}
}
The problem I have is if I get an error in the property ingredientNames, but if I add that property to the Recipe entity it works. It's like Ferry is not following the Recipe Dto. When I look at the schema.graphql is flutter The create recipe Dto is there.
input CreateRecipeInput {
videoUrl: String!
description: String!
ingredientNames: [String!]!
instructionNames: [String!]!
}

Related

Typeorm Listener trigger another entity that have relation with

I am using Typeorm and I want to trigger the listener of my entity if an entity that have relation with has been updated relation, here is an example:
#Entity()
export class User {
#PrimaryGeneratedColumn()
id: number;
#Column({ type: 'character varying'})
Name: string;
#Column({ type: 'character varying'})
email: string;
#OneToMany(() => Photo, (photo) => photo.user)
photos: Photo[]
}
and
#Entity()
export class Photo {
#PrimaryGeneratedColumn()
id: number;
#Column({ type: 'integer'})
size: string;
#Column({ type: 'character varying'})
userEmail: string;
#OneToOne(() => User, (user) => user.photo)
#JoinColumn()
user: User
#BeforeUpdate()
async emailChange() {
try{
let repo: any = await Object.values(require(`../../../repository/User.repository`),)[0];
const userRepository:any = getCustomRepository(repo);
const result = await userRepository.findOne({ where: {'id' : this.user}});
this.userEmail=result.email
}catch(err){
console.log('emailChange Error =====> ',err);
}
}
}
Thanks for any help in advance
Typeorm: 0.2.45
Postgresql: 13

Nest.js, Graphql, Typeorm, Postgres mutations

I`m a newbee at this technologies and I literally dont understand how to do task. My task: create 2 tables, for categories and tasks, after that create mutation for creating category and tasks(todos).
mutation {
createTodo(input: {
categoryName: “”,
text: “”
}) {
category: {
id
title
}
id
text
isCompleted
}
}
I created objectTypes and input types, but I dont understand how to create such mutation. Example objectTypes and Inputs
#ObjectType()
export class CategoryType {
#Field(() => ID)
id: number
#Field(() => String)
readonly title: string
}
export interface TodoI {
id: number;
text: string;
isCompleted: boolean;
categoryId: number
category: any;
}
#InputType()
export class CategoryInput {
#Field()
readonly title: string
#Field(() => TodoInput,{nullable: true})
readonly todos: TodoI
}
#ObjectType()
export class TodoType {
#Field(() => ID)
id: number
#Field()
readonly text: string
#Field()
readonly isCompleted: boolean
#Field(() => Int)
readonly categoryId: number
}
#InputType()
export class TodoInput {
#Field()
readonly text: string;
#Field()
readonly isCompleted: boolean;
#Field(() => Int)
readonly categoryId: number;
}

How to select data by using typeorm from 3 tables which depend on each other?

I have 3 entity which depend on each other and I have a problem with querying data from them by using one request.
First one User:
#Entity()
export class User {
#PrimaryGeneratedColumn()
id: number;
#Column()
firstName: string;
#Column()
lastName: string;
#Column()
login: string;
#Column()
password: string;
#Column()
ownerId: number;
#OneToOne(() => Role, (role) => role.user)
#JoinColumn()
role?: Role;
}
Second one Role:
#Entity()
export class Role extends BaseEntity {
#PrimaryGeneratedColumn()
id: number;
#Column()
name: string;
#Column()
description: string;
#ManyToOne(() => User, (user) => user.role)
#JoinColumn()
user: User | null;
#ManyToMany(() => Permission, { cascade: true })
#JoinTable({ name: 'roles_has_permissions' })
permissions: Permission[];
}
Third one Permission:
#Entity()
export class Permission {
#PrimaryGeneratedColumn()
id: number;
#Column()
name: PossiblePermissions;
}
How to select data from database. I’m using typeorm with Postgresql and I want to get array like this.
[{
…user info by ownerId
role: {
…role which related this user
permissions: [{
…permissions which related this role
}, …]
}
}, …]
Use query builder https://typeorm.io/#/select-query-builder
In your case it should be something like this:
await connection
.getRepository(User)
.createQueryBuilder("user")
.leftJoinAndSelect("user.role", "role")
.leftJoinAndSelect("role.permissions", "permissions")
.getMany()

NestJS/TypeORM error: The value passed as UUID is not a string when inserting record

I created a NestJS sample application that uses TypeORM to access the Postgres database.
The complete codes can be found from this link.
There are two entities like this.
#Entity({ name: 'posts' })
export class PostEntity {
#PrimaryGeneratedColumn('uuid')
id?: string;
#Column()
title: string;
#Column({ nullable: true })
content?: string;
#OneToMany((type) => CommentEntity, (comment) => comment.post, {
cascade: true,
})
comments?: Promise<CommentEntity[]>;
#ManyToOne((type) => UserEntity, { nullable: true })
#JoinColumn({ name: 'author_id' })
author?: UserEntity;
#RelationId((post: PostEntity) => post.author)
authorId?: string;
#CreateDateColumn({ name: 'created_at', type: 'timestamp', nullable: true })
createdAt?: Date;
#UpdateDateColumn({ name: 'updated_at', type: 'timestamp', nullable: true })
updatedAt?: Date;
}
#Entity({ name: 'comments' })
export class CommentEntity {
#PrimaryGeneratedColumn('uuid')
id: string;
#Column()
content: string;
#ManyToOne((type) => PostEntity, (p) => p.comments)
#JoinColumn({ name: 'post_id' })
post: PostEntity;
#RelationId((comment: CommentEntity) => comment.post)
postId?: string;
#CreateDateColumn({ name: 'created_at', type: 'timestamp' })
createdAt: Date;
}
When adding a comment through the GraphQL endpoint, which will call the following codes.
addComment(id: string, comment: string): Observable<Comment> {
const entity = new CommentEntity();
Object.assign(entity, {
content: comment,
postId: id,
});
return from(this.commentRepository.save(entity)).pipe(
map((c) => {
return { id: c.id, content: c.content } as Comment;
}),
);
}
When running the e2e tests it will fail due to an error message(I added a console.log to print the GraphQL errors in the response body):
addComment errors: [{"message":"The value passed as UUID is not a string"}]

Typegoose binding referernce to polymorphic model

I have a situation with polymorphic models
interface IEvent {
title: string;
}
class SubscribeEvent implements IEvent {
#prop()
title: string;
#prop({ref: User})
user: IUser;
}
class NewsEvent implements IEvent {
#prop()
title: string;
#prop({ref: Post})
post: IPost;
#prop({ref: User})
user: IUser;
}
class OrderEvent implements IEvent {
#prop()
title: string;
#prop({ref: Order})
order: IOrder;
}
Now I created a typegoose model:
class Card extends Typegoose {
#prop()
name: string;
#prop()
events: IEvent[];
}
How do I correctly map and extract the Card instance with populated references?