Possibility to re-use shield rule in resolver
See original GitHub issueQuestion about GraphQL Shield
Hi, first of all thanks for this great library and project! It saved me a lot of headache and time. 😄
Now for the question, how can I reuse a rule defined for graphql-shield
in a resolver? This would make the checking of inputs for mutations
less repetitive. Below an example of the ideal case for re-useability.
Resolver
import { isAdmin } from "../../../shield/rules";
const updateUser = async (parent, { data: { userId, ...data } }, context, info) => {
/* ---------- Validation ----------*/
await updateUserSchema.validate(data);
// You are not an admin, so you can't pass the role
if (!(await isAdmin()) && data.role) {
throw new Error("You are not allowed to update the role");
}
/* ---------- Process ----------*/
return User.update(userId, data);
};
export default updateUser;
graphql-shield
rule
export const isAdmin = rule()(async (parent, args, { req }) => req.user.role === "ADMIN");
Mutation and input type
type Mutation {
updateUser(data: UpdateUserInput): User!
}
input UpdateUserInput {
userId: String!
role: UserRoleEnum
firstName: String
lastName: String
telephoneNumber: String
email: String
password: String
language: LanguageEnum
notificationLevel: NotificationLevelEnum
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:10
Top Results From Across the Web
Route53Resolver — Boto3 Docs 1.26.32 documentation - AWS
When you associate a rule with a VPC, Resolver forwards all DNS queries for the domain name that is specified in the rule...
Read more >Filter DNS traffic using Route 53 Resolver DNS Firewall
A DNS Firewall rule group is a named, reusable collection of DNS Firewall rules for filtering DNS queries. You populate the rule group...
Read more >Filter DNS traffic using Route 53 Resolver DNS Firewall
A DNS Firewall rule group is a named, reusable collection of DNS Firewall rules for filtering DNS queries. You populate the rule group...
Read more >IAM Actions defined by Amazon Route 53 Resolver
Grants permission to get information about a specified Resolver rule, such as the domain name that the rule forwards DNS queries for and...
Read more >GraphQL Middleware is Open Source - Library to Simplify ...
GraphQL Middleware lets you run arbitrary code before or after a resolver is invoked. It improves your code structure by enabling code reuse...
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
This is not stale. Thank you for your patience and your example. I don’t think it’s worth making rules accessible in the bussiness layer of your application. Since rules can only fail or succeed, you should have sufficient information inside your resolver about the information you are testing (i.e.
isAdmin
in the example).Differentiating between roles in resolvers seems like a bussiness logic that would be very hard to solve with
graphql-shield
. I want to keepshield
serparted from business logic. I hope you understand. 🙂Actually it makes sense as you describe it, the app I use this (awesome) library for also is fine without it 👍