Hi everyone! I was getting started with GraphQL code first resolvers according to the doc
https://docs.nestjs.com/graphql/resolvers
I ran into an issue with type safety. Here's an example
@Resolver(() => Author)
export class AuthorsResolver {
@Query(() => Author)
async author(@Args('id', { type: () => Int }) id: number) {
return {id, firstName: 'John', lastName: 'Doe' };
}
@ResolveField()
async posts(@Parent() author: Author) {
// Well, we have the id, everything as expected
const { id } = author;
console.log({ id });
// The type of `posts` is `Post[]`!
// So should be safe to read the property of it, right?
const { posts } = author;
// Uh-oh... 🛑 "Cannot read properties of undefined (reading 'length')"
console.log(posts.length);
// return this.postsService.findAll({ authorId: id });
return [];
}
}
The docs suggest declaring the resolver parent as author: Author, but in reality the type is "whatever was returned from the parent resolver", not Author. Is there something I'm missing here?
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).