diff --git a/src/jsutils/__tests__/instanceOf-test.js b/src/jsutils/__tests__/instanceOf-test.js index 17a8d4e46d..dfa364c9ce 100644 --- a/src/jsutils/__tests__/instanceOf-test.js +++ b/src/jsutils/__tests__/instanceOf-test.js @@ -1,7 +1,7 @@ import { expect } from 'chai'; import { describe, it } from 'mocha'; -import instanceOf from '../instanceOf'; +import { instanceOf } from '../instanceOf'; describe('instanceOf', () => { it('fails with descriptive error message', () => { diff --git a/src/jsutils/instanceOf.js b/src/jsutils/instanceOf.js index 01909fbe6f..bdecd4cdfb 100644 --- a/src/jsutils/instanceOf.js +++ b/src/jsutils/instanceOf.js @@ -1,32 +1,25 @@ /** * A replacement for instanceof which includes an error warning when multi-realm * constructors are detected. + * See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production + * See: https://webpack.js.org/guides/production/ */ -declare function instanceOf( - value: mixed, - constructor: mixed, -): boolean %checks(value instanceof constructor); - -// See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production -// See: https://webpack.js.org/guides/production/ -// eslint-disable-next-line import/no-default-export -export default process.env.NODE_ENV === 'production' - ? // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317') - // eslint-disable-next-line no-shadow - function instanceOf(value: mixed, constructor: mixed): boolean { - return value instanceof constructor; - } - : // eslint-disable-next-line no-shadow - function instanceOf(value: any, constructor: any): boolean { - if (value instanceof constructor) { - return true; +export const instanceOf: (mixed, mixed) => boolean = + process.env.NODE_ENV === 'production' + ? // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317') + function instanceOf(value: mixed, constructor: mixed): boolean { + return value instanceof constructor; } - if (value) { - const valueClass = value.constructor; - const className = constructor.name; - if (className && valueClass && valueClass.name === className) { - throw new Error( - `Cannot use ${className} "${value}" from another module or realm. + : function instanceOf(value: any, constructor: any): boolean { + if (value instanceof constructor) { + return true; + } + if (value) { + const valueClass = value.constructor; + const className = constructor.name; + if (className && valueClass && valueClass.name === className) { + throw new Error( + `Cannot use ${className} "${value}" from another module or realm. Ensure that there is only one instance of "graphql" in the node_modules directory. If different versions of "graphql" are the dependencies of other @@ -38,8 +31,8 @@ Duplicate "graphql" modules cannot be used at the same time since different versions may have different capabilities and behavior. The data from one version used in the function from another could produce confusing and spurious results.`, - ); + ); + } } - } - return false; - }; + return false; + }; diff --git a/src/language/source.js b/src/language/source.js index 082c872005..dd4837ce06 100644 --- a/src/language/source.js +++ b/src/language/source.js @@ -1,6 +1,6 @@ import { inspect } from '../jsutils/inspect'; import { devAssert } from '../jsutils/devAssert'; -import instanceOf from '../jsutils/instanceOf'; +import { instanceOf } from '../jsutils/instanceOf'; type Location = {| line: number, diff --git a/src/type/definition.js b/src/type/definition.js index 02fccdd82d..fa37748347 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -11,7 +11,7 @@ import { mapValue } from '../jsutils/mapValue'; import { toObjMap } from '../jsutils/toObjMap'; import { devAssert } from '../jsutils/devAssert'; import { keyValMap } from '../jsutils/keyValMap'; -import instanceOf from '../jsutils/instanceOf'; +import { instanceOf } from '../jsutils/instanceOf'; import { didYouMean } from '../jsutils/didYouMean'; import { isObjectLike } from '../jsutils/isObjectLike'; import { identityFunc } from '../jsutils/identityFunc'; diff --git a/src/type/directives.js b/src/type/directives.js index 437e54132b..cbe32751a4 100644 --- a/src/type/directives.js +++ b/src/type/directives.js @@ -2,7 +2,7 @@ import type { ReadOnlyObjMap, ReadOnlyObjMapLike } from '../jsutils/ObjMap'; import { inspect } from '../jsutils/inspect'; import { toObjMap } from '../jsutils/toObjMap'; import { devAssert } from '../jsutils/devAssert'; -import instanceOf from '../jsutils/instanceOf'; +import { instanceOf } from '../jsutils/instanceOf'; import { isObjectLike } from '../jsutils/isObjectLike'; import type { DirectiveDefinitionNode } from '../language/ast'; diff --git a/src/type/schema.js b/src/type/schema.js index 326ddc9ee5..5b2e834865 100644 --- a/src/type/schema.js +++ b/src/type/schema.js @@ -6,7 +6,7 @@ import type { import { inspect } from '../jsutils/inspect'; import { toObjMap } from '../jsutils/toObjMap'; import { devAssert } from '../jsutils/devAssert'; -import instanceOf from '../jsutils/instanceOf'; +import { instanceOf } from '../jsutils/instanceOf'; import { isObjectLike } from '../jsutils/isObjectLike'; import type { GraphQLError } from '../error/GraphQLError';