|
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 | | - |
9 | 1 | /** |
10 | 2 | * A replacement for instanceof which includes an error warning when multi-realm |
11 | 3 | * constructors are detected. |
12 | 4 | * See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production |
13 | 5 | * See: https://webpack.js.org/guides/production/ |
14 | 6 | */ |
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, read more at https://graphql-js.org/errors#1', |
| 21 | + ); |
| 22 | + } |
| 23 | + } |
| 24 | + return false; |
| 25 | +} |
54 | 26 |
|
55 | 27 | interface Constructor { |
56 | 28 | prototype: { |
|
0 commit comments