Skip to content

Commit 496c1a0

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 496c1a0

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

src/jsutils/instanceOf.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,35 @@ 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
30-
Ensure that there is only one instance of "graphql" in the node_modules
31-
directory. If different versions of "graphql" are the dependencies of other
32-
relied on modules, use "resolutions" to ensure only one version is installed.
32+
Ensure that there is only one instance of "graphql" in the node_modules
33+
directory. If different versions of "graphql" are the dependencies of other
34+
relied on modules, use "resolutions" to ensure only one version is installed.
3335
34-
https://yarnpkg.com/en/docs/selective-version-resolutions
36+
https://yarnpkg.com/en/docs/selective-version-resolutions
3537
36-
Duplicate "graphql" modules cannot be used at the same time since different
37-
versions may have different capabilities and behavior. The data from one
38-
version used in the function from another could produce confusing and
39-
spurious results.`,
40-
);
38+
Duplicate "graphql" modules cannot be used at the same time since different
39+
versions may have different capabilities and behavior. The data from one
40+
version used in the function from another could produce confusing and
41+
spurious results.`,
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)