Using pipe.resolver for consecutive mutations #2446
-
|
As I understand, Also, how should I rewrite my mutation below to use updateLocation.ts maybe something like this.. Thanks 🍒 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Not sure if I totally understand, but sounds like you can use |
Beta Was this translation helpful? Give feedback.
-
You can put all your code in a single function like below. Really the only reason to add extra functions is if you want to easily share certain functions among multiple resolvers (like const UpdateLocation = z.object({
name: z.string(),
label: z.string(),
locality: z.string().optional(),
country: z.string().optional(),
region: z.string().optional(),
})
export default resolver.pipe(
resolver.zod(UpdateLocation),
resolver.authorize(),
async (location, ctx) => {
try {
const updateOrCreateLocation = await db.location.upsert({
where: {
label: location.label,
},
update: {},
create: {
name: location.name,
label: location.label,
locality: location?.locality,
region: location?.region,
country: location?.country,
},
})
const updatedLocation = await db.user.update({
where: {
id: ctx.session.userId,
},
data: {
profile: {
update: {
locationId: updateOrCreateLocation.id,
},
},
},
select: {
id: true,
profile: true,
},
})
console.log("updatedLocation: ", updatedLocation)
return updateOrCreateLocation
} catch (error) {
console.log("Error while updating your location: ", error)
}
}
) |
Beta Was this translation helpful? Give feedback.
You can put all your code in a single function like below.
Really the only reason to add extra functions is if you want to easily share certain functions among multiple resolvers (like
.zodand.authorize)