maticzav / graphql-shield

🛡 A GraphQL tool to ease the creation of permission layer.

Home Page:https://graphql-shield.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use `if` condition in GraphQL Shield

kxbin opened this issue · comments

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

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!

Thanks so much