#Error: Undefined type error. Make sure you are providing an explicit type for the "products" Graphql

17 messages · Page 1 of 1 (latest)

brittle helm
#

Hello, I am facing an error and I have no idea what is going on, So i have a resolver class called Cart and it has a ResolveField resolver "products" with a getProducts method, but even if i correctly give the return type to Resolver, it still throws me the error.

#

Source code : ```ts

@Resolver(() => Cart)
export class CartsResolver {
constructor(
private readonly cartsService: CartsService,
private readonly productsService: ProductsService,
) {}

@Mutation(() => ResponseAddToCart)
@UseGuards(JwtAuthGuard)
async addItemsToCart(
@Args('addItemsToCart') itemsInput: CartInputArray,
@GetUser() user: User,
) {
await this.cartsService.addItemsToCart(user.id, itemsInput.array);
const updatedCart = await this.cartsService.findCartOfUser(user.id);
return updatedCart;
}
// talking about this resolver field
@ResolveField('products', () => ProductsResponse)
getProducts(@Parent() product: Product): Promise<Product> {
return this.productsService.findOne(product.id);
}
}

#

The ProductResponse Graphql type is: ```ts
@ObjectType()
@Entity('products')
export class Product {
@PrimaryGeneratedColumn('uuid')
@Field(() => ID)
id: string;

@Column()
@Field()
name: string;

@Column()
@Field()
description: string;

@Column()
@Field(() => Float)
price: number;

@Column({
type: 'boolean',
default: true,
})
@Field(() => Boolean)
is_veg: boolean;

@Column()
@Field()
category_id: string;

// relations
@ManyToOne(() => Category, (category) => category.products, {
onDelete: 'CASCADE',
})
@JoinColumn({ name: 'category_id' })
@Field(() => Category)
category: Category;
}
// this is the returned graphql type thing
@ObjectType()
export class ProductsResponse extends OmitType(Product, [] as const) {}

#

Error:

#

Error: Undefined type error. Make sure you are providing an explicit type for the "products" of the "Cart" class.

twilit flare
#

What does your Cart class look like?

brittle helm
#

Here's my cart entity : ```ts
@ObjectType()
@Entity('carts')
export class Cart {
@PrimaryGeneratedColumn('uuid')
@Field()
id: string;

@Column()
@Field()
user_id: string;

@OneToOne(() => User)
@Field()
@JoinColumn({
name: 'user_id',
})
user: User;

@ManyToMany(() => Product)
@JoinTable({
name: 'carts_products',
joinColumn: {
name: 'cart_id',
referencedColumnName: 'id',
},
inverseJoinColumn: {
name: 'product_id',
referencedColumnName: 'id',
},
})
@Field()
products: Product[];
}

twilit flare
brittle helm
#

ooh thank you sir, haha

twilit flare
#

You'll have the same issue with your user property too.

#

TypeScript can only infer very basic types via reflection. Otherwise, you must specify them in the decorator.

brittle helm
#

That error was gone but new one showed up:

#
// Resolver
@Resolver(() => Cart)
export class CartsResolver {
  constructor(
    private readonly cartsService: CartsService,
    private readonly productsService: ProductsService,
  ) {}
  @Query((returns) => ResponseAddToCart)
  @UseGuards(JwtAuthGuard)
  carts(@GetUser() user: User) {
    return this.cartsService.findCartOfUser(user.id);
  }
  @Mutation(() => ResponseAddToCart)
  @UseGuards(JwtAuthGuard)
  async addItemsToCart(
    @Args('addItemsToCart') itemsInput: CartInputArray,
    @GetUser() user: User,
  ) {
    await this.cartsService.addItemsToCart(user.id, itemsInput.array);
    const updatedCart = await this.cartsService.findCartOfUser(user.id);
    return updatedCart.id;
  }
  // this is the products (IF I COMMENT THIS, ERROR GOES AWAY)
  @ResolveField('products', () => [Product])
  async getProducts(@Parent() cart: Cart): Promise<Array<Product>> {
    return await this.productsService.findProductsFromCart(cart.id);
  }

}
#
// product entity
@ObjectType()
@Entity('products')
export class Product {
  @PrimaryGeneratedColumn('uuid')
  @Field(() => ID)
  id: string;

  @Column()
  @Field()
  name: string;

  @Column()
  @Field()
  description: string;

  @Column()
  @Field(() => Float)
  price: number;

  @Column({
    type: 'boolean',
    default: true,
  })
  @Field(() => Boolean)
  is_veg: boolean;

  @Column()
  @Field()
  category_id: string;

  // relations
  @ManyToOne(() => Category, (category) => category.products, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'category_id' })
  @Field(() => Category)
  category: Category;
}
#
// cart entity
@ObjectType()
@Entity('carts')
export class Cart {
  @PrimaryGeneratedColumn('uuid')
  @Field()
  id: string;

  @Column()
  @Field()
  user_id: string;

  @OneToOne(() => User)
  @Field(() => User)
  @JoinColumn({
    name: 'user_id',
  })
  user: User;

  @ManyToMany(() => Product)
  @JoinTable({
    name: 'carts_products',
    joinColumn: {
      name: 'cart_id',
      referencedColumnName: 'id',
    },
    inverseJoinColumn: {
      name: 'product_id',
      referencedColumnName: 'id',
    },
  })
  @Field(() => [Product])
  products: Product[];
}```
#
// ResponseAddToCart Mutations' return thing
@ObjectType()
export class ResponseAddToCart {
  @Field()
  cart_id: string;

  @Field(() => [Product])
  products: Array<Product>;
}```
#

Error: "Cart" defined in resolvers, but not in schema