You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

instanceof判断异常:graphql-modules测试环境问题排查求助

graphql-modules中isScalarResolver的instanceof判断在测试环境失效问题分析

问题场景

使用TypeScript开发GraphQL API,依赖graphql-modules库时遇到异常:

  • 库内的isScalarResolver函数通过instanceof GraphQLScalarType判断对象类型
  • 执行pnpm run test时,明确是GraphQLScalarType实例的对象,判断结果返回false
  • 执行pnpm run dev时,相同判断逻辑返回true,结果正常

相关代码

isScalarResolver函数代码:

function isScalarResolver(obj) {
    console.log('obj:', obj, '\nisScalarResolver(obj):', obj instanceof GraphQLScalarType, '\n');
    return obj instanceof GraphQLScalarType;
}

复现步骤

  1. 搭建基于graphql-modules@2.1.2、graphql@16.6.0的TypeScript项目
  2. 找到node_modules目录下graphql-modules的index.mjs文件,定位到1859行的isScalarResolver函数
  3. 为函数添加上述日志代码
  4. 执行pnpm run test,观察到异常输出;执行pnpm run dev验证正常情况

问题根源

经调试确认,该问题属于dual package hazard(双包风险):测试环境中,GraphQLScalarType类被分别从CJS和ESM两种不同模块格式的文件中加载,导致内存中存在两个独立的类构造函数。由于instanceof判断依赖于构造函数的引用一致性,因此会出现明明是实例却判断为false的情况。这类问题在graphql-js项目中已被多次报告。

内容的提问来源于stack exchange,提问作者devatina11yb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 16:47:15