Skip to content

Commit 1dbe0d4

Browse files
committed
Be selective about instanceof
1 parent 7fdd84e commit 1dbe0d4

File tree

3 files changed

+37
-39
lines changed

3 files changed

+37
-39
lines changed

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
// The GraphQL.js version info.
3030
export { version, versionInfo } from './version.js';
31+
export { setEnv } from './utilities/env.js';
32+
export type { Env } from './utilities/env.js';
3133

3234
// The primary entry point into fulfilling a GraphQL request.
3335
export type { GraphQLArgs } from './graphql.js';

src/jsutils/instanceOf.ts

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,27 @@
1+
import { isProduction } from '../utilities/env.js';
2+
13
import { inspect } from './inspect.js';
24

3-
/* c8 ignore next 3 */
4-
const isProduction =
5-
globalThis.process != null &&
6-
// eslint-disable-next-line no-undef
7-
process.env.NODE_ENV === 'production';
5+
export function instanceOf(value: unknown, constructor: Constructor): boolean {
6+
if (isProduction()) {
7+
return value instanceof constructor;
8+
}
89

9-
/**
10-
* A replacement for instanceof which includes an error warning when multi-realm
11-
* constructors are detected.
12-
* See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production
13-
* See: https://webpack.js.org/guides/production/
14-
*/
15-
export const instanceOf: (value: unknown, constructor: Constructor) => boolean =
16-
/* c8 ignore next 6 */
17-
// FIXME: https://github.com/graphql/graphql-js/issues/2317
18-
isProduction
19-
? function instanceOf(value: unknown, constructor: Constructor): boolean {
20-
return value instanceof constructor;
21-
}
22-
: function instanceOf(value: unknown, constructor: Constructor): boolean {
23-
if (value instanceof constructor) {
24-
return true;
25-
}
26-
if (typeof value === 'object' && value !== null) {
27-
// Prefer Symbol.toStringTag since it is immune to minification.
28-
const className = constructor.prototype[Symbol.toStringTag];
29-
const valueClassName =
30-
// We still need to support constructor's name to detect conflicts with older versions of this library.
31-
Symbol.toStringTag in value
32-
? value[Symbol.toStringTag]
33-
: value.constructor?.name;
34-
if (className === valueClassName) {
35-
const stringifiedValue = inspect(value);
36-
throw new Error(
37-
`Cannot use ${className} "${stringifiedValue}" from another module or realm.
10+
if (value instanceof constructor) {
11+
return true;
12+
}
13+
if (typeof value === 'object' && value !== null) {
14+
// Prefer Symbol.toStringTag since it is immune to minification.
15+
const className = constructor.prototype[Symbol.toStringTag];
16+
const valueClassName =
17+
// We still need to support constructor's name to detect conflicts with older versions of this library.
18+
Symbol.toStringTag in value
19+
? value[Symbol.toStringTag]
20+
: value.constructor?.name;
21+
if (className === valueClassName) {
22+
const stringifiedValue = inspect(value);
23+
throw new Error(
24+
`Cannot use ${className} "${stringifiedValue}" from another module or realm.
3825
3926
Ensure that there is only one instance of "graphql" in the node_modules
4027
directory. If different versions of "graphql" are the dependencies of other
@@ -46,11 +33,11 @@ Duplicate "graphql" modules cannot be used at the same time since different
4633
versions may have different capabilities and behavior. The data from one
4734
version used in the function from another could produce confusing and
4835
spurious results.`,
49-
);
50-
}
51-
}
52-
return false;
53-
};
36+
);
37+
}
38+
}
39+
return false;
40+
}
5441

5542
interface Constructor {
5643
prototype: {

src/utilities/env.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export type Env = 'production' | 'development';
2+
3+
let env: Env = 'production';
4+
5+
export const setEnv = (newEnv: Env): void => {
6+
env = newEnv;
7+
};
8+
9+
export const isProduction = (): boolean => env === 'production';

0 commit comments

Comments
 (0)