-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(server): lazy routers #5489
Draft
KATT
wants to merge
20
commits into
next
Choose a base branch
from
issues/4129-lazy-routers
base: next
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+445
−74
Draft
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c754ee5
first stab
KATT 6f337e9
mkay
KATT b51b8e5
no only
KATT d02c35a
release tmp
KATT cafc1ff
mkay
KATT 94771a2
lazy load
KATT 3ee782d
fix lock
KATT 67b653f
tweak
KATT 40b889f
cool
KATT 61f63ef
more
KATT 101c0fa
coool
KATT 4e59b68
Merge remote-tracking branch 'origin/next' into issues/4129-lazy-routers
KATT b41338d
Merge branch 'next' into issues/4129-lazy-routers
kodiakhq[bot] f57c65d
Merge branch 'next' into issues/4129-lazy-routers
KATT cf8d1ab
Merge remote-tracking branch 'origin/next' into issues/4129-lazy-routers
KATT 6c1b8ab
chore: apply lint and formatting fixes
autofix-ci[bot] f184ec1
Merge remote-tracking branch 'origin/next' into issues/4129-lazy-routers
KATT b85ff52
cool
KATT 70ec374
Merge branch 'issues/4129-lazy-routers' of github.com:trpc/trpc into …
KATT ba5a1e4
mkay cool
KATT File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# A minimal working tRPC example | ||
|
||
Requires node 18 (for global fetch). | ||
|
||
## Playing around | ||
|
||
``` | ||
npm i | ||
npm run dev | ||
``` | ||
|
||
Try editing the ts files to see the type checking in action :) | ||
|
||
## Building | ||
|
||
``` | ||
npm run build | ||
npm run start | ||
``` |
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,26 @@ | ||
{ | ||
"name": "examples-lazy-load", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"build": "tsc", | ||
"dev:server": "tsx watch src/server", | ||
"dev:client": "wait-port 3000 && tsx watch src/client", | ||
"dev": "run-p dev:* --print-label", | ||
"test-dev": "start-server-and-test 'tsx src/server' 3000 'tsx src/client'", | ||
"test-start": "start-server-and-test 'node dist/server' 3000 'node dist/client'" | ||
}, | ||
"dependencies": { | ||
"@trpc/client": "npm:@trpc/client@next", | ||
"@trpc/server": "npm:@trpc/server@next", | ||
"zod": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.10.0", | ||
"npm-run-all": "^4.1.5", | ||
"start-server-and-test": "^1.12.0", | ||
"tsx": "^4.0.0", | ||
"typescript": "^5.4.0", | ||
"wait-port": "^1.0.1" | ||
} | ||
} |
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,36 @@ | ||
/** | ||
* This is the client-side code that uses the inferred types from the server | ||
*/ | ||
import { createTRPCClient, httpBatchLink } from '@trpc/client'; | ||
/** | ||
* We only import the `AppRouter` type from the server - this is not available at runtime | ||
*/ | ||
import type { AppRouter } from '../server/index.js'; | ||
|
||
// Initialize the tRPC client | ||
const trpc = createTRPCClient<AppRouter>({ | ||
links: [ | ||
httpBatchLink({ | ||
url: 'http://localhost:3000', | ||
}), | ||
], | ||
}); | ||
|
||
// Call procedure functions | ||
|
||
// 💡 Tip, try to: | ||
// - hover any types below to see the inferred types | ||
// - Cmd/Ctrl+click on any function to jump to the definition | ||
// - Rename any variable and see it reflected across both frontend and backend | ||
|
||
const users = await trpc.user.list.query(); | ||
// ^? | ||
console.log('Users:', users); | ||
|
||
const createdUser = await trpc.user.create.mutate({ name: 'sachinraja' }); | ||
// ^? | ||
console.log('Created user:', createdUser); | ||
|
||
const user = await trpc.user.byId.query('1'); | ||
// ^? | ||
console.log('User 1:', user); |
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,15 @@ | ||
type User = { id: string; name: string }; | ||
|
||
// Imaginary database | ||
const users: User[] = []; | ||
export const db = { | ||
user: { | ||
findMany: async () => users, | ||
findById: async (id: string) => users.find((user) => user.id === id), | ||
create: async (data: { name: string }) => { | ||
const user = { id: String(users.length + 1), ...data }; | ||
users.push(user); | ||
return user; | ||
}, | ||
}, | ||
}; |
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,25 @@ | ||
/** | ||
* This a minimal tRPC server | ||
*/ | ||
import { createHTTPServer } from '@trpc/server/adapters/standalone'; | ||
import { lazy } from '@trpc/server/unstable-core-do-not-import'; | ||
import { router } from './trpc.js'; | ||
|
||
const user = lazy(() => | ||
import('./routers/user.js').then((m) => { | ||
console.log('💤 lazy loaded user router'); | ||
return m.userRouter; | ||
}), | ||
); | ||
const appRouter = router({ | ||
user, | ||
}); | ||
|
||
// Export type router type signature, this is used by the client. | ||
export type AppRouter = typeof appRouter; | ||
|
||
const server = createHTTPServer({ | ||
router: appRouter, | ||
}); | ||
|
||
server.listen(3000); |
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,32 @@ | ||
/** | ||
* This a minimal tRPC server | ||
*/ | ||
import { z } from 'zod'; | ||
import { db } from '../db.js'; | ||
import { publicProcedure, router } from '../trpc.js'; | ||
|
||
export const userRouter = router({ | ||
list: publicProcedure.query(async () => { | ||
// Retrieve users from a datasource, this is an imaginary database | ||
const users = await db.user.findMany(); | ||
// ^? | ||
return users; | ||
}), | ||
byId: publicProcedure.input(z.string()).query(async (opts) => { | ||
const { input } = opts; | ||
// ^? | ||
// Retrieve the user with the given ID | ||
const user = await db.user.findById(input); | ||
return user; | ||
}), | ||
create: publicProcedure | ||
.input(z.object({ name: z.string() })) | ||
.mutation(async (opts) => { | ||
const { input } = opts; | ||
// ^? | ||
// Create a new user in the database | ||
const user = await db.user.create(input); | ||
// ^? | ||
return user; | ||
}), | ||
}); |
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,14 @@ | ||
import { initTRPC } from '@trpc/server'; | ||
|
||
/** | ||
* Initialization of tRPC backend | ||
* Should be done only once per backend! | ||
*/ | ||
const t = initTRPC.create(); | ||
|
||
/** | ||
* Export reusable router and procedure helpers | ||
* that can be used throughout the router | ||
*/ | ||
export const router = t.router; | ||
export const publicProcedure = t.procedure; |
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,12 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "esnext", | ||
"module": "NodeNext", | ||
"moduleResolution": "NodeNext", | ||
"esModuleInterop": true, | ||
"strict": true, | ||
"outDir": "dist", | ||
"skipLibCheck": true | ||
}, | ||
"include": ["src"] | ||
} |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
cc @zomars here's an example on how to use it