instanceOf.js 2.3 KB

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