Controlando errores en la respuesta de los resolvers en graphql

parent bbaf0eae
'use strict';
const { permissions } = require('../../../lib/auth');
module.exports = function setupResolver (services) {
module.exports = function setupResolver (services, res) {
const { EntidadService } = services;
return {
......@@ -10,13 +10,13 @@ module.exports = function setupResolver (services) {
permissions(context, 'entidades:read|usuarios:read');
let items = await EntidadService.findAll(args, context.rol, context.id_entidad);
return items.data;
return res(items);
},
entidad: async (_, args, context) => {
permissions(context, 'entidades:read');
let item = await EntidadService.findById(args.id);
return item.data;
return res(item);
}
},
Mutation: {
......@@ -25,7 +25,7 @@ module.exports = function setupResolver (services) {
args.entidad._user_created = context.id_usuario;
let item = await EntidadService.createOrUpdate(args.entidad);
return item.data;
return res(item);
},
entidadEdit: async (_, args, context) => {
permissions(context, 'entidades:update');
......@@ -34,13 +34,13 @@ module.exports = function setupResolver (services) {
args.entidad._updated_at = new Date();
args.entidad.id = args.id;
let item = await EntidadService.createOrUpdate(args.entidad);
return item.data;
return res(item);
},
entidadDelete: async (_, args, context) => {
permissions(context, 'entidades:delete');
let deleted = await EntidadService.deleteItem(args.id);
return { deleted: deleted.data };
return { deleted: res(deleted) };
}
}
};
......
'use strict';
const { permissions } = require('../../../lib/auth');
module.exports = function setupResolver (services) {
module.exports = function setupResolver (services, res) {
const { ModuloService } = services;
return {
......@@ -10,13 +10,13 @@ module.exports = function setupResolver (services) {
permissions(context, 'modulos:read');
let items = await ModuloService.findAll(args);
return items.data;
return res(items);
},
modulo: async (_, args, context) => {
permissions(context, 'modulos:read');
let items = await ModuloService.findById(args.id);
return items.data;
return res(items);
}
},
Mutation: {
......@@ -25,7 +25,7 @@ module.exports = function setupResolver (services) {
args.modulo._user_created = context.id_usuario;
let item = await ModuloService.createOrUpdate(args.modulo);
return item.data;
return res(item);
},
moduloEdit: async (_, args, context) => {
permissions(context, 'modulos:update');
......@@ -34,13 +34,13 @@ module.exports = function setupResolver (services) {
args.modulo._updated_at = new Date();
args.modulo.id = args.id;
let item = await ModuloService.createOrUpdate(args.modulo);
return item.data;
return res(item);
},
moduloDelete: async (_, args, context) => {
permissions(context, 'modulos:delete');
let deleted = await ModuloService.deleteItem(args.id);
return { deleted: deleted.data };
return { deleted: res(deleted) };
}
}
};
......
......@@ -2,7 +2,7 @@
const { permissions } = require('../../../lib/auth');
module.exports = function setupResolver (services) {
module.exports = function setupResolver (services, res) {
const { PermisoService } = services;
return {
......@@ -10,14 +10,14 @@ module.exports = function setupResolver (services) {
permisos: async (_, args, context) => {
permissions(context, 'permisos:read');
let lista = await PermisoService.findAll(args);
return lista.data;
let items = await PermisoService.findAll(args);
return res(items);
},
permiso: async (_, args, context) => {
permissions(context, 'permisos:read');
let item = await PermisoService.findById(args.id);
return item.data;
return res(item);
}
},
Mutation: {
......@@ -26,7 +26,7 @@ module.exports = function setupResolver (services) {
args.permiso._user_created = context.id_usuario;
let item = await PermisoService.createOrUpdate(args.permiso);
return item.data;
return res(item);
},
permisoEdit: async (_, args, context) => {
permissions(context, 'permisos:update');
......@@ -35,13 +35,13 @@ module.exports = function setupResolver (services) {
args.permiso._updated_at = new Date();
args.permiso.id = args.id;
let item = await PermisoService.createOrUpdate(args.permiso);
return item.data;
return res(item);
},
permisoDelete: async (_, args, context) => {
permissions(context, 'permisos:delete');
let deleted = await PermisoService.deleteItem(args.id);
return { deleted: deleted.data };
return { deleted: res(deleted) };
}
}
};
......
'use strict';
const { permissions } = require('../../../lib/auth');
module.exports = function setupResolver (services) {
module.exports = function setupResolver (services, res) {
const { RolService } = services;
return {
......@@ -10,13 +10,13 @@ module.exports = function setupResolver (services) {
permissions(context, 'roles:read|usuarios:read');
let items = await RolService.findAll(args, context.rol);
return items.data;
return res(items);
},
rol: async (_, args, context) => {
permissions(context, 'roles:read');
let items = await RolService.findById(args.id);
return items.data;
return res(items);
}
},
Mutation: {
......@@ -25,7 +25,7 @@ module.exports = function setupResolver (services) {
args.rol._user_created = context.id_usuario;
let item = await RolService.createOrUpdate(args.rol);
return item.data;
return res(item);
},
rolEdit: async (_, args, context) => {
permissions(context, 'roles:update');
......@@ -34,13 +34,13 @@ module.exports = function setupResolver (services) {
args.rol._updated_at = new Date();
args.rol.id = args.id;
let item = await RolService.createOrUpdate(args.rol);
return item.data;
return res(item);
},
rolDelete: async (_, args, context) => {
permissions(context, 'roles:delete');
let deleted = await RolService.deleteItem(args.id);
return { deleted: deleted.data };
return { deleted: res(deleted) };
}
}
};
......
......@@ -2,7 +2,7 @@
const { permissions } = require('../../../lib/auth');
module.exports = function setupResolver (services) {
module.exports = function setupResolver (services, res) {
const { UsuarioService } = services;
return {
......@@ -10,18 +10,18 @@ module.exports = function setupResolver (services) {
usuarios: async (_, args, context) => {
permissions(context, 'usuarios:read');
let lista = await UsuarioService.findAll(args, context.rol, context.id_entidad);
return lista.data;
let items = await UsuarioService.findAll(args, context.rol, context.id_entidad);
return res(items);
},
usuario: async (_, args, context) => {
permissions(context, 'usuarios:read');
let item = await UsuarioService.findById(args.id);
return item.data;
return res(item);
},
usuarioOnlyToken: async (_, args, context) => {
let item = await UsuarioService.findById(args.id);
return item.data;
return res(item);
}
},
Mutation: {
......@@ -30,7 +30,7 @@ module.exports = function setupResolver (services) {
args.usuario._user_created = context.id_usuario;
let item = await UsuarioService.createOrUpdate(args.usuario, context.rol, context.id_entidad);
return item.data;
return res(item);
},
usuarioEdit: async (_, args, context) => {
permissions(context, 'usuarios:update');
......@@ -39,7 +39,7 @@ module.exports = function setupResolver (services) {
args.usuario._updated_at = new Date();
args.usuario.id = args.id;
let item = await UsuarioService.createOrUpdate(args.usuario);
return item.data;
return res(item);
},
usuarioUpdate: async (_, args, context) => {
permissions(context, 'usuarios:update');
......@@ -48,13 +48,13 @@ module.exports = function setupResolver (services) {
args.usuario._updated_at = new Date();
args.usuario.id = args.id;
let item = await UsuarioService.update(args.usuario);
return item.data;
return res(item);
},
usuarioDelete: async (_, args, context) => {
permissions(context, 'usuarios:delete');
let deleted = await UsuarioService.deleteItem(args.id);
return { deleted: deleted.data };
return { deleted: res(deleted) };
}
}
};
......
......@@ -4,6 +4,14 @@ const fs = require('fs');
const path = require('path');
const Duplex = require('stream').Duplex;
function response (data) {
if (data.code === -1) {
throw new Error(data.message);
} else {
return data.data;
}
}
function loadGraphqlFile (PATH, opts = {}, services) {
let files = fs.readdirSync(PATH);
......@@ -42,7 +50,7 @@ function loadGraphqlFile (PATH, opts = {}, services) {
} else if (file.lastIndexOf('Mutation') !== -1) {
mutations += fs.readFileSync(pathFile, 'utf8');
} else if (file.lastIndexOf('Resolver') !== -1) {
const data = require(pathFile)(services);
const data = require(pathFile)(services, response);
if (data.Query) {
resolvers.Query = Object.assign(resolvers.Query, data.Query);
}
......@@ -228,5 +236,6 @@ module.exports = {
bufferToStream,
createObject,
loadGraphqlFile,
loadControllers
loadControllers,
response
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment