instanceOf.js.flow 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // @flow strict
  2. /**
  3. * A replacement for instanceof which includes an error warning when multi-realm
  4. * constructors are detected.
  5. */
  6. declare function instanceOf(
  7. value: mixed,
  8. constructor: mixed,
  9. ): boolean %checks(value instanceof constructor);
  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. export default process.env.NODE_ENV === 'production'
  13. ? // eslint-disable-next-line no-shadow
  14. function instanceOf(value: mixed, constructor: mixed) {
  15. return value instanceof constructor;
  16. }
  17. : // eslint-disable-next-line no-shadow
  18. function instanceOf(value: any, constructor: any) {
  19. if (value instanceof constructor) {
  20. return true;
  21. }
  22. if (value) {
  23. const valueClass = value.constructor;
  24. const className = constructor.name;
  25. if (className && valueClass && valueClass.name === className) {
  26. throw new Error(
  27. `Cannot use ${className} "${value}" from another module or realm.
  28. Ensure that there is only one instance of "graphql" in the node_modules
  29. directory. If different versions of "graphql" are the dependencies of other
  30. relied on modules, use "resolutions" to ensure only one version is installed.
  31. https://yarnpkg.com/en/docs/selective-version-resolutions
  32. Duplicate "graphql" modules cannot be used at the same time since different
  33. versions may have different capabilities and behavior. The data from one
  34. version used in the function from another could produce confusing and
  35. spurious results.`,
  36. );
  37. }
  38. }
  39. return false;
  40. };