Add support to synchronous execution
See original GitHub issueFeature 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
- Writing duplicate code.
- Using a single rule to check all the permissions (isUserAndCanCreateBankTransfersUsingThisAccount 😅).
- Using
rule.resolve
inside another rule.
Additional context
graphql-shield
version: 5.1.0
Issue Analytics
- State:
- Created 5 years ago
- Comments:11
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 to5.1.0
and all of my specs ended up failing by what you are describing in this issue due to thecontext.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 circuitand
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.@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!