db2graphql
Advanced tools
Comparing version 0.3.7 to 0.3.8
{ | ||
"name": "db2graphql", | ||
"version": "0.3.7", | ||
"version": "0.3.8", | ||
"description": "Generate Graphql schema based on existing relational database", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -74,9 +74,10 @@ const PostgreSQL = require('../src/adapters/postgres'); | ||
/** | ||
* Set authorization callback | ||
* Set before hook | ||
* | ||
* @param {Function} validator | ||
* @param {Function} validator The validator callback. Must return true/false | ||
* @param {Function} rejected The rejected callback. Must return resolver type/null | ||
*/ | ||
isAuthorized(validator, rejected) { | ||
this.resolver.isAuthorizedHook.validator = validator | ||
if (rejected) this.resolver.isAuthorizedHook.rejected = rejected; | ||
onBefore(validator, rejected) { | ||
this.resolver.beforeHook.validator = validator | ||
if (rejected) this.resolver.beforeHook.rejected = rejected; | ||
} | ||
@@ -83,0 +84,0 @@ |
@@ -29,4 +29,4 @@ const utils = require('../utils/utils'); | ||
// Default authorization hook | ||
this.isAuthorizedHook = { | ||
// Default before hook | ||
this.beforeHook = { | ||
validator: async () => true, | ||
@@ -154,4 +154,4 @@ rejected: async () => null | ||
this.resolvers[namespace][name] = async (root = null, args = {}, context = {}) => { | ||
const isAuthorized = await this.isAuthorizedHook.validator(namespace, name, root, args, context); | ||
if (!isAuthorized) return await this.isAuthorizedHook.rejected(namespace, name, root, args, context); | ||
const passBefore = await this.beforeHook.validator(namespace, name, root, args, context); | ||
if (!passBefore) return await this.beforeHook.rejected(namespace, name, root, args, context); | ||
const db = this.dbDriver ? this.dbDriver.db : null; | ||
@@ -158,0 +158,0 @@ context.ioc = { resolver: this, db }; |
@@ -269,3 +269,3 @@ const db2g = require('../src/db2g'); | ||
test('it should set and pass authorization hook', async (done) => { | ||
test('it should set and pass before hook', async (done) => { | ||
const api = new db2g(); | ||
@@ -277,3 +277,3 @@ const validator = async (type, field, parent, args) => { | ||
} | ||
api.isAuthorized(validator) | ||
api.onBefore(validator) | ||
api.add('Mutation', 'putFoo', 'Foo', (root, args, context) => { | ||
@@ -286,6 +286,6 @@ done(); | ||
test('it should set and get denied in authorization hook', async (done) => { | ||
test('it should set and get denied on before hook', async (done) => { | ||
const api = new db2g(); | ||
const validator = async () => false; | ||
api.isAuthorized(validator); | ||
api.onBefore(validator); | ||
api.add('Mutation', 'putFoo', 'Foo', () => {}); | ||
@@ -298,3 +298,3 @@ const resolvers = api.getResolvers(); | ||
test('it should set and get rejected in authorization hook', async (done) => { | ||
test('it should set and get rejected on before hook', async (done) => { | ||
const api = new db2g(); | ||
@@ -311,3 +311,3 @@ const validator = async (type, field, parent, args) => { | ||
} | ||
api.isAuthorized(validator, rejected) | ||
api.onBefore(validator, rejected) | ||
api.add('Mutation', 'putFoo', 'Foo', () => {}); | ||
@@ -314,0 +314,0 @@ const resolvers = api.getResolvers(); |
@@ -254,5 +254,5 @@ const Resolver = require('../../src/graphql/resolver'); | ||
test('it should get denied on default authorization hook', async (done) => { | ||
test('it should get denied on default before hook', async (done) => { | ||
const resolver = new Resolver(); | ||
resolver.isAuthorizedHook.validator = async () => false; | ||
resolver.beforeHook.validator = async () => false; | ||
const callback = async () => {} | ||
@@ -259,0 +259,0 @@ resolver.add('Query', 'getPage', 'foo', callback); |
153049
2394