Skip to content

Commit

Permalink
Remove instanceof check
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Feb 16, 2025
1 parent 72c9044 commit bf8ebc5
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 52 deletions.
5 changes: 4 additions & 1 deletion .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"*.{js,ts}": ["eslint --max-warnings 0 --fix", "prettier --write"],
"*.{js,ts}": [
"eslint --max-warnings 0 --fix --no-warn-ignored",
"prettier --write"
],
"*.json": "prettier --write",
"*.md": "prettier --write"
}
3 changes: 1 addition & 2 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export default tsConfig(
'npmDist',
'npmEsmDist',
'denoDist',
'website/.next',
'website/out',
'website/',
'integrationTests/ts/*.ts',
],
},
Expand Down
4 changes: 2 additions & 2 deletions src/jsutils/__tests__/instanceOf-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ describe('instanceOf', () => {
const Foo2 = getFoo();

expect(() => instanceOf(new Foo1(), Foo2)).to.throw(
/^Cannot use Foo "{}" from another module or realm./m,
/Cannot use Foo from another module or realm/m,
);
expect(() => instanceOf(new Foo2(), Foo1)).to.throw(
/^Cannot use Foo "{}" from another module or realm./m,
/Cannot use Foo from another module or realm/m,
);
});
});
66 changes: 19 additions & 47 deletions src/jsutils/instanceOf.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,28 @@
import { inspect } from './inspect.js';

/* c8 ignore next 3 */
const isProduction =
globalThis.process != null &&
// eslint-disable-next-line no-undef
process.env.NODE_ENV === 'production';

/**
* 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/
*/
export const instanceOf: (value: unknown, constructor: Constructor) => boolean =
/* c8 ignore next 6 */
// FIXME: https://github.com/graphql/graphql-js/issues/2317
isProduction
? function instanceOf(value: unknown, constructor: Constructor): boolean {
return value instanceof constructor;
}
: function instanceOf(value: unknown, constructor: Constructor): boolean {
if (value instanceof constructor) {
return true;
}
if (typeof value === 'object' && value !== null) {
// Prefer Symbol.toStringTag since it is immune to minification.
const className = constructor.prototype[Symbol.toStringTag];
const valueClassName =
// We still need to support constructor's name to detect conflicts with older versions of this library.
Symbol.toStringTag in value
? value[Symbol.toStringTag]
: value.constructor?.name;
if (className === valueClassName) {
const stringifiedValue = inspect(value);
throw new Error(
`Cannot use ${className} "${stringifiedValue}" 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
relied on modules, use "resolutions" to ensure only one version is installed.
https://yarnpkg.com/en/docs/selective-version-resolutions
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;
};
export function instanceOf(value: unknown, constructor: Constructor): boolean {
if (value instanceof constructor) {
return true;
}
if (typeof value === 'object' && value !== null) {
const className = constructor.prototype[Symbol.toStringTag];
const valueClassName =
// We still need to support constructor's name to detect conflicts with older versions of this library.
Symbol.toStringTag in value
? value[Symbol.toStringTag]
: value.constructor?.name;
if (className === valueClassName) {
throw new Error(
`Multiple GraphQL instances detected, Cannot use ${className} from another module or realm. Read more at https://graphql-js.org/errors#1`,
);
}
}
return false;
}

interface Constructor {
prototype: {
Expand Down
1 change: 1 addition & 0 deletions website/pages/_meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const meta = {
title: 'FAQ',
},
'going-to-production': '',
errors: '',
'api-v16': {
type: 'menu',
title: 'API',
Expand Down
14 changes: 14 additions & 0 deletions website/pages/errors.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Errors

## 1

Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed.

https://yarnpkg.com/en/docs/selective-version-resolutions

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.

0 comments on commit bf8ebc5

Please sign in to comment.