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.

Rules with parameters

See original GitHub issue

Is there a way to have a rule with some input arguments?

I am trying to make a rule like this:

const hasRole = (roleName: string) =>
    rule(`userHasRole-${roleName}`)(
        async (parent, args, ctx, info) => {
            return (ctx.user && ctx.user.role === roleName);
        }
    );

And then use it in this way:

Mutation: {
    createUser: and(user.hasRole("admin"), input.createUser),
},

And this gives me a run-time error:

Error: There seem to be multiple definitions of these rules: userHasRole-admin

I understand that this is exactly what is explained in the documentation. However my question remains, is there a way to have such rules with graphql-shield? I want to use rules for different claims that may be issued to a user. The total number of claims may be quite big and I don’t really want to define a separate function for every claim/role that may exist.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
maticzavcommented, Jun 19, 2019

Hey 👋 ,

That’s a fascinating question. Could you try creating role-specific rules before assigning them to the rule tree? Something like that:

const hasRole = (roleName: string) =>
    rule(`userHasRole-${roleName}`)(
        async (parent, args, ctx, info) => {
            return (ctx.user && ctx.user.role === roleName);
        }
    );

const hasAdminRole = hasRole("admin")
const hadUserRole = hasRole("user")

const permissions = shield({
  Query: {
    user: hasUserRole,
    admin: hasAdminRole,
  },
 Mutation: {
   createUser: hasAdminRole,
  }
})

What the error is saying is that you are trying to use two different rules with the same name (because you create multiple instances). Let me know if that worked; otherwise, we’ll try something new 😄

0reactions
maticzavcommented, Jun 29, 2019

@peterrogov happy to hear that. Thank you for a great thread!

I’ll close the issue as it seems to be resolved but feel free to reopen in case you feel there’s more to it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rules for defining parameters and attributes - IBM
Parameters are required and positional. Each parameter list has a fixed number of parameters. Each parameter is defined as an integer or a...
Read more >
List of Rule Parameters
List of Rule Parameters ; Strict policy calculation. Defines the conditions under which a measurement's status can improve. If the parameter value is...
Read more >
Rule with parameters—ArcGIS CityEngine Resources
In this topic. Rules can be parameterized; in other words, a signature with parameters can be defined, and the matching signature is chosen...
Read more >
Using Parameters as a Business Rule - Oracle Help Center
These execution variables are passed as parameters. The check box for execution variables with a Rule scope is clear by default, because these...
Read more >
What's the difference between a rule and a parameter?
Generally a Parameter is a part of a rule. For every rule you can define individually Parameters. For. With the Parameters you can...
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