调用Amplify DataStore.start()报Schema未初始化错误
问题描述
开发集成AWS AppSync GraphQL服务的应用时,每次执行DataStore.start()都会抛出如下报错:
[ERROR] DataStore - Schema is not initialized. DataStore will not function as expected. This could happen if you have multiple versions of DataStore installed.
已尝试以下排查手段均未解决问题:
- 从零初始化全新项目,报错仍然复现
- 删除
node_modules文件夹后重新安装全部依赖,问题无改善 - 暂未找到定位错误根因的方法
相关代码与配置
初始化逻辑代码
import Amplify, { API, Auth, DataStore, Hub, Storage } from 'aws-amplify'; import awsmobile from './src/aws-exports'; import awsapigateway from './src/aws-api-gateway'; Amplify.Logger.LOG_LEVEL = 'DEBUG'; Amplify.configure(awsmobile); API.configure(awsapigateway); DataStore.start() .then(() => console.log('DataStore started')) .catch(e => console.log('Error init datastore: ', e)) .finally(() => console.log('DataStore finally'));
package.json 依赖配置
{ "dependencies": { "@babel/plugin-proposal-nullish-coalescing-operator": "^7.16.7", "@babel/plugin-proposal-optional-chaining": "^7.16.7", "@babel/plugin-syntax-flow": "^7.14.5", "@babel/plugin-transform-react-jsx": "^7.14.9", "@react-native-async-storage/async-storage": "^1.17.7", "@react-native-community/geolocation": "^2.0.2", "@react-native-community/netinfo": "^9.3.0", "@react-native-picker/picker": "^2.4.0", "@react-navigation/bottom-tabs": "^6.2.0", "@react-navigation/native": "^6.0.8", "@react-navigation/stack": "^6.1.1", "amazon-cognito-identity-js": "^5.2.8", "aws-amplify": "^4.3.26", "graphql": "16.5.0", "graphql-tools": "^8.3.0", "moment": "^2.29.1", "prettier": "^2.6.0", "prop-types": "^15.8.1", "radio-buttons-react-native": "^1.0.4", "react": "^17.0.2", "react-native": "^0.68.2", "react-native-android-location-services-dialog-box": "^2.8.2", "react-native-bootsplash": "^4.1.4", "react-native-debugger": "^1.1.0", "react-native-elements": "^3.4.2", "react-native-fs": "^2.19.0", "react-native-gesture-handler": "2.5.0", "react-native-get-random-values": "^1.7.2", "react-native-image-pan-zoom": "^2.1.12", "react-native-linear-gradient": "^2.5.6", "react-native-loading-spinner-overlay": "^3.0.0", "react-native-permissions": "^3.3.1", "react-native-pressable-opacity": "^1.0.8", "react-native-reanimated": "^2.8.0", "react-native-safe-area-context": "^3.1.9", "react-native-screens": ">= 3.0.0", "react-native-sqlite-storage": "^6.0.1", "react-native-static-safe-area-insets": "^2.1.1", "react-native-vector-icons": "^9.1.0", "react-native-vision-camera": "^2.13.3", "typescript": "^4.6.2", "uuid": "^8.3.2", "vision-camera-code-scanner": "^0.2.0" }, "devDependencies": { "@babel/core": "7.18.6", "@babel/preset-env": "7.18.6", "@babel/runtime": "^7.17.8", "@react-native-community/eslint-config": "^2.0.0", "@react-native-community/eslint-plugin": "^1.1.0", "babel-eslint": "^10.1.0", "babel-jest": "^28.1.1", "eslint": "^7.32.0", "eslint-config-airbnb": "^19.0.4", "eslint-config-prettier": "^8.5.0", "eslint-plugin-import": "^2.25.4", "eslint-plugin-jsx-a11y": "^6.5.1", "eslint-plugin-react": "^7.29.4", "eslint-plugin-react-hooks": "^4.3.0", "ini": "^1.3.5", "inquirer": "^6.5.1", "jest": "^28.1.1", "metro-react-native-babel-preset": "^0.71.2", "react-test-renderer": "^17.0.2" } }
schema.graphql 定义
type Subpoena @model @auth(rules: [{allow: public}]) { id: id! status: SyncStatus! localImageStorage: AWSJSON } enum SyncStatus { PENDING DONATE ERROR }
根因与解决方案
根因
项目中安装的graphql@16.5.0版本和aws-amplify@4.3.26存在兼容冲突,会导致DataStore加载GraphQL schema时解析失败,抛出schema未初始化的报错,和多版本DataStore安装无直接关系。
修复步骤
- 卸载当前高版本graphql依赖,安装Amplify 4.x系列兼容的15.x稳定版本:
npm uninstall graphql npm install graphql@15.8.0
- 检查依赖树中是否存在多版本DataStore包:
npm ls @aws-amplify/datastore
如果输出结果存在多个版本的@aws-amplify/datastore,在package.json中添加resolutions字段锁定版本(npm 8.3+、yarn原生支持该配置):
"resolutions": { "@aws-amplify/datastore": "4.3.10" }
- 删除
node_modules目录、依赖锁文件(package-lock.json或yarn.lock)后重新安装依赖,执行amplify codegen models重新生成数据模型文件,重启项目即可正常初始化DataStore。
注意:当前代码中Amplify配置顺序正确,无需调整,不要在Amplify.configure()执行完成前调用DataStore相关方法。
内容的提问来源于stack exchange,提问作者Jonathan Michels
相关产品推荐
相关产品推荐

