This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {SchemaDirectiveVisitor} = require('graphql-tools') | |
const {defaultFieldResolver} = require('graphql') | |
//... | |
class AuthDirective extends SchemaDirectiveVisitor { | |
visitFieldDefinition(field) { | |
const {resolve = defaultFieldResolver} = field | |
const {roles: expectedRoles = []} = this.args | |
console.log(expectedRoles.length); | |
field.resolve = (...args) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const jwt = require('jsonwebtoken') | |
//... | |
const makeContext = (req) => { | |
if (!req.event || !req.event.headers || !req.event.headers.Authorization) { | |
return {} // no auth yet | |
} | |
const token = req.event.headers.Authorization; | |
const decoded = jwt.verify( | |
token.replace('Bearer ', ''), | |
'secret' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { GraphQLServer } = require('graphql-yoga') | |
const typeDefs = ` | |
type Query { | |
hello(name: String): String! | |
} | |
` | |
const resolvers = { | |
Query: { |
NewerOlder