Skip to content

Commit

Permalink
feat: improve error handling.
Browse files Browse the repository at this point in the history
If things break for you please update your apollo/federation and gateway packages to 0.16.0
  • Loading branch information
lgandecki committed Jun 3, 2020
1 parent deb3c67 commit d578858
Show file tree
Hide file tree
Showing 6 changed files with 1,447 additions and 223 deletions.
295 changes: 295 additions & 0 deletions helpers/executeQueryPlan.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 33 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const {
LocalGraphQLDataSource,
buildOperationContext,
buildQueryPlan,
executeQueryPlan
} = require("@apollo/gateway");
const { addMockFunctionsToSchema } = require("graphql-tools");
const { addResolversToSchema } = require("apollo-graphql");
Expand All @@ -16,6 +15,7 @@ const clone = require("clone");
const gql = require("graphql-tag");
const cloneDeepWith = require("lodash.clonedeepwith");
const isFunction = require("lodash.isfunction");
const { executeQueryPlan } = require("./helpers/executeQueryPlan")

const {
buildContextsPerService
Expand Down Expand Up @@ -215,7 +215,7 @@ function validateArguments(
}
}

const executeGraphql = ({
const executeGraphql = async ({
query,
mutation,
variables,
Expand All @@ -242,15 +242,37 @@ const executeGraphql = ({
addServiceInformationToResolvers(services);
}

return execute(
schema,
query,
mutation,
serviceMap,
variables,
context,
contextsPerService
);

const prepareError = new Error("");
const splitLines = prepareError.stack.split("\n").slice(2);
let result;
try {
result = await execute(
schema,
query,
mutation,
serviceMap,
variables,
context,
contextsPerService
);
if (result.errors) {
if (result.errors.length === 1) {
result.errors[0].message = result.errors[0].message + `, path: ${result.errors[0].path}`
throw result.errors[0];
} else {
throw new Error(result.errors.map((e) => `${e.message}, path: ${e.path}`).join(","));
}
}
} catch (e) {
const smallStack = e.stack.split("\n");
e.stack = [...smallStack, ...splitLines]
.filter((l) => l.indexOf("node_modules") === -1)
.join("\n");
e.message = e.message.split("\n")[0];
throw e;
}
return result;
};

function addServiceInformationToResolvers(services) {
Expand Down
Loading

0 comments on commit d578858

Please sign in to comment.