Skip to content

Commit 9b7a292

Browse files
committed
Only trigger "one instance of graphql" error in DEV environments.
This ensures the additional checking for multiple graphql instances only occurs in non-production builds. Not only does this improve the performance for production builds, it also solves a spurious error case for minified builds: Since this additional check compares the `.name` of the constructors as a proxy for determining if two classes are representationally equal, minified builds may result in spurious false-positives if class names are minified to single-character names and those characters are likely to conflict. This doesn't require `process` to exist (defaulting to the non-production mode), but bundlers like Webpack should consider using a stripping transform (see: https://webpack.js.org/guides/production/#specify-the-environment) in production builds if they also use a minifier.
1 parent 674e3c7 commit 9b7a292

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/jsutils/instanceOf.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@ declare function instanceOf(
1616
constructor: mixed,
1717
): boolean %checks(value instanceof constructor);
1818

19-
// eslint-disable-next-line no-redeclare
20-
export default function instanceOf(value, constructor) {
21-
if (value instanceof constructor) {
22-
return true;
23-
}
24-
if (value) {
25-
const valueConstructor = value.constructor;
26-
if (valueConstructor && valueConstructor.name === constructor.name) {
27-
throw new Error(
28-
`Cannot use ${constructor.name} "${value}" from another module or realm.
19+
export default (process && process.env.NODE_ENV !== 'production'
20+
? // eslint-disable-next-line no-shadow
21+
function instanceOf(value: any, constructor: any) {
22+
if (value instanceof constructor) {
23+
return true;
24+
}
25+
if (value) {
26+
const valueClass = value.constructor;
27+
const className = constructor.name;
28+
if (valueClass && valueClass.name === className) {
29+
throw new Error(
30+
`Cannot use ${className} "${value}" from another module or realm.
2931
3032
Ensure that there is only one instance of "graphql" in the node_modules
3133
directory. If different versions of "graphql" are the dependencies of other
@@ -37,8 +39,12 @@ Duplicate "graphql" modules cannot be used at the same time since different
3739
versions may have different capabilities and behavior. The data from one
3840
version used in the function from another could produce confusing and
3941
spurious results.`,
40-
);
42+
);
43+
}
44+
}
45+
return false;
4146
}
42-
}
43-
return false;
44-
}
47+
: // eslint-disable-next-line no-shadow
48+
function instanceOf(value: any, constructor: any) {
49+
return value instanceof constructor;
50+
});

0 commit comments

Comments
 (0)