升级Expo SDK 52后出现TypeError: Cannot read property 'add' of undefined错误
Expo SDK升级后Wundergraph Mutation出现TypeError: Cannot read property 'add' of undefined
问题背景
- 技术栈:Expo React Native + Wundergraph(通过
templates.typescript.client生成客户端) - 原依赖版本:
@wundergraph/metro-config@^0.0.1、@wundergraph/sdk@^0.184.2、@wundergraph/swr@^0.19.3、swr@^2.2.5 - 现象:Expo SDK 51下应用运行正常;执行
npm install expo@latest+npx expo install --fix升级SDK及依赖后,部分mutation触发TypeError: Cannot read property 'add' of undefined;回退至SDK 51及旧依赖后恢复正常,可通过devclient进行开发构建测试。
排查与解决方案
1. 升级Wundergraph依赖至兼容版本
当前使用的Wundergraph相关依赖(尤其是@wundergraph/metro-config@^0.0.1)可能未适配Expo SDK 52+的运行时变化。尝试升级所有Wundergraph依赖到最新版本:
npm install @wundergraph/metro-config@latest @wundergraph/sdk@latest @wundergraph/swr@latest
升级完成后重新生成客户端代码,确保模板与新依赖匹配:
npx wundergraph generate
2. 检查Metro配置适配性
Expo SDK 52+对Metro配置有调整,旧版@wundergraph/metro-config可能存在集成冲突。确认项目metro.config.js配置正确:
const { getDefaultConfig } = require('expo/metro-config'); const { withWunderGraph } = require('@wundergraph/metro-config'); const config = getDefaultConfig(__dirname); module.exports = withWunderGraph(config);
3. 调试Mutation调用逻辑
触发错误的mutation可能存在实例初始化问题,在调用前添加日志确认客户端实例状态:
// 在mutation调用代码前添加调试日志 console.log('Wundergraph client instance:', client); const result = await client.mutations.yourMutationName({ /* 参数 */ });
排查是否存在依赖旧版Expo运行时的全局API被移除,导致Wundergraph客户端内部调用失败。
4. 锁定依赖逐步排查
若暂时无法完全适配,可锁定Wundergraph依赖版本并升级Expo SDK,逐步定位冲突点:
"dependencies": { "expo": "^52.0.0", "@wundergraph/metro-config": "^0.0.1", "@wundergraph/sdk": "^0.184.2", "@wundergraph/swr": "^0.19.3", "swr": "^2.2.5" }
执行npm install后,通过npx expo start --dev-client测试,确认是否仅特定mutation触发错误,进一步定位问题代码。
内容的提问来源于stack exchange,提问作者aswinraj vm
相关产品推荐
相关产品推荐

