type-graphql-dataloader
type-graphql-dataloader copied to clipboard
Could not mention the return type of a field resolver function with @Loader decorator
How do i mention the return of photos function as Promise<Photo[] | undefined> ?
@FieldResolver() @Loader<number, Photo[]>(async (ids, { context }) => { // batchLoadFn const photos = await getRepository(Photo).find({ where: { user: { id: In([...ids]) } }, }); const photosById = groupBy(photos, "userId"); return ids.map((id) => photosById[id] ?? []); }) photos(@Root() root: User) { return (dataloader: DataLoader<number, Photo[]>) => dataloader.load(root.id); }
Hey, it's not related to this lib, but you can define type of FieldResolver inside it, like this:
@FieldResolver(() => [Photo])
More info here: https://typegraphql.com/docs/0.17.0/resolvers.html
i was not clear before. I was asking about typing the return type after photos(@root() root: User)
like photos(@root() root: User): Promise<Photo[]>