0
I’m currently building a GraphQL API using TypeORM and Nexus Schema. I’ve defined several entities with associations between them, such as Product, Brand, and User. The problem I’m facing is that when I query the Product entity using GraphQL, the related fields like category_id and brand_id are returning null values even though there is data in the database.
Product Entity:
// Product.ts
import { BaseEntity, Entity, Column, ManyToOne, OneToMany, JoinColumn } from 'typeorm';
import { User } from './User';
import { Brand } from './Brand';
import { Category } from './Category';
@Entity()
export class Product extends BaseEntity {
@Column()
creatorId!: number;
@ManyToOne(() => User, (user) => user.products)
creator: User;
@ManyToOne(() => Brand, (brand) => brand.products)
@JoinColumn({ name: 'brand_id' })
brand: Brand;
@ManyToOne(() => Category, (category) => category.products)
@JoinColumn({ name: 'category_id' })
category: Category;
}
GraphQL Nexus Schema:
// ProductType.ts
import { objectType } from 'nexus';
import { context } from './context';
import { User } from './User';
export const ProductType = objectType({
name: 'Product',
definition(t) {
t.nonNull.int('id');
t.nonNull.string('name');
t.int('creatorId');
t.nonNull.int('category_id');
t.nonNull.int('brand_id');
t.field('createdBy', {
type: 'User',
resolve(parent, _args, _context: context, _info): Promise<User | null> {
return User.findOne({ where: { id: parent.creatorId } });
},
});
},
});
brand entity:
//brand.ts
import {
BaseEntity,
Entity,
Column,
CreateDateColumn,
UpdateDateColumn,
PrimaryGeneratedColumn,
ManyToOne,
OneToMany,
} from 'typeorm';
import { User } from './User';
import { Product } from './Product';
import { OrderItem } from './OrderItem';
@Entity()
export class Brand extends BaseEntity {
@PrimaryGeneratedColumn()
id!: number;
@Column({ unique: true })
name!: string;
@OneToMany(() => Product, (product) => product.brand)
products: Product[];
@CreateDateColumn()
createdAt: Date;
@UpdateDateColumn()
updatedAt: Date;
}
product entity:
import {
BaseEntity,
Entity,
Column,
CreateDateColumn,
UpdateDateColumn,
PrimaryGeneratedColumn,
OneToMany,
ManyToOne,
JoinColumn,
} from 'typeorm';
import { User } from './User';
import { Wishlist } from './Wishlist';
import { Review } from './Review';
import { OrderItem } from './OrderItem';
import { Brand } from './Brand';
import { Category } from './Category';
import { ProductImage } from './ProductImage';
@Entity()
export class Product extends BaseEntity {
@PrimaryGeneratedColumn()
id!: number;
@Column()
name!: string;
@Column()
description!: string;
@Column({ type: 'decimal' })
price!: number;
@Column()
available!: boolean;
@Column()
creatorId!: number;
@ManyToOne(() => User, (user) => user.products)
creator: User;
@ManyToOne(() => Brand, (brand) => brand.products)
@JoinColumn({ name: 'brand_id' })
brand: Brand;
@ManyToOne(() => Category, (category) => category.products)
@JoinColumn({ name: 'category_id' })
category: Category;
@OneToMany(() => Wishlist, (wishlist) => wishlist.product)
wishlist: Wishlist[];
@OneToMany(() => Review, (review) => review.product)
review: Review[];
@OneToMany(() => OrderItem, (orderitem) => orderitem.product)
order: OrderItem[];
@OneToMany(() => ProductImage, (image) => image.product)
images: ProductImage[];
@CreateDateColumn()
createdAt: Date;
@UpdateDateColumn()
updatedAt: Date;
}
I’ve confirmed that the category_id and brand_id fields have data in the database. However, when querying the Product entity, these fields return null. I’ve checked my associations and entity definitions, but I can’t seem to figure out the issue.
Can anyone help me understand why the related fields are returning null values, even though the data is present in the database? Is there anything I might be missing in my setup?
Any guidance or suggestions would be greatly appreciated. Thank you!
|