Skip to content

Commit

Permalink
Don't use Symbol if you can't
Browse files Browse the repository at this point in the history
  • Loading branch information
freiksenet committed Feb 12, 2018
1 parent a97e6b3 commit 3bf3aa9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Make iterall a runtime dependency [PR #627](https://github.com/apollographql/graphql-tools/pull/627)
* Added support for lexical parser options [PR #567](https://github.com/apollographql/graphql-tools/pull/567)
* Support `graphql@^0.13.0` [PR #567](https://github.com/apollographql/graphql-tools/pull/567)
* Don't use `Symbol` in incompatible envs [Issue #535](https://github.com/apollographql/graphql-tools/issues/535) [PR #631](https://github.com/apollographql/graphql-tools/pull/631)

### v2.20.2

Expand Down
26 changes: 18 additions & 8 deletions src/stitching/errors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { GraphQLResolveInfo, responsePathAsArray } from 'graphql';
import { locatedError } from 'graphql/error';

const ERROR_SYMBOL = Symbol('subSchemaErrors');
let ERROR_SYMBOL: any;
if (
(typeof global !== 'undefined' && 'Symbol' in global) ||
(typeof window !== undefined && 'Symbol' in window)
) {
ERROR_SYMBOL = Symbol('subSchemaErrors');
} else {
ERROR_SYMBOL = '@@__subSchemaErrors';
}

export function annotateWithChildrenErrors(
object: any,
Expand All @@ -11,7 +19,9 @@ export function annotateWithChildrenErrors(
if (Array.isArray(object)) {
const byIndex = {};
childrenErrors.forEach(error => {
if (!error.path) { return; }
if (!error.path) {
return;
}
const index = error.path[1];
const current = byIndex[index] || [];
current.push({
Expand All @@ -28,7 +38,7 @@ export function annotateWithChildrenErrors(
...object,
[ERROR_SYMBOL]: childrenErrors.map(error => ({
...error,
...(error.path ? { path: error.path.slice(1) } : {}),
...error.path ? { path: error.path.slice(1) } : {},
})),
};
}
Expand Down Expand Up @@ -81,8 +91,10 @@ export function checkResultAndHandleErrors(
// apollo-link-http & http-link-dataloader need the
// result property to be passed through for better error handling.
// If there is only one error, which contains a result property, pass the error through
const newError = result.errors.length === 1 && hasResult(result.errors[0])
? result.errors[0] : new Error(concatErrors(result.errors));
const newError =
result.errors.length === 1 && hasResult(result.errors[0])
? result.errors[0]
: new Error(concatErrors(result.errors));

throw locatedError(
newError,
Expand All @@ -102,9 +114,7 @@ export function checkResultAndHandleErrors(
}

function concatErrors(errors: Error[]) {
return errors
.map(error => error.message)
.join('\n');
return errors.map(error => error.message).join('\n');
}

function hasResult(error: any) {
Expand Down

0 comments on commit 3bf3aa9

Please sign in to comment.