Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use if condition in GraphQL Shield #1494

Closed
kxbin opened this issue Nov 19, 2022 · 2 comments
Closed

How to use if condition in GraphQL Shield #1494

kxbin opened this issue Nov 19, 2022 · 2 comments

Comments

@kxbin
Copy link

kxbin commented Nov 19, 2022

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
Copy link
Collaborator

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
Copy link
Author

kxbin commented Dec 22, 2022

Thanks so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants