instanceOf.mjs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { inspect } from './inspect.mjs';
  2. /**
  3. * A replacement for instanceof which includes an error warning when multi-realm
  4. * constructors are detected.
  5. * See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production
  6. * See: https://webpack.js.org/guides/production/
  7. */
  8. export const instanceOf =
  9. /* c8 ignore next 6 */
  10. // FIXME: https://github.com/graphql/graphql-js/issues/2317
  11. globalThis.process && globalThis.process.env.NODE_ENV === 'production'
  12. ? function instanceOf(value, constructor) {
  13. return value instanceof constructor;
  14. }
  15. : function instanceOf(value, constructor) {
  16. if (value instanceof constructor) {
  17. return true;
  18. }
  19. if (typeof value === 'object' && value !== null) {
  20. var _value$constructor;
  21. // Prefer Symbol.toStringTag since it is immune to minification.
  22. const className = constructor.prototype[Symbol.toStringTag];
  23. const valueClassName = // We still need to support constructor's name to detect conflicts with older versions of this library.
  24. Symbol.toStringTag in value // @ts-expect-error TS bug see, https://github.com/microsoft/TypeScript/issues/38009
  25. ? value[Symbol.toStringTag]
  26. : (_value$constructor = value.constructor) === null ||
  27. _value$constructor === void 0
  28. ? void 0
  29. : _value$constructor.name;
  30. if (className === valueClassName) {
  31. const stringifiedValue = inspect(value);
  32. throw new Error(`Cannot use ${className} "${stringifiedValue}" from another module or realm.
  33. Ensure that there is only one instance of "graphql" in the node_modules
  34. directory. If different versions of "graphql" are the dependencies of other
  35. relied on modules, use "resolutions" to ensure only one version is installed.
  36. https://yarnpkg.com/en/docs/selective-version-resolutions
  37. Duplicate "graphql" modules cannot be used at the same time since different
  38. versions may have different capabilities and behavior. The data from one
  39. version used in the function from another could produce confusing and
  40. spurious results.`);
  41. }
  42. }
  43. return false;
  44. };