Skip to content

Commit bf8ebc5

Browse files
committed
Remove instanceof check
1 parent 72c9044 commit bf8ebc5

File tree

6 files changed

+41
-52
lines changed

6 files changed

+41
-52
lines changed

.lintstagedrc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
2-
"*.{js,ts}": ["eslint --max-warnings 0 --fix", "prettier --write"],
2+
"*.{js,ts}": [
3+
"eslint --max-warnings 0 --fix --no-warn-ignored",
4+
"prettier --write"
5+
],
36
"*.json": "prettier --write",
47
"*.md": "prettier --write"
58
}

eslint.config.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ export default tsConfig(
2121
'npmDist',
2222
'npmEsmDist',
2323
'denoDist',
24-
'website/.next',
25-
'website/out',
24+
'website/',
2625
'integrationTests/ts/*.ts',
2726
],
2827
},

src/jsutils/__tests__/instanceOf-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ describe('instanceOf', () => {
7070
const Foo2 = getFoo();
7171

7272
expect(() => instanceOf(new Foo1(), Foo2)).to.throw(
73-
/^Cannot use Foo "{}" from another module or realm./m,
73+
/Cannot use Foo from another module or realm/m,
7474
);
7575
expect(() => instanceOf(new Foo2(), Foo1)).to.throw(
76-
/^Cannot use Foo "{}" from another module or realm./m,
76+
/Cannot use Foo from another module or realm/m,
7777
);
7878
});
7979
});

src/jsutils/instanceOf.ts

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,28 @@
1-
import { inspect } from './inspect.js';
2-
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';
8-
91
/**
102
* A replacement for instanceof which includes an error warning when multi-realm
113
* constructors are detected.
124
* See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production
135
* See: https://webpack.js.org/guides/production/
146
*/
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.
38-
39-
Ensure that there is only one instance of "graphql" in the node_modules
40-
directory. If different versions of "graphql" are the dependencies of other
41-
relied on modules, use "resolutions" to ensure only one version is installed.
42-
43-
https://yarnpkg.com/en/docs/selective-version-resolutions
44-
45-
Duplicate "graphql" modules cannot be used at the same time since different
46-
versions may have different capabilities and behavior. The data from one
47-
version used in the function from another could produce confusing and
48-
spurious results.`,
49-
);
50-
}
51-
}
52-
return false;
53-
};
7+
export function instanceOf(value: unknown, constructor: Constructor): boolean {
8+
if (value instanceof constructor) {
9+
return true;
10+
}
11+
if (typeof value === 'object' && value !== null) {
12+
const className = constructor.prototype[Symbol.toStringTag];
13+
const valueClassName =
14+
// We still need to support constructor's name to detect conflicts with older versions of this library.
15+
Symbol.toStringTag in value
16+
? value[Symbol.toStringTag]
17+
: value.constructor?.name;
18+
if (className === valueClassName) {
19+
throw new Error(
20+
`Multiple GraphQL instances detected, Cannot use ${className} from another module or realm. Read more at https://graphql-js.org/errors#1`,
21+
);
22+
}
23+
}
24+
return false;
25+
}
5426

5527
interface Constructor {
5628
prototype: {

website/pages/_meta.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const meta = {
2424
title: 'FAQ',
2525
},
2626
'going-to-production': '',
27+
errors: '',
2728
'api-v16': {
2829
type: 'menu',
2930
title: 'API',

website/pages/errors.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Errors
2+
3+
## 1
4+
5+
Ensure that there is only one instance of "graphql" in the node_modules
6+
directory. If different versions of "graphql" are the dependencies of other
7+
relied on modules, use "resolutions" to ensure only one version is installed.
8+
9+
https://yarnpkg.com/en/docs/selective-version-resolutions
10+
11+
Duplicate "graphql" modules cannot be used at the same time since different
12+
versions may have different capabilities and behavior. The data from one
13+
version used in the function from another could produce confusing and
14+
spurious results.

0 commit comments

Comments
 (0)