How to use `if` condition in GraphQL Shield
kxbin opened this issue · 2 comments
kxbin commented
Question about GraphQL Shield
Hello, For example I have an upsertTodo
mutation
By judging whether there is an id parameter in the input
If it's a create, all users can, if it's an update, it must be the owner of the object
Thanks
dimatill commented
Hi @kxbin ! Sorry for the late response.
you can use regular if condition inside any shield rule, just like this:
rule()(async (parent, args, ctx) => {
if (!args.id) {
return true;
}
const existingTodo = await db.getTodoById(args.id);
if (existingTodo.ownerId === ctx.userId) {
return true;
}
return false;
})
Please feel free to reopen the issue if there are still some questions!
kxbin commented
Thanks so much