graffiti-mongoose
graffiti-mongoose copied to clipboard
Schema level mutation hooks
From the docs, resolve hooks can be added per field in the schema.
For mutations, we define it through a top level hook object we pass to getSchema.
Is there a way to define mutation hooks inside the schema? Like so:
const UserSchema = new mongoose.Schema({
name: {
type: String,
},
hooks: {
mutation: {
addUser: {
pre: (next, root, args, context) => {
authorize(request)
next()
}
}
}
}
});
I know this isn't possible since hooks will be seen as a type, but it's the gist of the idea
I've digged around in the source and something like this doesn't seem to be possible. What seems to be possible is creating a sort of root reducer for the mutations (like redux) and making it resolve the mutation based on the context's fieldName. So something like:
pre: (next, args, context) => {
switch(context.fieldName) {
case 'addUser':
authorize(request)
return next()
}
}
I'd like to know if this is the canonical way to do this, and it'd be great to have examples in the docs for this use case.