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.

Allow specifying rules for both types and fields

See original GitHub issue

Feature request

Is your feature request related to a problem? Please describe

As far as I’m aware, right now you can specify shield rules for either a type or a selection of subfields on that type, but not both. So if I wanted to restrict access to users that have been archieved, I can do that no problem, if I want to restrict access so only the current user can see their respective address, no problem either, but I’m not aware of a way to combine the two.

Describe the solution you’d like

Implementing this might be really hard to implement without breaking anything. Ideally you’d have the option to introduce an special field into your type permissions that handles the type permissions, as that would hopefully be the least invasive.

So somethina long the lines of:

shield({
User: {
_canViewModel: rules.notArchieved,
address: rules.isCurrentUser
})

Describe alternatives you’ve considered

Other solutions would include changing the internal shieldRules schema to diffrentiate between subfields and the parent type, or having a diffrent naming convention for the two, along the lines of User: {...}, Usertype: rule, but both of those wouldd probably break to much existing code.

Additional context

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
maticzavcommented, Mar 11, 2020

Hey @luhagel 👋 ,

Thank you for opening the issue. I had a similar idea a long time ago that would allow us to use and, or, and other logical patterns on dictionaries as well as rules. In the case of dictionaries, we would simply merge the rules at the lower level.

For example,

const permissions = shield({
  Query: and(isAuthenticated, {
    fruits: or(isAdmin, isEditor),
    customers: isAdmin,
  },
})

would translate to

const permissions = shield({
  Query: {
    fruits: and(isAuthenticated, or(isAdmin, isEditor)),
    customers: and(isAuthenticated, isAdmin),
  }
})

Is that what you had in mind?

It’s one of the last things that I think are truly missing from the library alongside race rule (https://github.com/maticzav/graphql-shield/issues/645).

I am sad to say this, but since this library lost its funding, I am not sure when I’ll find time to implement these features.

0reactions
maticzavcommented, Jul 3, 2020

Not exactly. I mean it depends on your schema, not your permissions.

If one of your fields (let’s say email) were required in User and the email-resolver returned null, you’d get

{ "user": null }

If email were an optional field, however, you’d get

{ "user": { "name": null, "email": null } }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow specifying rules for both types and fields · Issue #680
As far as I'm aware, right now you can specify shield rules for either a type or a selection of subfields on that...
Read more >
Introduction to data types and field properties
Specifies whether the field has an index. There are three available values: Yes (No duplicates) Creates a unique index on the field. Yes...
Read more >
Fields and Datatypes
After you enter each field's name and data type, you can specify how each field is used by entering properties in the property...
Read more >
Conditional Fields
The fields you select to show can be of any type. You can create multiple rules for a trigger fields, which allows you...
Read more >
Define fields in tables
Field names in the same table must be unique; for instance, you can't have two fields with the name ObjectID. Field names must...
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