Skip to content

Commit

Permalink
chore: add dataloader reference (#4073)
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Mar 24, 2023
1 parent d91c6a6 commit f865f66
Show file tree
Hide file tree
Showing 4 changed files with 643 additions and 134 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -12,6 +12,7 @@
"dev-www": "turbo --filter www dev",
"test": "turbo --filter \"@trpc/tests\" test",
"test-watch": "cd packages/tests && pnpm test-watch",
"vitest": "cd packages/tests && pnpm vitest",
"oldlint": "eslint --ext \".js,.ts,.tsx\" --ignore-path .gitignore . --report-unused-disable-directives",
"lint": "turbo lint",
"lint-fix": "turbo lint -- --fix && manypkg fix",
Expand Down
1 change: 1 addition & 0 deletions packages/tests/package.json
Expand Up @@ -34,6 +34,7 @@
"@vitest/ui": "^0.28.5",
"abort-controller": "^3.0.0",
"concurrently": "^7.6.0",
"dataloader": "^2.2.2",
"devalue": "^4.0.0",
"eslint": "^8.30.0",
"expect-type": "^0.15.0",
Expand Down
61 changes: 61 additions & 0 deletions packages/tests/showcase/dataloader.test.ts
@@ -0,0 +1,61 @@
import { routerToServerAndClientNew } from '../server/___testHelpers';
import { inferAsyncReturnType, initTRPC } from '@trpc/server';
import { CreateHTTPContextOptions } from '@trpc/server/adapters/standalone';
import DataLoader from 'dataloader';
import { konn } from 'konn';
import { z } from 'zod';

const posts = [
{
id: 1,
text: 'foo',
},
{
id: 2,
text: 'bar',
},
];

function createContext(_opts: CreateHTTPContextOptions) {
return {
postLoader: new DataLoader(async (ids: ReadonlyArray<number>) => {
return ids.map((id) => posts.find((post) => post.id === id));
}),
};
}
type Context = inferAsyncReturnType<typeof createContext>;

const ctx = konn()
.beforeEach(() => {
const t = initTRPC.context<Context>().create();

const appRouter = t.router({
post: t.router({
byId: t.procedure
.input(
z.object({
id: z.number(),
}),
)
.query(({ input, ctx }) => ctx.postLoader.load(input.id)),
}),
});
return routerToServerAndClientNew(appRouter, {
server: {
createContext,
},
});
})
.afterEach(async (ctx) => {
await ctx?.close?.();
})
.done();

test('dataloader', async () => {
const result = await Promise.all([
ctx.proxy.post.byId.query({ id: 1 }),
ctx.proxy.post.byId.query({ id: 2 }),
]);

expect(result).toEqual([posts[0], posts[1]]);
});

3 comments on commit f865f66

@vercel
Copy link

@vercel vercel bot commented on f865f66 Mar 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

www – ./www

www-trpc.vercel.app
beta.trpc.io
www-git-main-trpc.vercel.app
www.trpc.io
alpha.trpc.io
trpc.io

@vercel
Copy link

@vercel vercel bot commented on f865f66 Mar 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

next-prisma-starter – ./examples/next-prisma-starter

next-prisma-starter-trpc.vercel.app
next-prisma-starter-git-main-trpc.vercel.app
nextjs.trpc.io

@vercel
Copy link

@vercel vercel bot commented on f865f66 Mar 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

og-image – ./www/og-image

og-image.trpc.io
og-image-three-neon.vercel.app
og-image-git-main-trpc.vercel.app
og-image-trpc.vercel.app

Please sign in to comment.