Skip to content

Commit

Permalink
feat(gql-utils): add completion handler to create-data-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
usirin committed Aug 11, 2023
1 parent 0d229db commit 0322f0e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
14 changes: 8 additions & 6 deletions apps/gql/loaders/pano.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ const createPanoUpvoteLoaders = ({ prisma }: Clients) => {
results.map((result) => (result.status === "fulfilled" ? result.value : null))
);

results.forEach((upvote) => {
return results;
},
(results) => {
results?.forEach((upvote) => {
if (upvote) {
byPostID.prime(upvote.id, upvote);
}
});

return results;
}
);

Expand All @@ -87,13 +88,14 @@ const createPanoUpvoteLoaders = ({ prisma }: Clients) => {
results.map((result) => (result.status === "fulfilled" ? result.value : null))
);

results.forEach((upvote) => {
return results;
},
(results) => {
results?.forEach((upvote) => {
if (upvote) {
byCommentID.prime(upvote.id, upvote);
}
});

return results;
}
);

Expand Down
15 changes: 13 additions & 2 deletions packages/gql-utils/data-loader/create-data-loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import DataLoader, { type BatchLoadFn } from "dataloader";

export function createDataLoader<T, U>(batchFn: BatchLoadFn<T, U>) {
return new DataLoader(batchFn);
export function createDataLoader<TKey, TValue>(
batchFn: BatchLoadFn<TKey, TValue>,
onComplete?: (values: TValue[]) => void
) {
return new DataLoader(async (keys: readonly TKey[]) => {
const result = (await batchFn(keys)) ?? [];

console.log({ result });

onComplete?.(result as unknown as TValue[]);

return result;
});
}

0 comments on commit 0322f0e

Please sign in to comment.