-
I've found it very common for mutations to want the following:
So let's make that easier. I'm proposing adding a export const CreateProject = z.object({
name: z.string(),
dueDate: z.date().optional(),
orgId: z.number().optional(),
})
// Pipe function runs functions in order.
// So here CreateProject.parse will be called on input data first verifying input
// This will automatically add correct type to the (input, ctx) arguments of next function.
export default resolverPipe(CreateProject.parse, async (input, ctx) => {
// orgId input is optional, defaults to currentOrgId
input.orgId ??= ctx.session.orgId
// User must be logged in
ctx.session.$authorize()
// If input.orgId does not match the current user, require user to have admin role
ctx.session.$authorize("admin", {if: input.orgId !== ctx.session.orgId})
const project = await db.project.create({
data: input,
})
return project
}) And with a new export default resolverPipe(
CreateProject.parse,
// set default orgId
(input, {session}) => ({orgId: session.orgId, ...input}),
authorize(),
authorize("admin", (input, {session}) => input.orgId !== session.orgId),
async (input) => {
console.log("Creating project...")
const project = await db.project.create({
data: input,
})
console.log("Created project")
return project
},
) Furthermore since requiring a logged in user is so common, we could add Key Benefits
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
If you're curious, here's an earlier iteration of this general idea that I didn't like very well: #1234 |
Beta Was this translation helpful? Give feedback.
-
I don't know about you all, but the more I think about this the more I really like it! It's so powerful and elegant. I don't particularly like the name of |
Beta Was this translation helpful? Give feedback.
-
Just want to make sure I'm understanding this correctly 😄 Does the output of each step (I like the name step somewhere in the method name btw) set the value of What happens when one of the steps fails? Does it stop the whole chain? Can you post an example of how you'd deal with errors? Is What is the second signature of authorize mean with two arguments? Is it: |
Beta Was this translation helpful? Give feedback.
-
Love it! 🥰 |
Beta Was this translation helpful? Give feedback.
-
Update!Here's the final result. I had to have a little wrapper for zod schemas to make the types correct, so decided to make This is fully working both for strong types and runtime! :) 😍😍😍😍😍😍 import {pipe} from "blitz"
import db from "db"
import * as z from "zod"
export const CreateProject = z.object({
name: z.string(),
dueDate: z.date().optional(),
orgId: z.number().optional(),
})
export default pipe.resolver(
pipe.zod(CreateProject),
pipe.authorize(),
// Set default orgId
(input, {session}) => ({orgId: session.orgId, ...input}),
async (input, ctx) => {
console.log("Creating project...", ctx.session.orgId)
const project = await db.project.create({
data: input,
})
console.log("Created project")
return project
},
) PR coming soon |
Beta Was this translation helpful? Give feedback.
Update!
Here's the final result. I had to have a little wrapper for zod schemas to make the types correct, so decided to make
pipe
and object that we can add various utilities to. Currently haspipe.resolver
,pipe.zod
, andpipe.authorize
This is fully working both for strong types and runtime! :)
😍😍😍😍😍😍