I am working on a [email protected] app – I’m new to it.
I have a postRouter like so:
export const postRouter = createTRPCRouter({
create: protectedProcedure
.input(createPostSchema)
.mutation(async ({ ctx, input } /*where does this type come from? */) => {
/* lots of logic to check if we can create a post ( uniqueness checks ) */
return ctx.db.post.create({
data: {
author: {
connect: { id: ctx.session.user.id },
},
content: input.content,
excerpt: input.excerpt,
slug,
title: input.title,
videoUrl: input.videoUrl,
},
});
}),
getSecretMessage: protectedProcedure.query(() => {...}),
});
I want to refactor this to
export const postRouter = createTRPCRouter({
create: protectedProcedure
.input(createPostSchema)
.mutation(createPostService)
});
How exactly do I specify the create post argument types for this resolver fn?
https://trpc.io/docs/server/procedures#inferProcedureBuilderResolverOptions and https://trpc.io/docs/client/vanilla/infer-types seems to be a start, but not exactly sure the best way here.
// this is what I have come up with - but there's gotta be a better way.
type NotGreatNotTerrible = inferProcedureBuilderResolverOptions<typeof protectedProcedure> & {
input: inferRouterInputs<typeof postRouter>["create"];
}
export const createPostService = async ({ ctx, input }: NotGreatNotTerrible) => {
/* lots of logic to check if we can create a post ( uniqueness checks ) */
return ctx.db.post.create({
data: {
author: {
connect: { id: ctx.session.user.id },
},
content: input.content,
excerpt: input.excerpt,
slug,
title: input.title,
videoUrl: input.videoUrl,
},
});
};