From 988e4ebb7056c9405aa1dd99e692b85ed6c01389 Mon Sep 17 00:00:00 2001 From: Mikhail Novikov Date: Mon, 12 Feb 2018 15:17:53 +0200 Subject: [PATCH] Don't use Symbol if you can't (#631) --- CHANGELOG.md | 1 + src/stitching/errors.ts | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad7d4168f89..0df90ca4bc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/stitching/errors.ts b/src/stitching/errors.ts index 720345d6e87..3af0dc609e1 100644 --- a/src/stitching/errors.ts +++ b/src/stitching/errors.ts @@ -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, @@ -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({ @@ -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) } : {}, })), }; } @@ -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, @@ -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) {