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.

How to use rules on input types and arguments for authorization?

See original GitHub issue

According to https://github.com/maticzav/graphql-shield#rules-on-input-types-or-arguments, itā€™s possible to use rules to validate argument value.

  1. How to apply rules to input types (and specific fields of input types)? In the https://github.com/maticzav/graphql-shield#rules-on-input-types-or-arguments example the rule is in fact applied to the argument, not the input type, and validates the argument as a nested object, instead of applying rules to the fields of the input type.

  2. How to apply rules to arguments and input types (and specific fields of input types) for authorization (permission validation), not value validation?

Suppose there is the following schema:

type Mutation {
  createUser(name: String, email: String, age: Int, verified: Boolean): User!
  updateUser(id: ID!, name: String, email: String, age: Int, verified: Boolean): User!
  deleteUser(id: ID!): User
  ...
}

Itā€™s possible to restrict the whole mutation, e.g.:

const permissions = shield({
  Mutation: {
    updateUser: isAdmin,
  }
})

But what if I want to restrict only updating of the verified field (but allow updating the rest fileds) in the updateUser mutation, not the whole mutation (by applying the rule to the argument)?

Is it possible to do something like the following?

const permissions = shield({
  Mutation: {
    updateUser(
      verified: isAdmin
    )
  }
})

The mutation arguments may be extracted to separate input types (hello, Prisma! šŸ‘‹). In this case the rules should be applied to input type fields. Something like:

type Mutation {
  updateUser(data: UserUpdateInput!, where: UserWhereUniqueInput!): User!
  ...
}

...

input UserUpdateInput {
  name: String
  email: String
  age: Int
  verified: Boolean
}
const permissions = shield({
  UserUpdateInput: {
    email: or(isOwner, isAdmin)
    verified: isAdmin
  }
})

Is it possible?

Expected behavior

The partial response is one of the basic principles of GraphQL: a response may contain both a partial data as well as encountered errors in the case that a field error occurred on a field which was replaced with null.

If a fieldā€™s resolve function throws an error, the error will be inserted into the responseā€™s errors key and the field will resolve to null.

If the null value returns in a resolver for a non-null field, the null result bubbles up to the nearest nullable parent.

This is what concerns data querying (reading). But what about data mutating (writing)?

Suppose there is a mutation that has 3 arguments, and we add validations to all these arguments.

Current behavior: if at least one of the mutation arguments fails validation, the entire mutation will not be executed. Right?

But what if this argument (which failed validation) is not non-null (optional / not required)? In this case the mutation could be executed with two rest arguments.

Is it possible to just remove this argument from mutation if it fails validation?

The argument can be input type (nested object) i.e. contains other input types with its own fields that can also be input types, and so on.

If the field from depth (deep nested input type) fails (permission or value) validation and this field is not non-null (optional / not required), this field should just be removed.

If the field from depth (deep nested input type) fails (permission or value) validation and this field is non-null (required), the null should bubbles up to the nearest nullable parent.

In the case of field (optional or required) was removed, the response should contain both a mutation response as well as encountered errors in the case that a field error occurred on a input field which was removed.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:5
  • Comments:8

github_iconTop GitHub Comments

1reaction
maticzavcommented, May 21, 2019

Hey šŸ‘‹,

Excuse my absence, I had final examinations at school, back at programming now. My point with nesting input types etc., stemmed from the idea that you can also reuse rules across your rule tree. Therefore, I donā€™t see benefit in having rules applicable to input types if we could make them more declarative by applying the same rule in multiple places.

Perhaps I havenā€™t thought this through completely, is the problem here that you cannot nest input rules when inputs take ā€objectsā€ as parameters?

1reaction
FluorescentHallucinogencommented, May 19, 2019

@maticzav PTAL at the implementation by @uxname:

https://github.com/uxname/uxbackend/blob/master/src/helper/InputSheldFilter.js

Could you please integrate it (or something similar) into graphql-shield?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use rules on input types and arguments for ... - GitHub
How to apply rules to arguments and input types (and specific fields of input types) for authorization (permission validation),Ā ...
Read more >
<input>: The Input (Form Input) element - HTML
How an <input> works varies considerably depending on the value of its type attribute, hence the different types are covered in their ownĀ ......
Read more >
Validating parameters (ā€œinput rulesā€)
2. Go to Web Protection > Input Validation > Parameter Validation Rule. To access this part of the web UI, your administrator's account...
Read more >
Validating Input | Web Accessibility Initiative (WAI) - W3C
HTML5 defines a range of built-in functionality to validate common types of input, such as email addresses and dates. In some situations, such...
Read more >
4.1. Group coupled arguments to the new input-type.
The same rule applies to fields within input types. Consider filtering by rating field. Instead of two separate arguments ratingMin and ratingMax ,...
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