@idealyst/cli 1.1.9 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/add.js +233 -0
- package/dist/commands/add.js.map +1 -0
- package/dist/commands/index.js +13 -0
- package/dist/commands/index.js.map +1 -0
- package/dist/commands/info.js +193 -0
- package/dist/commands/info.js.map +1 -0
- package/dist/commands/init.js +155 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/constants.js +119 -0
- package/dist/constants.js.map +1 -0
- package/dist/generators/core/index.js +23 -0
- package/dist/generators/core/index.js.map +1 -0
- package/dist/generators/core/mobile.js +286 -0
- package/dist/generators/core/mobile.js.map +1 -0
- package/dist/generators/core/shared.js +207 -0
- package/dist/generators/core/shared.js.map +1 -0
- package/dist/generators/core/web.js +377 -0
- package/dist/generators/core/web.js.map +1 -0
- package/dist/generators/extensions/api.js +223 -0
- package/dist/generators/extensions/api.js.map +1 -0
- package/dist/generators/extensions/devcontainer.js +510 -0
- package/dist/generators/extensions/devcontainer.js.map +1 -0
- package/dist/generators/extensions/graphql.js +202 -0
- package/dist/generators/extensions/graphql.js.map +1 -0
- package/dist/generators/extensions/index.js +17 -0
- package/dist/generators/extensions/index.js.map +1 -0
- package/dist/generators/extensions/prisma.js +249 -0
- package/dist/generators/extensions/prisma.js.map +1 -0
- package/dist/generators/extensions/trpc.js +258 -0
- package/dist/generators/extensions/trpc.js.map +1 -0
- package/dist/generators/index.js +106 -4
- package/dist/generators/index.js.map +1 -1
- package/dist/generators/reactNative.js +177 -0
- package/dist/generators/reactNative.js.map +1 -0
- package/dist/generators/workspace.js +272 -0
- package/dist/generators/workspace.js.map +1 -0
- package/dist/identifiers/android.js +75 -0
- package/dist/identifiers/android.js.map +1 -0
- package/dist/identifiers/index.js +35 -0
- package/dist/identifiers/index.js.map +1 -0
- package/dist/identifiers/ios.js +65 -0
- package/dist/identifiers/ios.js.map +1 -0
- package/dist/index.js +41 -319
- package/dist/index.js.map +1 -1
- package/dist/templates/copier.js +135 -0
- package/dist/templates/copier.js.map +1 -0
- package/dist/templates/index.js +23 -0
- package/dist/templates/index.js.map +1 -0
- package/dist/templates/merger.js +113 -0
- package/dist/templates/merger.js.map +1 -0
- package/dist/templates/processor.js +142 -0
- package/dist/templates/processor.js.map +1 -0
- package/dist/types/commands/add.d.ts +14 -0
- package/dist/types/commands/index.d.ts +6 -0
- package/dist/types/commands/info.d.ts +10 -0
- package/dist/types/commands/init.d.ts +19 -0
- package/dist/types/constants.d.ts +76 -0
- package/dist/types/generators/core/index.d.ts +6 -0
- package/dist/types/generators/core/mobile.d.ts +13 -0
- package/dist/types/generators/core/shared.d.ts +8 -0
- package/dist/types/generators/core/web.d.ts +8 -0
- package/dist/types/generators/extensions/api.d.ts +8 -0
- package/dist/types/generators/extensions/devcontainer.d.ts +15 -0
- package/dist/types/generators/extensions/graphql.d.ts +8 -0
- package/dist/types/generators/extensions/index.d.ts +8 -0
- package/dist/types/generators/extensions/prisma.d.ts +8 -0
- package/dist/types/generators/extensions/trpc.d.ts +9 -0
- package/dist/types/generators/index.d.ts +11 -3
- package/dist/types/generators/reactNative.d.ts +19 -0
- package/dist/types/generators/workspace.d.ts +8 -0
- package/dist/types/identifiers/android.d.ts +26 -0
- package/dist/types/identifiers/index.d.ts +26 -0
- package/dist/types/identifiers/ios.d.ts +21 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/templates/copier.d.ts +24 -0
- package/dist/types/templates/index.d.ts +6 -0
- package/dist/types/templates/merger.d.ts +43 -0
- package/dist/types/templates/processor.d.ts +39 -0
- package/dist/types/types.d.ts +153 -5
- package/dist/types/utils/filesystem.d.ts +56 -0
- package/dist/types/utils/index.d.ts +7 -0
- package/dist/types/utils/logger.d.ts +58 -0
- package/dist/types/utils/shell.d.ts +35 -0
- package/dist/types/utils/validation.d.ts +32 -0
- package/dist/types/wizard/index.d.ts +20 -0
- package/dist/types/wizard/steps/apiExtension.d.ts +5 -0
- package/dist/types/wizard/steps/appDisplayName.d.ts +5 -0
- package/dist/types/wizard/steps/devcontainerExtension.d.ts +5 -0
- package/dist/types/wizard/steps/graphqlExtension.d.ts +5 -0
- package/dist/types/wizard/steps/index.d.ts +12 -0
- package/dist/types/wizard/steps/orgDomain.d.ts +5 -0
- package/dist/types/wizard/steps/prismaExtension.d.ts +5 -0
- package/dist/types/wizard/steps/projectName.d.ts +5 -0
- package/dist/types/wizard/steps/summary.d.ts +5 -0
- package/dist/types/wizard/steps/trpcExtension.d.ts +5 -0
- package/dist/types/wizard/validators.d.ts +17 -0
- package/dist/types.js +41 -0
- package/dist/types.js.map +1 -1
- package/dist/utils/filesystem.js +173 -0
- package/dist/utils/filesystem.js.map +1 -0
- package/dist/utils/index.js +24 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/logger.js +95 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/shell.js +113 -0
- package/dist/utils/shell.js.map +1 -0
- package/dist/utils/validation.js +196 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/wizard/index.js +177 -0
- package/dist/wizard/index.js.map +1 -0
- package/dist/wizard/steps/apiExtension.js +33 -0
- package/dist/wizard/steps/apiExtension.js.map +1 -0
- package/dist/wizard/steps/appDisplayName.js +53 -0
- package/dist/wizard/steps/appDisplayName.js.map +1 -0
- package/dist/wizard/steps/devcontainerExtension.js +78 -0
- package/dist/wizard/steps/devcontainerExtension.js.map +1 -0
- package/dist/wizard/steps/graphqlExtension.js +37 -0
- package/dist/wizard/steps/graphqlExtension.js.map +1 -0
- package/dist/wizard/steps/index.js +25 -0
- package/dist/wizard/steps/index.js.map +1 -0
- package/dist/wizard/steps/orgDomain.js +40 -0
- package/dist/wizard/steps/orgDomain.js.map +1 -0
- package/dist/wizard/steps/prismaExtension.js +33 -0
- package/dist/wizard/steps/prismaExtension.js.map +1 -0
- package/dist/wizard/steps/projectName.js +40 -0
- package/dist/wizard/steps/projectName.js.map +1 -0
- package/dist/wizard/steps/summary.js +107 -0
- package/dist/wizard/steps/summary.js.map +1 -0
- package/dist/wizard/steps/trpcExtension.js +37 -0
- package/dist/wizard/steps/trpcExtension.js.map +1 -0
- package/dist/wizard/validators.js +35 -0
- package/dist/wizard/validators.js.map +1 -0
- package/package.json +6 -19
- package/README.md +0 -110
- package/dist/generators/init.js +0 -268
- package/dist/generators/init.js.map +0 -1
- package/dist/generators/utils.js +0 -682
- package/dist/generators/utils.js.map +0 -1
- package/dist/scripts/configure-react-native-monorepo.js +0 -144
- package/dist/scripts/configure-react-native-monorepo.js.map +0 -1
- package/dist/template/.devcontainer/Dockerfile +0 -26
- package/dist/template/.devcontainer/devcontainer.json +0 -113
- package/dist/template/.devcontainer/docker-compose.yml +0 -59
- package/dist/template/.devcontainer/figma-mcp.sh +0 -32
- package/dist/template/.devcontainer/gitignore.template +0 -2
- package/dist/template/.devcontainer/setup.sh +0 -45
- package/dist/template/DOCKER.md +0 -0
- package/dist/template/Dockerfile +0 -111
- package/dist/template/README.md +0 -233
- package/dist/template/docker/nginx/prod.conf +0 -238
- package/dist/template/docker/nginx.conf +0 -131
- package/dist/template/docker/postgres/init.sql +0 -41
- package/dist/template/docker/prometheus/prometheus.yml +0 -52
- package/dist/template/docker-compose.prod.yml +0 -146
- package/dist/template/docker-compose.yml +0 -143
- package/dist/template/dockerignore.template +0 -151
- package/dist/template/env.example.template +0 -36
- package/dist/template/gitignore.template +0 -56
- package/dist/template/jest.config.js +0 -20
- package/dist/template/mcp.json.template +0 -8
- package/dist/template/package.json +0 -45
- package/dist/template/packages/api/README.md +0 -510
- package/dist/template/packages/api/__tests__/api.test.ts +0 -26
- package/dist/template/packages/api/env.example.template +0 -6
- package/dist/template/packages/api/gitignore.template +0 -35
- package/dist/template/packages/api/jest.config.js +0 -23
- package/dist/template/packages/api/jest.setup.js +0 -9
- package/dist/template/packages/api/package.json +0 -66
- package/dist/template/packages/api/src/context.ts +0 -52
- package/dist/template/packages/api/src/controllers/TestController.ts +0 -0
- package/dist/template/packages/api/src/graphql/builder.ts +0 -75
- package/dist/template/packages/api/src/graphql/generated.ts +0 -64
- package/dist/template/packages/api/src/graphql/index.ts +0 -75
- package/dist/template/packages/api/src/graphql/types/index.ts +0 -44
- package/dist/template/packages/api/src/graphql/types/test.ts +0 -245
- package/dist/template/packages/api/src/index.ts +0 -26
- package/dist/template/packages/api/src/lib/database.ts +0 -23
- package/dist/template/packages/api/src/router/index.ts +0 -163
- package/dist/template/packages/api/src/routers/test.ts +0 -161
- package/dist/template/packages/api/src/server.ts +0 -68
- package/dist/template/packages/api/src/trpc.ts +0 -28
- package/dist/template/packages/api/tsconfig.json +0 -44
- package/dist/template/packages/database/README.md +0 -162
- package/dist/template/packages/database/gitignore.template +0 -41
- package/dist/template/packages/database/package.json +0 -49
- package/dist/template/packages/database/prisma/seed.ts +0 -64
- package/dist/template/packages/database/schema.prisma +0 -107
- package/dist/template/packages/database/src/index.ts +0 -15
- package/dist/template/packages/database/src/validators.ts +0 -10
- package/dist/template/packages/database/tsconfig.json +0 -18
- package/dist/template/packages/mobile/README.md +0 -86
- package/dist/template/packages/mobile/__tests__/App.test.tsx +0 -156
- package/dist/template/packages/mobile/__tests__/components.test.tsx +0 -300
- package/dist/template/packages/mobile/app.json +0 -5
- package/dist/template/packages/mobile/babel.config.js +0 -10
- package/dist/template/packages/mobile/gitignore.template +0 -73
- package/dist/template/packages/mobile/index.js +0 -6
- package/dist/template/packages/mobile/jest.config.js +0 -21
- package/dist/template/packages/mobile/jest.setup.js +0 -12
- package/dist/template/packages/mobile/metro.config.js +0 -36
- package/dist/template/packages/mobile/package.json +0 -60
- package/dist/template/packages/mobile/src/App.tsx +0 -8
- package/dist/template/packages/mobile/src/utils/trpc.ts +0 -7
- package/dist/template/packages/mobile/tsconfig.json +0 -28
- package/dist/template/packages/shared/README.md +0 -135
- package/dist/template/packages/shared/__tests__/shared.test.ts +0 -51
- package/dist/template/packages/shared/gitignore.template +0 -35
- package/dist/template/packages/shared/jest.config.js +0 -22
- package/dist/template/packages/shared/package.json +0 -68
- package/dist/template/packages/shared/src/components/App.tsx +0 -57
- package/dist/template/packages/shared/src/components/HelloWorld.tsx +0 -531
- package/dist/template/packages/shared/src/components/index.ts +0 -1
- package/dist/template/packages/shared/src/graphql/client.ts +0 -34
- package/dist/template/packages/shared/src/index.ts +0 -22
- package/dist/template/packages/shared/src/navigation/AppRouter.tsx +0 -565
- package/dist/template/packages/shared/src/trpc/client.ts +0 -44
- package/dist/template/packages/shared/tsconfig.json +0 -22
- package/dist/template/packages/web/README.md +0 -131
- package/dist/template/packages/web/__tests__/App.test.tsx +0 -342
- package/dist/template/packages/web/__tests__/components.test.tsx +0 -564
- package/dist/template/packages/web/gitignore.template +0 -35
- package/dist/template/packages/web/index.html +0 -13
- package/dist/template/packages/web/jest.config.js +0 -27
- package/dist/template/packages/web/jest.setup.js +0 -24
- package/dist/template/packages/web/package.json +0 -69
- package/dist/template/packages/web/src/App.tsx +0 -14
- package/dist/template/packages/web/src/components/TestDemo.tsx +0 -164
- package/dist/template/packages/web/src/main.tsx +0 -25
- package/dist/template/packages/web/src/utils/trpc.ts +0 -7
- package/dist/template/packages/web/tsconfig.json +0 -26
- package/dist/template/packages/web/vite.config.ts +0 -98
- package/dist/template/setup.sh +0 -30
- package/dist/template/tsconfig.json +0 -22
- package/dist/template/yarnrc.yml.template +0 -4
- package/dist/types/generators/init.d.ts +0 -5
- package/dist/types/generators/utils.d.ts +0 -45
- package/dist/types/scripts/configure-react-native-monorepo.d.ts +0 -23
- package/template/.devcontainer/Dockerfile +0 -26
- package/template/.devcontainer/devcontainer.json +0 -113
- package/template/.devcontainer/docker-compose.yml +0 -59
- package/template/.devcontainer/figma-mcp.sh +0 -32
- package/template/.devcontainer/setup.sh +0 -45
- package/template/.dockerignore +0 -151
- package/template/.env.example +0 -36
- package/template/.env.production +0 -56
- package/template/DOCKER.md +0 -0
- package/template/Dockerfile +0 -111
- package/template/README.md +0 -233
- package/template/docker/nginx/prod.conf +0 -238
- package/template/docker/nginx.conf +0 -131
- package/template/docker/postgres/init.sql +0 -41
- package/template/docker/prometheus/prometheus.yml +0 -52
- package/template/docker-compose.prod.yml +0 -146
- package/template/docker-compose.yml +0 -143
- package/template/jest.config.js +0 -20
- package/template/package.json +0 -45
- package/template/packages/api/.env.example +0 -6
- package/template/packages/api/README.md +0 -510
- package/template/packages/api/__tests__/api.test.ts +0 -26
- package/template/packages/api/jest.config.js +0 -23
- package/template/packages/api/jest.setup.js +0 -9
- package/template/packages/api/package.json +0 -66
- package/template/packages/api/src/context.ts +0 -52
- package/template/packages/api/src/controllers/TestController.ts +0 -0
- package/template/packages/api/src/graphql/builder.ts +0 -75
- package/template/packages/api/src/graphql/generated.ts +0 -64
- package/template/packages/api/src/graphql/index.ts +0 -75
- package/template/packages/api/src/graphql/types/index.ts +0 -44
- package/template/packages/api/src/graphql/types/test.ts +0 -245
- package/template/packages/api/src/index.ts +0 -26
- package/template/packages/api/src/lib/database.ts +0 -23
- package/template/packages/api/src/router/index.ts +0 -163
- package/template/packages/api/src/routers/test.ts +0 -161
- package/template/packages/api/src/server.ts +0 -68
- package/template/packages/api/src/trpc.ts +0 -28
- package/template/packages/api/tsconfig.json +0 -44
- package/template/packages/database/README.md +0 -162
- package/template/packages/database/package.json +0 -49
- package/template/packages/database/prisma/seed.ts +0 -64
- package/template/packages/database/schema.prisma +0 -107
- package/template/packages/database/src/index.ts +0 -15
- package/template/packages/database/src/validators.ts +0 -10
- package/template/packages/database/tsconfig.json +0 -18
- package/template/packages/mobile/README.md +0 -86
- package/template/packages/mobile/__tests__/App.test.tsx +0 -156
- package/template/packages/mobile/__tests__/components.test.tsx +0 -300
- package/template/packages/mobile/app.json +0 -5
- package/template/packages/mobile/babel.config.js +0 -26
- package/template/packages/mobile/index.js +0 -8
- package/template/packages/mobile/jest.config.js +0 -21
- package/template/packages/mobile/jest.setup.js +0 -12
- package/template/packages/mobile/metro.config.js +0 -36
- package/template/packages/mobile/package.json +0 -60
- package/template/packages/mobile/src/App.tsx +0 -8
- package/template/packages/mobile/src/utils/trpc.ts +0 -7
- package/template/packages/mobile/tsconfig.json +0 -28
- package/template/packages/shared/README.md +0 -135
- package/template/packages/shared/__tests__/shared.test.ts +0 -51
- package/template/packages/shared/jest.config.js +0 -22
- package/template/packages/shared/package.json +0 -68
- package/template/packages/shared/src/components/App.tsx +0 -57
- package/template/packages/shared/src/components/HelloWorld.tsx +0 -531
- package/template/packages/shared/src/components/index.ts +0 -1
- package/template/packages/shared/src/graphql/client.ts +0 -34
- package/template/packages/shared/src/index.ts +0 -25
- package/template/packages/shared/src/navigation/AppRouter.tsx +0 -565
- package/template/packages/shared/src/trpc/client.ts +0 -44
- package/template/packages/shared/src/unistyles.ts +0 -50
- package/template/packages/shared/tsconfig.json +0 -22
- package/template/packages/web/README.md +0 -131
- package/template/packages/web/__tests__/App.test.tsx +0 -342
- package/template/packages/web/__tests__/components.test.tsx +0 -564
- package/template/packages/web/index.html +0 -13
- package/template/packages/web/jest.config.js +0 -27
- package/template/packages/web/jest.setup.js +0 -24
- package/template/packages/web/package.json +0 -69
- package/template/packages/web/src/App.tsx +0 -14
- package/template/packages/web/src/components/TestDemo.tsx +0 -164
- package/template/packages/web/src/main.tsx +0 -27
- package/template/packages/web/src/utils/trpc.ts +0 -7
- package/template/packages/web/tsconfig.json +0 -26
- package/template/packages/web/vite.config.ts +0 -114
- package/template/setup.sh +0 -30
- package/template/tsconfig.json +0 -22
|
@@ -1,510 +0,0 @@
|
|
|
1
|
-
# {{projectName}} API
|
|
2
|
-
|
|
3
|
-
{{description}}
|
|
4
|
-
|
|
5
|
-
A dual-API backend with both **tRPC** (for internal TypeScript clients) and **GraphQL** (for public APIs and mobile apps).
|
|
6
|
-
|
|
7
|
-
## Tech Stack
|
|
8
|
-
|
|
9
|
-
- **tRPC** - End-to-end type-safe APIs for TypeScript clients
|
|
10
|
-
- **GraphQL + Pothos** - Code-first GraphQL schema with Prisma integration
|
|
11
|
-
- **GraphQL Yoga** - Modern, performant GraphQL server
|
|
12
|
-
- **Zod** - TypeScript-first schema validation
|
|
13
|
-
- **Express.js** - Web framework for Node.js
|
|
14
|
-
- **Prisma** - Next-generation ORM for database access
|
|
15
|
-
- **TypeScript** - Type-safe JavaScript
|
|
16
|
-
|
|
17
|
-
## API Architecture
|
|
18
|
-
|
|
19
|
-
```
|
|
20
|
-
┌─────────────────────────────────────────────────┐
|
|
21
|
-
│ Single Express Server │
|
|
22
|
-
│ (port 3000) │
|
|
23
|
-
├─────────────────────────────────────────────────┤
|
|
24
|
-
│ │
|
|
25
|
-
│ GET /health → Health check │
|
|
26
|
-
│ POST /trpc/* → tRPC procedures │
|
|
27
|
-
│ POST /graphql → GraphQL queries │
|
|
28
|
-
│ GET /graphql → GraphiQL Playground │
|
|
29
|
-
│ │
|
|
30
|
-
└─────────────────────────────────────────────────┘
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
### When to Use Each
|
|
34
|
-
|
|
35
|
-
| Use Case | Recommended API |
|
|
36
|
-
|----------|----------------|
|
|
37
|
-
| Internal web app (TypeScript) | tRPC |
|
|
38
|
-
| Mobile apps (React Native, iOS, Android) | GraphQL |
|
|
39
|
-
| Third-party integrations | GraphQL |
|
|
40
|
-
| Admin dashboards | tRPC |
|
|
41
|
-
| Auth flows (login, register) | tRPC |
|
|
42
|
-
| Public data queries | GraphQL |
|
|
43
|
-
| File uploads | tRPC |
|
|
44
|
-
|
|
45
|
-
## Quick Start
|
|
46
|
-
|
|
47
|
-
1. **Setup environment variables:**
|
|
48
|
-
```bash
|
|
49
|
-
cp .env.example .env
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
2. **Install dependencies:**
|
|
53
|
-
```bash
|
|
54
|
-
yarn install
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
3. **Setup database:**
|
|
58
|
-
```bash
|
|
59
|
-
# Generate Prisma client and Pothos types
|
|
60
|
-
yarn generate
|
|
61
|
-
|
|
62
|
-
# Run migrations
|
|
63
|
-
cd ../database && yarn db:migrate
|
|
64
|
-
|
|
65
|
-
# Seed database with sample data
|
|
66
|
-
yarn db:seed
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
4. **Start development server:**
|
|
70
|
-
```bash
|
|
71
|
-
yarn dev
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
The API will be available at:
|
|
75
|
-
- **tRPC**: `http://localhost:3000/trpc`
|
|
76
|
-
- **GraphQL**: `http://localhost:3000/graphql`
|
|
77
|
-
- **GraphiQL Playground**: `http://localhost:3000/graphql` (in development)
|
|
78
|
-
|
|
79
|
-
## Test API - tRPC vs GraphQL Comparison
|
|
80
|
-
|
|
81
|
-
The template includes a `Test` model with equivalent implementations in both tRPC and GraphQL, demonstrating how the same functionality can be exposed through both APIs.
|
|
82
|
-
|
|
83
|
-
### API Equivalence Table
|
|
84
|
-
|
|
85
|
-
| Operation | tRPC | GraphQL |
|
|
86
|
-
|-----------|------|---------|
|
|
87
|
-
| Get all | `trpc.test.getAll({ skip, take })` | `query { tests(skip, take) { ... } }` |
|
|
88
|
-
| Get by ID | `trpc.test.getById({ id })` | `query { test(id) { ... } }` |
|
|
89
|
-
| Create | `trpc.test.create({ name, message })` | `mutation { createTest(input) { ... } }` |
|
|
90
|
-
| Update | `trpc.test.update({ id, data })` | `mutation { updateTest(id, input) { ... } }` |
|
|
91
|
-
| Delete | `trpc.test.delete({ id })` | `mutation { deleteTest(id) { ... } }` |
|
|
92
|
-
| Count | `trpc.test.count({})` | `query { testCount }` |
|
|
93
|
-
|
|
94
|
-
### tRPC Usage (TypeScript Client)
|
|
95
|
-
|
|
96
|
-
```typescript
|
|
97
|
-
import { trpc } from './utils/trpc';
|
|
98
|
-
|
|
99
|
-
// Get all tests
|
|
100
|
-
const { data: tests } = trpc.test.getAll.useQuery({ take: 10 });
|
|
101
|
-
|
|
102
|
-
// Get test by ID
|
|
103
|
-
const { data: test } = trpc.test.getById.useQuery({ id: 'test-id' });
|
|
104
|
-
|
|
105
|
-
// Create a new test
|
|
106
|
-
const createTest = trpc.test.create.useMutation();
|
|
107
|
-
await createTest.mutateAsync({
|
|
108
|
-
name: 'My Test',
|
|
109
|
-
message: 'Hello World',
|
|
110
|
-
status: 'active',
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
// Update a test
|
|
114
|
-
const updateTest = trpc.test.update.useMutation();
|
|
115
|
-
await updateTest.mutateAsync({
|
|
116
|
-
id: 'test-id',
|
|
117
|
-
data: { name: 'Updated Name' },
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
// Delete a test
|
|
121
|
-
const deleteTest = trpc.test.delete.useMutation();
|
|
122
|
-
await deleteTest.mutateAsync({ id: 'test-id' });
|
|
123
|
-
|
|
124
|
-
// Get count
|
|
125
|
-
const { data: count } = trpc.test.count.useQuery({});
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
### GraphQL Usage
|
|
129
|
-
|
|
130
|
-
Visit `http://localhost:3000/graphql` to access the GraphiQL playground.
|
|
131
|
-
|
|
132
|
-
```graphql
|
|
133
|
-
# Get all tests (equivalent to trpc.test.getAll)
|
|
134
|
-
query GetTests {
|
|
135
|
-
tests(take: 10) {
|
|
136
|
-
id
|
|
137
|
-
name
|
|
138
|
-
message
|
|
139
|
-
status
|
|
140
|
-
createdAt
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
# Get test by ID (equivalent to trpc.test.getById)
|
|
145
|
-
query GetTest($id: String!) {
|
|
146
|
-
test(id: $id) {
|
|
147
|
-
id
|
|
148
|
-
name
|
|
149
|
-
message
|
|
150
|
-
status
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
# Create a new test (equivalent to trpc.test.create)
|
|
155
|
-
mutation CreateTest {
|
|
156
|
-
createTest(input: {
|
|
157
|
-
name: "My Test"
|
|
158
|
-
message: "Hello World"
|
|
159
|
-
status: "active"
|
|
160
|
-
}) {
|
|
161
|
-
id
|
|
162
|
-
name
|
|
163
|
-
message
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
# Update a test (equivalent to trpc.test.update)
|
|
168
|
-
mutation UpdateTest($id: String!) {
|
|
169
|
-
updateTest(id: $id, input: { name: "Updated Name" }) {
|
|
170
|
-
id
|
|
171
|
-
name
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
# Delete a test (equivalent to trpc.test.delete)
|
|
176
|
-
mutation DeleteTest($id: String!) {
|
|
177
|
-
deleteTest(id: $id) {
|
|
178
|
-
id
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
# Get count (equivalent to trpc.test.count)
|
|
183
|
-
query GetTestCount {
|
|
184
|
-
testCount
|
|
185
|
-
}
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
### GraphQL with React/Apollo Client
|
|
189
|
-
|
|
190
|
-
```typescript
|
|
191
|
-
import { useQuery, useMutation, gql } from '@apollo/client';
|
|
192
|
-
|
|
193
|
-
const GET_TESTS = gql`
|
|
194
|
-
query GetTests($take: Int) {
|
|
195
|
-
tests(take: $take) {
|
|
196
|
-
id
|
|
197
|
-
name
|
|
198
|
-
message
|
|
199
|
-
status
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
`;
|
|
203
|
-
|
|
204
|
-
const CREATE_TEST = gql`
|
|
205
|
-
mutation CreateTest($input: CreateTestInput!) {
|
|
206
|
-
createTest(input: $input) {
|
|
207
|
-
id
|
|
208
|
-
name
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
`;
|
|
212
|
-
|
|
213
|
-
// In your component
|
|
214
|
-
function TestList() {
|
|
215
|
-
const { data, loading } = useQuery(GET_TESTS, { variables: { take: 10 } });
|
|
216
|
-
const [createTest] = useMutation(CREATE_TEST);
|
|
217
|
-
|
|
218
|
-
const handleCreate = async () => {
|
|
219
|
-
await createTest({
|
|
220
|
-
variables: {
|
|
221
|
-
input: { name: 'New Test', message: 'Hello!' },
|
|
222
|
-
},
|
|
223
|
-
});
|
|
224
|
-
};
|
|
225
|
-
|
|
226
|
-
return (/* ... */);
|
|
227
|
-
}
|
|
228
|
-
```
|
|
229
|
-
|
|
230
|
-
## Adding New Models
|
|
231
|
-
|
|
232
|
-
### Step 1: Define Prisma Model
|
|
233
|
-
|
|
234
|
-
Add to `packages/database/prisma/schema.prisma`:
|
|
235
|
-
|
|
236
|
-
```prisma
|
|
237
|
-
model Product {
|
|
238
|
-
id String @id @default(cuid())
|
|
239
|
-
name String
|
|
240
|
-
price Float
|
|
241
|
-
createdAt DateTime @default(now())
|
|
242
|
-
updatedAt DateTime @updatedAt
|
|
243
|
-
}
|
|
244
|
-
```
|
|
245
|
-
|
|
246
|
-
### Step 2: Create tRPC Router
|
|
247
|
-
|
|
248
|
-
Create `src/routers/product.ts`:
|
|
249
|
-
|
|
250
|
-
```typescript
|
|
251
|
-
import { z } from 'zod';
|
|
252
|
-
import { createCrudRouter } from '../lib/crud.js';
|
|
253
|
-
|
|
254
|
-
const createProductSchema = z.object({
|
|
255
|
-
name: z.string().min(1),
|
|
256
|
-
price: z.number().positive(),
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
export const productRouter = createCrudRouter('Product', createProductSchema);
|
|
260
|
-
```
|
|
261
|
-
|
|
262
|
-
Add to `src/router/index.ts`:
|
|
263
|
-
|
|
264
|
-
```typescript
|
|
265
|
-
import { productRouter } from '../routers/product.js';
|
|
266
|
-
|
|
267
|
-
export const appRouter = router({
|
|
268
|
-
test: testRouter,
|
|
269
|
-
product: productRouter, // Add here
|
|
270
|
-
});
|
|
271
|
-
```
|
|
272
|
-
|
|
273
|
-
### Step 3: Create GraphQL Type
|
|
274
|
-
|
|
275
|
-
Create `src/graphql/types/product.ts`:
|
|
276
|
-
|
|
277
|
-
```typescript
|
|
278
|
-
import { builder } from '../builder.js';
|
|
279
|
-
import { prisma } from '../../lib/database.js';
|
|
280
|
-
|
|
281
|
-
builder.prismaObject('Product', {
|
|
282
|
-
fields: (t) => ({
|
|
283
|
-
id: t.exposeID('id'),
|
|
284
|
-
name: t.exposeString('name'),
|
|
285
|
-
price: t.exposeFloat('price'),
|
|
286
|
-
createdAt: t.expose('createdAt', { type: 'DateTime' }),
|
|
287
|
-
}),
|
|
288
|
-
});
|
|
289
|
-
|
|
290
|
-
builder.queryFields((t) => ({
|
|
291
|
-
products: t.prismaField({
|
|
292
|
-
type: ['Product'],
|
|
293
|
-
args: {
|
|
294
|
-
skip: t.arg.int(),
|
|
295
|
-
take: t.arg.int(),
|
|
296
|
-
},
|
|
297
|
-
resolve: async (query, _root, args) => {
|
|
298
|
-
return prisma.product.findMany({
|
|
299
|
-
...query,
|
|
300
|
-
skip: args.skip ?? undefined,
|
|
301
|
-
take: args.take ?? 10,
|
|
302
|
-
});
|
|
303
|
-
},
|
|
304
|
-
}),
|
|
305
|
-
}));
|
|
306
|
-
|
|
307
|
-
const CreateProductInput = builder.inputType('CreateProductInput', {
|
|
308
|
-
fields: (t) => ({
|
|
309
|
-
name: t.string({ required: true }),
|
|
310
|
-
price: t.float({ required: true }),
|
|
311
|
-
}),
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
builder.mutationFields((t) => ({
|
|
315
|
-
createProduct: t.prismaField({
|
|
316
|
-
type: 'Product',
|
|
317
|
-
args: {
|
|
318
|
-
input: t.arg({ type: CreateProductInput, required: true }),
|
|
319
|
-
},
|
|
320
|
-
resolve: async (query, _root, args) => {
|
|
321
|
-
return prisma.product.create({
|
|
322
|
-
...query,
|
|
323
|
-
data: args.input,
|
|
324
|
-
});
|
|
325
|
-
},
|
|
326
|
-
}),
|
|
327
|
-
}));
|
|
328
|
-
```
|
|
329
|
-
|
|
330
|
-
Register in `src/graphql/types/index.ts`:
|
|
331
|
-
|
|
332
|
-
```typescript
|
|
333
|
-
import './product.js';
|
|
334
|
-
```
|
|
335
|
-
|
|
336
|
-
### Step 4: Regenerate Types
|
|
337
|
-
|
|
338
|
-
```bash
|
|
339
|
-
yarn generate
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
## Project Structure
|
|
343
|
-
|
|
344
|
-
```
|
|
345
|
-
src/
|
|
346
|
-
├── graphql/
|
|
347
|
-
│ ├── builder.ts # Pothos schema builder setup
|
|
348
|
-
│ ├── generated.ts # Auto-generated Prisma types
|
|
349
|
-
│ ├── index.ts # GraphQL Yoga server
|
|
350
|
-
│ └── types/
|
|
351
|
-
│ ├── index.ts # Type registration
|
|
352
|
-
│ ├── test.ts # Test model (active)
|
|
353
|
-
│ ├── user.ts # User model (example)
|
|
354
|
-
│ ├── post.ts # Post model (example)
|
|
355
|
-
│ └── comment.ts # Comment model (example)
|
|
356
|
-
├── router/
|
|
357
|
-
│ └── index.ts # Main tRPC router
|
|
358
|
-
├── routers/
|
|
359
|
-
│ ├── test.ts # Test tRPC CRUD router
|
|
360
|
-
│ └── user.example.ts # User tRPC example
|
|
361
|
-
├── lib/
|
|
362
|
-
│ ├── crud.ts # tRPC CRUD generator
|
|
363
|
-
│ └── database.ts # Prisma client
|
|
364
|
-
├── context.ts # Shared context
|
|
365
|
-
├── trpc.ts # tRPC setup
|
|
366
|
-
├── index.ts # Exports
|
|
367
|
-
└── server.ts # Express server
|
|
368
|
-
```
|
|
369
|
-
|
|
370
|
-
## Available Scripts
|
|
371
|
-
|
|
372
|
-
| Script | Description |
|
|
373
|
-
|--------|-------------|
|
|
374
|
-
| `yarn dev` | Start development server with hot reload |
|
|
375
|
-
| `yarn build` | Build production bundle |
|
|
376
|
-
| `yarn start` | Start production server |
|
|
377
|
-
| `yarn generate` | Generate Prisma client and Pothos types |
|
|
378
|
-
| `yarn test` | Run tests |
|
|
379
|
-
| `yarn lint` | Lint code |
|
|
380
|
-
| `yarn type-check` | Check TypeScript types |
|
|
381
|
-
|
|
382
|
-
## Environment Variables
|
|
383
|
-
|
|
384
|
-
```env
|
|
385
|
-
# Database
|
|
386
|
-
DATABASE_URL="postgresql://user:password@localhost:5432/mydb"
|
|
387
|
-
|
|
388
|
-
# API
|
|
389
|
-
PORT=3000
|
|
390
|
-
NODE_ENV=development
|
|
391
|
-
CORS_ORIGIN=http://localhost:5173
|
|
392
|
-
```
|
|
393
|
-
|
|
394
|
-
## Testing
|
|
395
|
-
|
|
396
|
-
### Testing tRPC
|
|
397
|
-
|
|
398
|
-
```typescript
|
|
399
|
-
import { createContext } from '../src/context.js';
|
|
400
|
-
import { appRouter } from '../src/router/index.js';
|
|
401
|
-
|
|
402
|
-
describe('tRPC Test API', () => {
|
|
403
|
-
test('should get all tests', async () => {
|
|
404
|
-
const ctx = createContext({} as any);
|
|
405
|
-
const caller = appRouter.createCaller(ctx);
|
|
406
|
-
|
|
407
|
-
const tests = await caller.test.getAll({ take: 10 });
|
|
408
|
-
expect(tests).toBeDefined();
|
|
409
|
-
});
|
|
410
|
-
|
|
411
|
-
test('should create a test', async () => {
|
|
412
|
-
const ctx = createContext({} as any);
|
|
413
|
-
const caller = appRouter.createCaller(ctx);
|
|
414
|
-
|
|
415
|
-
const test = await caller.test.create({
|
|
416
|
-
name: 'Test Name',
|
|
417
|
-
message: 'Test Message',
|
|
418
|
-
});
|
|
419
|
-
expect(test.id).toBeDefined();
|
|
420
|
-
expect(test.name).toBe('Test Name');
|
|
421
|
-
});
|
|
422
|
-
});
|
|
423
|
-
```
|
|
424
|
-
|
|
425
|
-
### Testing GraphQL
|
|
426
|
-
|
|
427
|
-
```typescript
|
|
428
|
-
import { schema } from '../src/graphql/index.js';
|
|
429
|
-
import { graphql } from 'graphql';
|
|
430
|
-
|
|
431
|
-
describe('GraphQL Test API', () => {
|
|
432
|
-
test('should query tests', async () => {
|
|
433
|
-
const query = `
|
|
434
|
-
query {
|
|
435
|
-
tests(take: 5) {
|
|
436
|
-
id
|
|
437
|
-
name
|
|
438
|
-
message
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
`;
|
|
442
|
-
|
|
443
|
-
const result = await graphql({ schema, source: query });
|
|
444
|
-
expect(result.errors).toBeUndefined();
|
|
445
|
-
expect(result.data?.tests).toBeDefined();
|
|
446
|
-
});
|
|
447
|
-
|
|
448
|
-
test('should create a test', async () => {
|
|
449
|
-
const mutation = `
|
|
450
|
-
mutation {
|
|
451
|
-
createTest(input: {
|
|
452
|
-
name: "Test Name"
|
|
453
|
-
message: "Test Message"
|
|
454
|
-
}) {
|
|
455
|
-
id
|
|
456
|
-
name
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
`;
|
|
460
|
-
|
|
461
|
-
const result = await graphql({ schema, source: mutation });
|
|
462
|
-
expect(result.errors).toBeUndefined();
|
|
463
|
-
expect(result.data?.createTest.name).toBe('Test Name');
|
|
464
|
-
});
|
|
465
|
-
});
|
|
466
|
-
```
|
|
467
|
-
|
|
468
|
-
## Advanced: Shared Service Layer
|
|
469
|
-
|
|
470
|
-
For complex business logic, use a shared service layer:
|
|
471
|
-
|
|
472
|
-
```typescript
|
|
473
|
-
// src/services/test.service.ts
|
|
474
|
-
import { prisma } from '../lib/database.js';
|
|
475
|
-
|
|
476
|
-
export const testService = {
|
|
477
|
-
async getAll(options: { skip?: number; take?: number }) {
|
|
478
|
-
return prisma.test.findMany({
|
|
479
|
-
skip: options.skip,
|
|
480
|
-
take: options.take ?? 10,
|
|
481
|
-
orderBy: { createdAt: 'desc' },
|
|
482
|
-
});
|
|
483
|
-
},
|
|
484
|
-
|
|
485
|
-
async create(data: { name: string; message: string; status?: string }) {
|
|
486
|
-
// Add business logic, validation, etc.
|
|
487
|
-
return prisma.test.create({ data });
|
|
488
|
-
},
|
|
489
|
-
};
|
|
490
|
-
|
|
491
|
-
// Use in tRPC (src/routers/test.ts)
|
|
492
|
-
import { testService } from '../services/test.service.js';
|
|
493
|
-
|
|
494
|
-
export const testRouter = router({
|
|
495
|
-
getAll: publicProcedure
|
|
496
|
-
.input(z.object({ skip: z.number().optional(), take: z.number().optional() }))
|
|
497
|
-
.query(({ input }) => testService.getAll(input)),
|
|
498
|
-
});
|
|
499
|
-
|
|
500
|
-
// Use in GraphQL (src/graphql/types/test.ts)
|
|
501
|
-
builder.queryFields((t) => ({
|
|
502
|
-
tests: t.field({
|
|
503
|
-
type: [TestType],
|
|
504
|
-
args: { skip: t.arg.int(), take: t.arg.int() },
|
|
505
|
-
resolve: (_, args) => testService.getAll(args),
|
|
506
|
-
}),
|
|
507
|
-
}));
|
|
508
|
-
```
|
|
509
|
-
|
|
510
|
-
This ensures consistent business logic across both APIs.
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from '@jest/globals';
|
|
2
|
-
import { appRouter } from '../src/router/index.js';
|
|
3
|
-
|
|
4
|
-
describe('API Router', () => {
|
|
5
|
-
it('should have a valid router configuration', () => {
|
|
6
|
-
expect(appRouter).toBeDefined();
|
|
7
|
-
expect(typeof appRouter).toBe('object');
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it('should export the expected router structure', () => {
|
|
11
|
-
// Test that the router has the expected structure
|
|
12
|
-
expect(appRouter._def).toBeDefined();
|
|
13
|
-
expect(appRouter._def.router).toBe(true);
|
|
14
|
-
});
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
describe('Sample API Test', () => {
|
|
18
|
-
it('should pass a basic test', () => {
|
|
19
|
-
expect(1 + 1).toBe(2);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('should handle async operations', async () => {
|
|
23
|
-
const result = await Promise.resolve('test');
|
|
24
|
-
expect(result).toBe('test');
|
|
25
|
-
});
|
|
26
|
-
});
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/** @type {import('jest').Config} */
|
|
2
|
-
module.exports = {
|
|
3
|
-
preset: 'ts-jest',
|
|
4
|
-
testEnvironment: 'node',
|
|
5
|
-
roots: ['<rootDir>/src', '<rootDir>/__tests__'],
|
|
6
|
-
testMatch: [
|
|
7
|
-
'**/__tests__/**/*.{ts,tsx,js}',
|
|
8
|
-
'**/*.{test,spec}.{ts,tsx,js}'
|
|
9
|
-
],
|
|
10
|
-
transform: {
|
|
11
|
-
'^.+\\.tsx?$': 'ts-jest',
|
|
12
|
-
},
|
|
13
|
-
collectCoverageFrom: [
|
|
14
|
-
'src/**/*.{ts,tsx}',
|
|
15
|
-
'!src/**/*.d.ts',
|
|
16
|
-
'!src/**/index.ts',
|
|
17
|
-
],
|
|
18
|
-
coverageDirectory: 'coverage',
|
|
19
|
-
coverageReporters: ['text', 'lcov'],
|
|
20
|
-
testTimeout: 10000,
|
|
21
|
-
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
|
|
22
|
-
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
|
|
23
|
-
};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
// Global test setup for API tests
|
|
2
|
-
// This file runs before all tests
|
|
3
|
-
|
|
4
|
-
// Mock environment variables for testing
|
|
5
|
-
process.env.NODE_ENV = 'test';
|
|
6
|
-
process.env.DATABASE_URL = 'file:./test.db';
|
|
7
|
-
|
|
8
|
-
// Increase timeout for async operations
|
|
9
|
-
jest.setTimeout(10000);
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@{{workspaceScope}}/api",
|
|
3
|
-
"version": "{{version}}",
|
|
4
|
-
"description": "{{description}}",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "src/index.ts",
|
|
7
|
-
"types": "src/index.ts",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"types": "./src/index.ts",
|
|
11
|
-
"import": "./src/index.ts"
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
"scripts": {
|
|
15
|
-
"build": "tsc",
|
|
16
|
-
"dev": "tsx watch src/server.ts",
|
|
17
|
-
"start": "node dist/server.js",
|
|
18
|
-
"test": "jest",
|
|
19
|
-
"test:watch": "jest --watch",
|
|
20
|
-
"test:coverage": "jest --coverage",
|
|
21
|
-
"lint": "eslint src --ext .ts,.tsx",
|
|
22
|
-
"lint:fix": "eslint src --ext .ts,.tsx --fix",
|
|
23
|
-
"type-check": "tsc --noEmit",
|
|
24
|
-
"generate": "prisma generate && yarn generate:pothos",
|
|
25
|
-
"generate:pothos": "tsx scripts/generate-pothos-types.ts"
|
|
26
|
-
},
|
|
27
|
-
"dependencies": {
|
|
28
|
-
"@{{workspaceScope}}/database": "workspace:*",
|
|
29
|
-
"@{{workspaceScope}}/shared": "workspace:*",
|
|
30
|
-
"@pothos/core": "^4.10.0",
|
|
31
|
-
"@pothos/plugin-prisma": "^4.12.0",
|
|
32
|
-
"@pothos/plugin-relay": "^4.6.2",
|
|
33
|
-
"@pothos/plugin-validation": "^4.2.0",
|
|
34
|
-
"@trpc/server": "^11.5.1",
|
|
35
|
-
"cors": "^2.8.5",
|
|
36
|
-
"dotenv": "^16.3.1",
|
|
37
|
-
"express": "^4.18.2",
|
|
38
|
-
"graphql": "^16.9.0",
|
|
39
|
-
"graphql-yoga": "^5.10.6",
|
|
40
|
-
"zod": "^3.22.4"
|
|
41
|
-
},
|
|
42
|
-
"devDependencies": {
|
|
43
|
-
"@types/cors": "^2.8.17",
|
|
44
|
-
"@types/express": "^4.17.21",
|
|
45
|
-
"@types/jest": "^29.5.12",
|
|
46
|
-
"@types/node": "^20.10.4",
|
|
47
|
-
"@typescript-eslint/eslint-plugin": "^6.13.1",
|
|
48
|
-
"@typescript-eslint/parser": "^6.13.1",
|
|
49
|
-
"eslint": "^8.54.0",
|
|
50
|
-
"jest": "^29.7.0",
|
|
51
|
-
"ts-jest": "^29.1.2",
|
|
52
|
-
"tsx": "^4.6.2",
|
|
53
|
-
"typescript": "^5.3.3"
|
|
54
|
-
},
|
|
55
|
-
"keywords": [
|
|
56
|
-
"api",
|
|
57
|
-
"trpc",
|
|
58
|
-
"graphql",
|
|
59
|
-
"pothos",
|
|
60
|
-
"zod",
|
|
61
|
-
"typescript",
|
|
62
|
-
"express"
|
|
63
|
-
],
|
|
64
|
-
"author": "",
|
|
65
|
-
"license": "MIT"
|
|
66
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import type { CreateExpressContextOptions } from '@trpc/server/adapters/express';
|
|
2
|
-
import type { Request, Response } from 'express';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Shared Context
|
|
6
|
-
*
|
|
7
|
-
* This context is used by both tRPC and GraphQL handlers.
|
|
8
|
-
* It provides access to the request/response objects and any
|
|
9
|
-
* shared dependencies like database clients or authenticated user info.
|
|
10
|
-
*/
|
|
11
|
-
export interface Context {
|
|
12
|
-
req: Request;
|
|
13
|
-
res: Response;
|
|
14
|
-
// Add your dependencies here (e.g., database client, external services)
|
|
15
|
-
// Example: db: PrismaClient;
|
|
16
|
-
// Example: user: User | null;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Create tRPC Context
|
|
21
|
-
*
|
|
22
|
-
* Factory function for creating the context for tRPC procedures.
|
|
23
|
-
* Called for each incoming tRPC request.
|
|
24
|
-
*/
|
|
25
|
-
export const createContext = ({ req, res }: CreateExpressContextOptions): Context => {
|
|
26
|
-
return {
|
|
27
|
-
req,
|
|
28
|
-
res,
|
|
29
|
-
// Initialize your dependencies here
|
|
30
|
-
// Example: db: prisma,
|
|
31
|
-
// Example: user: await getUserFromToken(req.headers.authorization),
|
|
32
|
-
};
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Create GraphQL Context
|
|
37
|
-
*
|
|
38
|
-
* Factory function for creating the context for GraphQL resolvers.
|
|
39
|
-
* This is called by GraphQL Yoga for each incoming request.
|
|
40
|
-
*
|
|
41
|
-
* Note: The GraphQL Yoga context factory is defined in src/graphql/index.ts
|
|
42
|
-
* and uses this same Context interface for consistency.
|
|
43
|
-
*/
|
|
44
|
-
export const createGraphQLContext = (req: Request, res: Response): Context => {
|
|
45
|
-
return {
|
|
46
|
-
req,
|
|
47
|
-
res,
|
|
48
|
-
// Same initialization as tRPC context
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
export type { CreateExpressContextOptions };
|
|
File without changes
|