question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support to synchronous execution

See original GitHub issue

Feature request

Currently, all rules are executed simultaneously using Promise.all. It should be possible to run rules sequentially, it can be very useful in some cases.

Is your feature request related to a problem? Please describe

Yes, here is an example of a group of permissions to create a bank transfer.

and(isUser, canCreateBankTransfers, canUseBankAccount)

In this scenario, canUseBankAccounk can only be executed if the user is logged in and can create bank transfers. If canUseBankAccount runs without the user being logged in and before isUser is resolved, it will eventually crash, since we could be using the user context which isUser evaluates.

To fix that, we need check if context.user is set in every rule we need to get the user information. This leads to a lot of duplicate logic.

Describe the solution you’d like

Here are some ideas:

  • Implement an options argument in every rule.
and(isUser, canCreateBankTransfers, canUseBankAccount, { async: false })
  • Add [RuleName]sync rules (Node.js style).
andSync(isUser, canCreateBankTransfers, canUseBankAccount)
orSync(isUser, canCreateBankTransfers, canUseBankAccount)

Describe alternatives you’ve considered

  1. Writing duplicate code.
  2. Using a single rule to check all the permissions (isUserAndCanCreateBankTransfersUsingThisAccount 😅).
  3. Using rule.resolve inside another rule.

Additional context

  • graphql-shield version: 5.1.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11

github_iconTop GitHub Comments

3reactions
IsaaXcommented, Feb 1, 2019

So this could be due to some recent changes as well. So if you use version 4.1.2, you can achieve this behavior. I tried upgrading to 5.1.0 and all of my specs ended up failing by what you are describing in this issue due to the context.user not being present. Thought I’d provide that info, perhaps @maticzav would know what was changed that caused this behavior changed.

This is how I’ve had my rules defined

and(isAuthenticated, or(isAdmin, RuleWithUserInContext))

and when I upgrade to 5.1.0 it breaks my unit tests. When I revert it back without making any changes (other than change whitelist to fallbackRule or vice versa), it goes back to functioning. It would make sense to short circuit and rules once the first condition returns a false similarly how most programming languages act. https://en.wikipedia.org/wiki/Short-circuit_evaluation That’s what I was imagining when writing out my logic and made my assumption based on that semantic.

2reactions
IsaaXcommented, Feb 5, 2019

@maticzav @iagomelanias here’s a replicated repro https://codesandbox.io/s/jv981orw4v hope it helps! Also might be worth creating your own codesandbox for people to be able to quickly fork it and make faster examples to help you out. Feel free to duplicate mine since it has some custom rules since that’ll be the more common case. Think it would be quite helpful and I can reuse it for future issues as well. Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert Asynchronous calls to Synchronous in JavaScript
Learn Asynchronous calls to Synchronous concepts in JavaScript and convert your Asynchronous calls to Synchronous.
Read more >
Asynchronous vs. Synchronous Programming: When to ...
Let's explore when you should apply asynchronous programming and when sticking to synchronous execution is the best option.
Read more >
Calling Synchronous Methods Asynchronously
The simplest way to execute a method asynchronously is to start executing the method by calling the delegate's BeginInvoke method, do some work ......
Read more >
Synchronous and asynchronous requests - Web APIs | MDN
Synchronous requests block the execution of code which causes "freezing" on the screen and an unresponsive user experience. Asynchronous request.
Read more >
Asynchronous vs synchronous execution. What is the ...
Oddly enough "Synchronously" means "using the same clock" so when two instructions are synchronous they use the same clock and must happen one...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found