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.

Possibility to re-use shield rule in resolver

See original GitHub issue

Question 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:closed
  • Created 4 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
maticzavcommented, Jan 17, 2020

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 keep shield serparted from business logic. I hope you understand. 🙂

0reactions
nealokecommented, Jan 21, 2020

Actually it makes sense as you describe it, the app I use this (awesome) library for also is fine without it 👍

Read more comments on GitHub >

github_iconTop 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 >

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