instanceOf.js.flow 1.7 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. ? // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
  14. // eslint-disable-next-line no-shadow
  15. function instanceOf(value: mixed, constructor: mixed): boolean {
  16. return value instanceof constructor;
  17. }
  18. : // eslint-disable-next-line no-shadow
  19. function instanceOf(value: any, constructor: any): boolean {
  20. if (value instanceof constructor) {
  21. return true;
  22. }
  23. if (value) {
  24. const valueClass = value.constructor;
  25. const className = constructor.name;
  26. if (className && valueClass && valueClass.name === className) {
  27. throw new Error(
  28. `Cannot use ${className} "${value}" from another module or realm.
  29. Ensure that there is only one instance of "graphql" in the node_modules
  30. directory. If different versions of "graphql" are the dependencies of other
  31. relied on modules, use "resolutions" to ensure only one version is installed.
  32. https://yarnpkg.com/en/docs/selective-version-resolutions
  33. Duplicate "graphql" modules cannot be used at the same time since different
  34. versions may have different capabilities and behavior. The data from one
  35. version used in the function from another could produce confusing and
  36. spurious results.`,
  37. );
  38. }
  39. }
  40. return false;
  41. };