diff --git a/src/server/graphql/httpGraphQLHandler.js b/src/server/graphql/httpGraphQLHandler.js index 6891aae..705d654 100644 --- a/src/server/graphql/httpGraphQLHandler.js +++ b/src/server/graphql/httpGraphQLHandler.js @@ -1,14 +1,15 @@ import Schema from './rootSchema'; import {graphql} from 'graphql'; +import {prepareClientError} from './models/utils'; export default async (req, res) => { // Check for admin privileges const {query, variables, ...newContext} = req.body; const authToken = req.user || {}; - const context = {authToken, context: newContext}; + const context = {authToken, ...newContext}; const result = await graphql(Schema, query, null, context, variables); - if (result.errors) { - console.log('DEBUG GraphQL Error:', result.errors); - } - res.send(result); + const {error} = prepareClientError(result); + const body = {data: result.data}; + if (error) body.error = error; + res.send(body); }; diff --git a/src/universal/utils/fetching.js b/src/universal/utils/fetching.js index 6a791f9..b2cf68b 100644 --- a/src/universal/utils/fetching.js +++ b/src/universal/utils/fetching.js @@ -32,15 +32,14 @@ export function getJSON(route) { // return Object.assign({}, ...(for (p of fields) {[p]: o[p]})); // } -export const getClientError = errors => { - if (!errors) { - return; +export const getClientError = error => { + if (error) { + if (error.indexOf('{"_error"') === -1) { + return {_error: 'Server query error'}; + } + return JSON.parse(error); } - const error = errors[0].message; - if (!error || error.indexOf('{"_error"') === -1) { - return {_error: 'Server query error'}; - } - return JSON.parse(error); + return null; }; export const prepareGraphQLParams = graphParams => { @@ -63,6 +62,6 @@ export const fetchGraphQL = async graphParams => { body: serializedParams }); const resJSON = await res.json(); - const {data, errors} = resJSON; - return {data, error: getClientError(errors)}; + const {data, error} = resJSON; + return {data, error: getClientError(error)}; };