type-graphql-dataloader icon indicating copy to clipboard operation
type-graphql-dataloader copied to clipboard

Could not mention the return type of a field resolver function with @Loader decorator

Open malisetti opened this issue 3 years ago • 2 comments

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); }

malisetti avatar Mar 19 '21 12:03 malisetti

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

nudabagana avatar Apr 08 '21 13:04 nudabagana

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[]>

malisetti avatar Apr 08 '21 13:04 malisetti