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

Export AnyTRPCProcedureBuilder from @trpc/server #5497

Open
wants to merge 5 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/server/src/@trpc/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export {
type RouterRecord as TRPCRouterRecord,
type AnySubscriptionProcedure as AnyTRPCSubscriptionProcedure,
type ProcedureOptions as TRPCProcedureOptions,
type AnyProcedureBuilder as AnyTRPCProcedureBuilder,
} from '../../unstable-core-do-not-import';

export type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ export type AnyProcedureBuilder = ProcedureBuilder<
any,
any,
any,
any,
any,
any,
any
UnsetMarker,
UnsetMarker,
UnsetMarker,
UnsetMarker
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I'm basically 100% sure this is going to cause regressions which our test suite may not be capturing yet (because we never broke it before)

Here's the reason: https://trpc.io/docs/server/validators#input-merging

and here's the scenario:

image

It's totally valid to set up an input or output and then create a router with that procedure, so UnsetMarker wouldn't be the root type in that situation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so as per https://github.com/trpc/trpc/pull/5497/files#r1494523733 I think what we could do is leave this AnyProcedureBuilder be

If you copy it over to packages/react-query/src/shared/polymorphism and export it from there though, I think we can live with its shortcomings as a limitation and open it up later. But we shouldn't make the rest of the codebase live with UnsetMarker hard-coded

>;

/**
Expand Down Expand Up @@ -325,9 +325,9 @@ export function createBuilder<TContext, TMeta>(
const builder: AnyProcedureBuilder = {
_def,
input(input) {
const parser = getParseFn(input as Parser);
const parser = getParseFn(input);
return createNewBuilder(_def, {
inputs: [input as Parser],
inputs: [input],
middlewares: [createInputMiddleware(parser)],
});
},
Expand Down
2 changes: 0 additions & 2 deletions packages/tests/server/react/polymorphism.common.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { initTRPC } from '@trpc/server';

export const t = initTRPC.create();

export type $RootTypes = (typeof t)['_config']['$types'];
12 changes: 2 additions & 10 deletions packages/tests/server/react/polymorphism.factory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
// building factories which can produce common functionality over a homologous data source.
//
import type { RouterLike, UtilsLike } from '@trpc/react-query/shared';
import type { AnyTRPCProcedureBuilder } from '@trpc/server';
import { TRPCError } from '@trpc/server';
import type {
AnyRootTypes,
createBuilder,
} from '@trpc/server/unstable-core-do-not-import';
import z from 'zod';
import type { $RootTypes } from './polymorphism.common';
import { t } from './polymorphism.common';

//
Expand All @@ -33,10 +29,6 @@ export type FileExportStatusType = z.infer<typeof FileExportStatus>;
// Dependencies
//

type BaseProcedure<TRoot extends AnyRootTypes> = ReturnType<
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly what was here before actually does produce a type with typeof unsetMarker there, so inter-compat with .input merging wouldn't work in production today for router factories. Maybe we can find a compromise then

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried export this from @trpc/server basically and found the same problem popped up with procedure.input() being passed through

// type $RootTypes = ReturnType<typeof initTRPC.create>['_config']['$types'];
// export type AnyTRPCProcedureBuilder = ReturnType<
//   typeof createBuilder<$RootTypes['ctx'], $RootTypes['meta']>
// >;

typeof createBuilder<TRoot['ctx'], TRoot['meta']>
>;

export type DataProvider = FileExportStatusType[];

//
Expand All @@ -47,7 +39,7 @@ export type DataProvider = FileExportStatusType[];
let COUNTER = 1;

export function createExportRoute<
TBaseProcedure extends BaseProcedure<$RootTypes>,
TBaseProcedure extends AnyTRPCProcedureBuilder,
>(baseProcedure: TBaseProcedure, dataProvider: DataProvider) {
return t.router({
start: baseProcedure
Expand Down
15 changes: 2 additions & 13 deletions packages/tests/server/react/polymorphism.subtyped-factory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
// which can be used with existing components, but has extra data for other use cases
//
import type { RouterLike, UtilsLike } from '@trpc/react-query/shared';
import type { AnyTRPCProcedureBuilder } from '@trpc/server';
import { TRPCError } from '@trpc/server';
import type {
AnyRootTypes,
createBuilder,
createRouterFactory,
} from '@trpc/server/unstable-core-do-not-import';
import z from 'zod';
import { t } from './polymorphism.common';
import { FileExportRequest, FileExportStatus } from './polymorphism.factory';
Expand All @@ -32,13 +28,6 @@ export type SubTypedFileExportStatusType = z.infer<
// Dependencies
//

type RouterFactory<TRoot extends AnyRootTypes> = ReturnType<
typeof createRouterFactory<TRoot>
>;
type BaseProcedure<TRoot extends AnyRootTypes> = ReturnType<
typeof createBuilder<TRoot['ctx'], TRoot['meta']>
>;

export type SubTypedDataProvider = SubTypedFileExportStatusType[];

//
Expand All @@ -49,7 +38,7 @@ export type SubTypedDataProvider = SubTypedFileExportStatusType[];
let COUNTER = 1;

export function createSubTypedExportRoute<
TBaseProcedure extends BaseProcedure<(typeof t)['_config']['$types']>,
TBaseProcedure extends AnyTRPCProcedureBuilder,
>(baseProcedure: TBaseProcedure, dataProvider: SubTypedDataProvider) {
return t.router({
start: baseProcedure
Expand Down