Skip to content
This repository has been archived by the owner on May 31, 2019. It is now read-only.

Commit

Permalink
Merge pull request #61 from aerogear/directives-spike
Browse files Browse the repository at this point in the history
feat: initial empty hasRole implementation
  • Loading branch information
Dara Hayes committed Aug 21, 2018
2 parents 1c4190e + 0b380c3 commit 27cee76
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
20 changes: 20 additions & 0 deletions server/lib/schemaDirectives/hasRole.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { SchemaDirectiveVisitor } = require('graphql-tools')
const { defaultFieldResolver } = require('graphql')
const { log } = require('../util/logger')

class HasRoleDirective extends SchemaDirectiveVisitor {
visitFieldDefinition (field) {
const { resolve = defaultFieldResolver } = field
const { role } = this.args
field.resolve = async function (root, args, context, info) {
log.info('checking has role directive', role)
log.info('Printing user', context.request.session)
// TODO if (context.request.kauth.grant.access_token.hasRealmRole(role))
// Return appropriate error if this is false
const result = await resolve.apply(this, [root, args, context, info])
return result
}
}
}

module.exports = HasRoleDirective
7 changes: 6 additions & 1 deletion server/lib/schemaParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const dataSourceParser = require('./datasources/dataSourceParser')
const resolverMapper = require('./resolvers/resolverMapper')
const subscriptionsMapper = require('./subscriptions/subscriptionMapper')

const HasRoleDirective = require('./schemaDirectives/hasRole')

module.exports = function (schemaString, dataSourcesJson, resolverMappingsJson, subscriptionMappingsJson, pubsub) {
const dataSources = dataSourceParser(dataSourcesJson)
const subscriptionResolvers = subscriptionsMapper(subscriptionMappingsJson, pubsub)
Expand All @@ -12,7 +14,10 @@ module.exports = function (schemaString, dataSourcesJson, resolverMappingsJson,

const schema = makeExecutableSchema({
typeDefs: [schemaString],
resolvers
resolvers: resolvers,
schemaDirectives: {
hasRole: HasRoleDirective
}
})

return {schema, dataSources}
Expand Down

0 comments on commit 27cee76

Please sign in to comment.