Skip to content
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

docs: add trpc to examples #38272

Merged
merged 7 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions examples/with-trpc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# local env files
.env*.local

# vercel
.vercel

# cache
*.swc/

# typescript
*.tsbuildinfo
23 changes: 23 additions & 0 deletions examples/with-trpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Next.js and TRPC

This example shows how you can make a typed query using a minimal implementation of TRPC following [`this as a reference`](https://trpc.io/docs/nextjs). You can get a more complete implementation with Prisma, E2E testing, & ESLint [`here`](https://github.com/trpc/examples-next-prisma-starter).

## Deploy your own

Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example):

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-trpc&project-name=with-trpc&repository-name=with-trpc)

## How to use

Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:

```bash
npx create-next-app --example with-trpc with-trpc-app
# or
yarn create next-app --example with-trpc with-trpc-app
# or
pnpm create next-app --example with-trpc with-trpc-app
```

Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
5 changes: 5 additions & 0 deletions examples/with-trpc/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
25 changes: 25 additions & 0 deletions examples/with-trpc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"@trpc/client": "^9.25.3",
"@trpc/next": "^9.25.3",
"@trpc/react": "^9.25.3",
"@trpc/server": "^9.25.3",
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-query": "^3.39.1",
"zod": "^3.17.3"
},
"devDependencies": {
"@types/node": "^18.0.0",
"@types/react": "^18.0.14",
"@types/react-dom": "^18.0.5",
"typescript": "^4.7.4"
}
}
16 changes: 16 additions & 0 deletions examples/with-trpc/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { withTRPC } from "@trpc/next";
import { AppType } from "next/dist/shared/lib/utils";
import { AppRouter } from "./api/trpc/[trpc]";

const MyApp: AppType = ({ Component, pageProps }) => {
return <Component {...pageProps} />;
};

export default withTRPC<AppRouter>({
config({ ctx }) {
const url = "http://localhost:3000/api/trpc";
return {
url,
};
},
})(MyApp);
25 changes: 25 additions & 0 deletions examples/with-trpc/src/pages/api/trpc/[trpc].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as trpc from '@trpc/server'
import * as trpcNext from '@trpc/server/adapters/next'
import { z } from 'zod'

export const appRouter = trpc.router().query('hello', {
input: z
.object({
text: z.string().nullish(),
})
.nullish(),
resolve({ input }) {
return {
greeting: `hello ${input?.text ?? 'world'}`,
}
},
})

// export type definition of API
export type AppRouter = typeof appRouter

// export API handler
export default trpcNext.createNextApiHandler({
router: appRouter,
createContext: () => null,
})
26 changes: 26 additions & 0 deletions examples/with-trpc/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { trpc } from "../utils/trpc";

export default function IndexPage() {
const hello = trpc.useQuery(["hello", { text: "client" }]);
if (!hello.data) {
return (
<div style={styles}>
<h1>Loading...</h1>
</div>
);
}
return (
<div style={styles}>
{/* the type is define, it can be autocompleted */}
<h1>{hello.data.greeting}</h1>
</div>
);
}

const styles = {
width: "100vw",
height: "100vh",
display: "flex",
justifyContent: "center",
alignItems: "center",
};
5 changes: 5 additions & 0 deletions examples/with-trpc/src/utils/trpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createReactQueryHooks } from '@trpc/react'
import type { AppRouter } from '../pages/api/trpc/[trpc]'

export const trpc = createReactQueryHooks<AppRouter>()
// => { useQuery: ..., useMutation: ...}
20 changes: 20 additions & 0 deletions examples/with-trpc/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}