outis99
outis992y ago

Mutation type issue

I'm trying to mutate something like this with tRPC and Prisma
const updateColumn = trpc.project.update.useMutation();
updateColumn.mutate({ colId: list.id, cards: list.cards })
const updateColumn = trpc.project.update.useMutation();
updateColumn.mutate({ colId: list.id, cards: list.cards })
list.id is just a string but list.cards has a type of Card[] which I'm importing from a file Prisma auto-generated from my schema Then on my backend
update: protectedProcedure
.input(
z.object({
colId: z.string(),
cards: //what do I put here?
})
)
.mutation(async ({ input, ctx }) => {
const result = await ctx.prisma.column.update({
where: {
id: input.colId,
},
data: {
cards: input.cards, //type error
},
});

console.log(result, "?dbresult");
})
update: protectedProcedure
.input(
z.object({
colId: z.string(),
cards: //what do I put here?
})
)
.mutation(async ({ input, ctx }) => {
const result = await ctx.prisma.column.update({
where: {
id: input.colId,
},
data: {
cards: input.cards, //type error
},
});

console.log(result, "?dbresult");
})
I tried putting something like z.array(z.object({})) but obviously that doesn't work and it shows this error
20 Replies
volks
volks2y ago
@Outis I can pinch in with what I'd do, but you either define the types manually, or you can use something like https://github.com/omar-dulaimi/prisma-zod-generator and then generate the zod schema based on the card
GitHub
GitHub - omar-dulaimi/prisma-zod-generator: Prisma 2+ generator to ...
Prisma 2+ generator to emit Zod schemas from your Prisma schema - GitHub - omar-dulaimi/prisma-zod-generator: Prisma 2+ generator to emit Zod schemas from your Prisma schema
volks
volks2y ago
But if the card is a simple type then just manually create it
outis99
outis992y ago
This is really useful but my problem has evolved even further lol My front end receives an array of objects and I just want to overwrite/update my previous array of objects inside my cards property I've tried plenty of solutions, posted on the slack prisma help channel but nothing yet and I'm really contemplating if the switch to trcp/prisma is worth it
volks
volks2y ago
What exactly is the issue that prevents you from updating the previous array?
outis99
outis992y ago
I just don't understand the prisma update syntax for it
const column = await ctx.prisma.column.update({
where: {
id: input.colId,
},
data: {
cards: {},
},
});
const column = await ctx.prisma.column.update({
where: {
id: input.colId,
},
data: {
cards: {},
},
});
This is where I get to and I truly have tried so much stuff
volks
volks2y ago
You need to update all the cards? Or you just need to update a single entity with the cards?
outis99
outis992y ago
I simply want to update the order of the cards So what I'm doing is reorder my cards in my front end and send it to my backend
volks
volks2y ago
Like this?
const column = await ctx.prisma.column.update({
where: {
id: input.colId,
},
data: {
cards: CARDS_YOU_GET_FROM_FRONTEND,
},
});
const column = await ctx.prisma.column.update({
where: {
id: input.colId,
},
data: {
cards: CARDS_YOU_GET_FROM_FRONTEND,
},
});
outis99
outis992y ago
You would think it works like that but doesn't
volks
volks2y ago
Are you getting any errors or?
outis99
outis992y ago
volks
volks2y ago
Yeah well it says that the type is invalid Your cards you got from the frontend seems to be incorrect Can you print typeof cards[0]?
outis99
outis992y ago
export type Card = {
id: string
title: string
index: number
description: string | null
columnId: string
}
export type Card = {
id: string
title: string
index: number
description: string | null
columnId: string
}
This is the type It's exactly the same, I don't it's a type issue I think it has to do with how updating records works in Prisma when you also have relations
volks
volks2y ago
Yeah I'd say this is not related to tRPC but rather prisma, their error message is horrible so I cant decrypt much more from it IS there any more info in the error?
outis99
outis992y ago
Argument cards: Got invalid value
[
{
id: 'cl9ego52z0001wb0k7ueqykh7',
title: 'Test 2',
index: 1,
description: null,
columnId: 'cl9eftdjm0002wbucv6rjulj0'
},
{
id: 'cl9eg8r4r0000wb0kd9j6cy17',
title: 'Test',
index: 0,
description: null,
columnId: 'cl9eftdjm0002wbucv6rjulj0'
}
]
on prisma.updateOneColumn. Provided List<Json>, expected CardUpdateManyWithoutColumnNestedInput:
type CardUpdateManyWithoutColumnNestedInput {
create?: CardCreateWithoutColumnInput | List<CardCreateWithoutColumnInput> | CardUncheckedCreateWithoutColumnInput | List<CardUncheckedCreateWithoutColumnInput>
connectOrCreate?: CardCreateOrConnectWithoutColumnInput | List<CardCreateOrConnectWithoutColumnInput>
upsert?: CardUpsertWithWhereUniqueWithoutColumnInput | List<CardUpsertWithWhereUniqueWithoutColumnInput>
createMany?: CardCreateManyColumnInputEnvelope
set?: CardWhereUniqueInput | List<CardWhereUniqueInput>
disconnect?: CardWhereUniqueInput | List<CardWhereUniqueInput>
delete?: CardWhereUniqueInput | List<CardWhereUniqueInput>
connect?: CardWhereUniqueInput | List<CardWhereUniqueInput>
update?: CardUpdateWithWhereUniqueWithoutColumnInput | List<CardUpdateWithWhereUniqueWithoutColumnInput>
updateMany?: CardUpdateManyWithWhereWithoutColumnInput | List<CardUpdateManyWithWhereWithoutColumnInput>
deleteMany?: CardScalarWhereInput | List<CardScalarWhereInput>
}
Argument cards: Got invalid value
[
{
id: 'cl9ego52z0001wb0k7ueqykh7',
title: 'Test 2',
index: 1,
description: null,
columnId: 'cl9eftdjm0002wbucv6rjulj0'
},
{
id: 'cl9eg8r4r0000wb0kd9j6cy17',
title: 'Test',
index: 0,
description: null,
columnId: 'cl9eftdjm0002wbucv6rjulj0'
}
]
on prisma.updateOneColumn. Provided List<Json>, expected CardUpdateManyWithoutColumnNestedInput:
type CardUpdateManyWithoutColumnNestedInput {
create?: CardCreateWithoutColumnInput | List<CardCreateWithoutColumnInput> | CardUncheckedCreateWithoutColumnInput | List<CardUncheckedCreateWithoutColumnInput>
connectOrCreate?: CardCreateOrConnectWithoutColumnInput | List<CardCreateOrConnectWithoutColumnInput>
upsert?: CardUpsertWithWhereUniqueWithoutColumnInput | List<CardUpsertWithWhereUniqueWithoutColumnInput>
createMany?: CardCreateManyColumnInputEnvelope
set?: CardWhereUniqueInput | List<CardWhereUniqueInput>
disconnect?: CardWhereUniqueInput | List<CardWhereUniqueInput>
delete?: CardWhereUniqueInput | List<CardWhereUniqueInput>
connect?: CardWhereUniqueInput | List<CardWhereUniqueInput>
update?: CardUpdateWithWhereUniqueWithoutColumnInput | List<CardUpdateWithWhereUniqueWithoutColumnInput>
updateMany?: CardUpdateManyWithWhereWithoutColumnInput | List<CardUpdateManyWithWhereWithoutColumnInput>
deleteMany?: CardScalarWhereInput | List<CardScalarWhereInput>
}
Some more that was missing but I don't think it's of any use I also tried something like this
const column = await ctx.prisma.column.update({
where: {
id: input.colId,
},
data: {
cards: {
set: input.cards,
},
},
});
const column = await ctx.prisma.column.update({
where: {
id: input.colId,
},
data: {
cards: {
set: input.cards,
},
},
});
But set only takes exactly one arguement, the id