Skip to content

NestJS-Trpc fails to generate schema using imports in monorepo. #63

@ctrhub

Description

@ctrhub

Hey @KevinEdry. Thanks for this great package.

I have a problem with schema generation. For some reason it can't resolve paths in monorepo.
In my case I created Zod schemas in a package named @repo/gateway-shared. I build this package with tsup.

Here is a sample code that leads to the issue:

// users.router.ts
import { Router, Query } from 'nestjs-trpc';

import { User, userSchema } from '@repo/gateway-shared/schemas';

import { UsersService } from '#gateway/shared/services/users/users.service';
import { ErrorsUtils } from '#gateway/shared/utils/errors.utils';

@Router({ alias: 'users' })
export class UsersRouter {
  constructor(private readonly usersService: UsersService) {}

  @Query({ output: userSchema,})
  async currentUser(): Promise<User> {
    const user = await this.usersService.findOne('id');

    if (!user) throw ErrorsUtils.Unauthorized();

    return user;
  }
}

Here is the userSchema:

// user.schema.ts
export const userSchema = z.object({
  id: z.string(),
  externalId: z.string(),
  email: z.string(),
  firstName: z.string(),
  lastName: z.string(),
  avatarKey: z.string(),
  avatarUrl: z.string(),
  isOnboarded: z.string(),
  password: z.string(),
  createdAt: z.date()
});

And here is the generated schema:

// server.ts
import { initTRPC } from "@trpc/server";
import { z } from "zod";

const t = initTRPC.create();
const publicProcedure = t.procedure;

const appRouter = t.router({ users: t.router({ currentUser: publicProcedure.output(userSchema).query(async () => "PLACEHOLDER_DO_NOT_REMOVE" as any) }) });
export type AppRouter = typeof appRouter;

You may notice that userSchema is not imported/generated in the server.ts file.


I also tried:

  1. Add this schema to schemaFileImports:
// trpc.module.ts
import { userSchema } from '@repo/gateway-shared/schemas';

// ...
TRPCModule.forRoot({
    context: AppContext,
    autoSchemaFile: process.env.NODE_ENV === 'production' ? undefined : './src/trpc/@generated',
    schemaFileImports: [userSchema]
  }),

This did't work.

  1. Add explicit package path to tsconfig.json:

Notice "@repo/gateway-shared/*": ["../../node_modules/@repo/gateway-shared/*"], in the end of the file.

{
  "extends": "@repo/typescript-config/base.json",
  "compilerOptions": {
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2021",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    // https://github.com/trpc/trpc/discussions/3472#discussioncomment-5988620
    "strictNullChecks": true,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,

    "paths": {
      // https://github.com/trpc/trpc/issues/2717#issuecomment-1247425462
      "@trpc/server/*": ["../../node_modules/@trpc/server/*"],
      "@repo/gateway-shared/*": ["../../node_modules/@repo/gateway-shared/*"],
    }
  }
}

This did't work.


Any ideas why it can't be generated?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions