user3474541
user3474541

Reputation: 167

Graphql NestJS: Undefined type error. Make sure you are providing an explicit type

Could see exception with the below type definition.

UnhandledPromiseRejectionWarning: Error: Undefined type error. Make sure you are providing an explicit type for the "rows" of the "CommentsView"

@ObjectType()
export class CommentsView {

    
 @Field({ nullable: true })
 count: number;

 @Field({ nullable: true })
 rows: Comment[];

}

So, have defined the type on the filed like below, but its throwing new error

(node:14964) UnhandledPromiseRejectionWarning: Error: Schema must contain uniquely named types but contains multiple types named "Comment".

@ObjectType()
export class CommentsView {

    
 @Field({ nullable: true })
 count: number;

 @Field(() => [Comment])
 rows: Comment[];

}

Upvotes: 0

Views: 8962

Answers (2)

Yash Pokar
Yash Pokar

Reputation: 5451

@ObjectType()
export class CommentsView {

    
 @Field(() => Int, { nullable: true })
 count: number;

 @Field(() =>[Comment], { nullable: true })
 rows: Comment[];
}

Try this out

Upvotes: 0

skink
skink

Reputation: 5711

You're most likely using the Comment class as both input and object type simultaneously, which is not supported in GraphQL.

One solution would be providing a different input type name:

@ObjectType()
@InputType("CommentInput")
export class Comment {
  ...
}

Another would be providing different subclasses for input and object types:

@ObjectType({ isAbstract: true })
@InputType({ isAbstract: true })
export class Comment {
  ...
}

@ObjectType()
export class CommentObject extends Comment {}

@InputType()
export class CommentInput extends Comment {}

@ObjectType()
export class CommentsView {
  ...

  @Field(CommentObject, { nullable: true })
  rows: CommentObject[];
}

// somewhere else where you used the Comment class as an input
...
@InputType()
export class CommentUserInput {
  @Field(() => CommentInput)
  comment: CommentInput;
}

Upvotes: 4

Related Questions