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.

Add example for using custom directives

See original GitHub issue

@fabien0102 pointed out a library to use custom directives, that integrates very well with graphql-yoga. It would be great if that could be added to howtographql as a recipe: https://github.com/smooth-code/graphql-directive.

@fabien0102 feel free to share a code snippet in this issue. 🙏

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
giautmcommented, Feb 1, 2018

I open PR to allow passing directiveResolvers as a prop. 😄 https://github.com/graphcool/graphql-yoga/pull/152

2reactions
fabien0102commented, Jan 26, 2018

This is my diff 😉

diff --git a/src/index.js b/src/index.js
index 6f5527c..ca1ad08 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,4 +1,5 @@
 const { GraphQLServer } = require("graphql-yoga");
+const { addDirectiveResolveFunctionsToSchema } = require("graphql-directive");
 const { MongoClient } = require("mongodb");
 const config = require("config");
 const jwt = require("express-jwt");
@@ -61,6 +62,15 @@ const start = async () => {
       }
     });
 
+    addDirectiveResolveFunctionsToSchema(server.executableSchema, {
+      async auth(resolve, source, { permission }, { user }) {
+      if (!user.permissions.includes(permission)) {
+          throw Error("Not authorized");
+        }
+        return resolve();
+      }
+    });
+
     // Health route
     server.express.get("/health", (req, res) => res.json({ ok: true }));
 
diff --git a/src/schema.graphql b/src/schema.graphql
index 4824fc0..d22f080 100644
--- a/src/schema.graphql
+++ b/src/schema.graphql
 
+# Require to have particular permission auth
+directive @auth(permission: String) on FIELD_DEFINITION
+
@@ -76,7 +80,7 @@ type Query {
-  allQuestionnaires: [Questionnaire!]!
+  allQuestionnaires: [Questionnaire!]! @auth(permission: "admin")

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use and create custom directives in Angular
Creating a custom directive is just like creating an Angular component. To create a custom directive we have to replace @Component decorator ...
Read more >
How to Create & Use Custom Directive In Angular
The Angular has several built-in attribute directives. Let us create a ttClass directive, which allows us to add class to an element. Similar...
Read more >
Angular Directive Tutorial With Example | Custom Directives
This blog will introduce you to Angular Directive with sample codes for better understanding. You will also learn about Custom Directives in ...
Read more >
Creating a Custom Directive • Angular - codecraft.tv
We create a directive by decorating a class with the @Directive decorator. The convention is to associate a directive to an element via...
Read more >
Attribute directives - Angular
To create a directive, use the CLI command ng generate directive . · Import ElementRef from @angular/core . · Add ElementRef in the...
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