From 004e8d5fe9d78cc47ca79573f761e42d27a7511e Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Fri, 31 Mar 2017 12:37:11 -0300 Subject: [PATCH 1/4] Using wrapResponse --- graph/helpers/response.js | 31 +++++++++++++++++++ graph/resolvers/root_mutation.js | 29 +---------------- .../client/components/Icon.js | 2 +- plugins/coral-plugin-respect/index.js | 3 +- 4 files changed, 35 insertions(+), 30 deletions(-) create mode 100644 graph/helpers/response.js diff --git a/graph/helpers/response.js b/graph/helpers/response.js new file mode 100644 index 000000000..ebdbc02ec --- /dev/null +++ b/graph/helpers/response.js @@ -0,0 +1,31 @@ +const errors = require('../../errors'); +const {Error: {ValidationError}} = require('mongoose'); + +/** + * Wraps up a promise to return an object with the resolution of the promise + * keyed at `key` or an error caught at `errors`. + */ + +const wrapResponse = (key) => (promise) => { + return promise.then((value) => { + let res = {}; + if (key) { + res[key] = value; + } + return res; + }).catch((err) => { + if (err instanceof errors.APIError) { + return { + errors: [err] + }; + } else if (err instanceof ValidationError) { + + // TODO: wrap this with one of our internal errors. + throw err; + } + + throw err; + }); +}; + +module.exports = wrapResponse; diff --git a/graph/resolvers/root_mutation.js b/graph/resolvers/root_mutation.js index a474407d2..3d5ea9ad9 100644 --- a/graph/resolvers/root_mutation.js +++ b/graph/resolvers/root_mutation.js @@ -1,33 +1,6 @@ -const {Error: {ValidationError}} = require('mongoose'); -const errors = require('../../errors'); +const wrapResponse = require('../helpers/response'); const CommentsService = require('../../services/comments'); -/** - * Wraps up a promise to return an object with the resolution of the promise - * keyed at `key` or an error caught at `errors`. - */ -const wrapResponse = (key) => (promise) => { - return promise.then((value) => { - let res = {}; - if (key) { - res[key] = value; - } - return res; - }).catch((err) => { - if (err instanceof errors.APIError) { - return { - errors: [err] - }; - } else if (err instanceof ValidationError) { - - // TODO: wrap this with one of our internal errors. - throw err; - } - - throw err; - }); -}; - const RootMutation = { createComment(_, {asset_id, parent_id, body}, {mutators: {Comment}}) { return wrapResponse('comment')(Comment.create({asset_id, parent_id, body})); diff --git a/plugins/coral-plugin-respect/client/components/Icon.js b/plugins/coral-plugin-respect/client/components/Icon.js index 8a39218dc..1e4dd30b9 100644 --- a/plugins/coral-plugin-respect/client/components/Icon.js +++ b/plugins/coral-plugin-respect/client/components/Icon.js @@ -1,5 +1,5 @@ import React from 'react'; export default () => ( - +