Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add dataloader reference (#4073)
- Loading branch information
Showing
4 changed files
with
643 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]]); | ||
}); |
Oops, something went wrong.
f865f66
There was a problem hiding this comment.
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
f865f66
There was a problem hiding this comment.
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
f865f66
There was a problem hiding this comment.
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