diff --git a/.lintstagedrc.json b/.lintstagedrc.json index 86a7859e7b..ada6d9dcfb 100644 --- a/.lintstagedrc.json +++ b/.lintstagedrc.json @@ -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" } diff --git a/eslint.config.mjs b/eslint.config.mjs index 557d75c22b..4b4b9ec333 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -21,8 +21,7 @@ export default tsConfig( 'npmDist', 'npmEsmDist', 'denoDist', - 'website/.next', - 'website/out', + 'website/', 'integrationTests/ts/*.ts', ], }, diff --git a/src/jsutils/__tests__/instanceOf-test.ts b/src/jsutils/__tests__/instanceOf-test.ts index 5a54a641e5..cd2c3b7075 100644 --- a/src/jsutils/__tests__/instanceOf-test.ts +++ b/src/jsutils/__tests__/instanceOf-test.ts @@ -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, ); }); }); diff --git a/src/jsutils/instanceOf.ts b/src/jsutils/instanceOf.ts index 66811433ae..dc2e750c5f 100644 --- a/src/jsutils/instanceOf.ts +++ b/src/jsutils/instanceOf.ts @@ -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/conflicting-versions`, + ); + } + } + return false; +} interface Constructor { prototype: { diff --git a/website/pages/_meta.ts b/website/pages/_meta.ts index b56f19ce54..d02325237c 100644 --- a/website/pages/_meta.ts +++ b/website/pages/_meta.ts @@ -24,6 +24,7 @@ const meta = { title: 'FAQ', }, 'going-to-production': '', + errors: '', 'api-v16': { type: 'menu', title: 'API', diff --git a/website/pages/errors/conflicting-versions.mdx b/website/pages/errors/conflicting-versions.mdx new file mode 100644 index 0000000000..f681f88e55 --- /dev/null +++ b/website/pages/errors/conflicting-versions.mdx @@ -0,0 +1,12 @@ +# Conflicting GraphQL versions + +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.