使用AWS Amplify模块化包时Appsync订阅功能失效求助
将AWS Amplify单体包替换为模块化包后遇到的问题
背景
我有一个React应用,希望将单体包aws-amplify替换为@aws-amplify/<package-name>形式的模块化包。使用单体包时所有功能均正常运行,当前需要用到Auth、带订阅功能的GraphQL API。
导入的模块化包版本
"@aws-amplify/core": "^4.7.2" "@aws-amplify/api": "^4.0.51" "@aws-amplify/auth": "^4.6.4" "@aws-amplify/pubsub": "^4.5.1" // 该包似乎是GraphQL订阅所需
配置代码
我调用了Amplify的register函数(仅导入包无法生效),随后调用core的config函数向已注册模块传入配置:
Amplify.register(Auth) Amplify.register(API) Amplify.register(PubSub) Amplify.configure({ // AUTH aws_cognito_region: region, // (required) - Region where Amazon Cognito project was created aws_user_pools_id: userPoolId, // (optional) - Amazon Cognito User Pool ID aws_user_pools_web_client_id: userPoolClientId, // (optional) - Amazon Cognito App Client ID (App client secret needs to be disabled) aws_cognito_identity_pool_id: identityPoolRef, // (optional) - Amazon Cognito Identity Pool ID aws_mandatory_sign_in: "disabled", // (optional) - Users are not allowed to get the aws credentials unless they are signed in // API aws_appsync_region: region, // (optional) - AWS AppSync region aws_appsync_graphqlEndpoint: graphqlUrl, // (optional) - AWS AppSync endpoint // 更像是默认认证类型,可在单个查询中覆盖,参考创建租户逻辑 aws_appsync_authenticationType: "AMAZON_COGNITO_USER_POOLS", // (optional) - Primary AWS AppSync authentication type // 通常我们只发送不包含用户名及JWT中其他编码信息的访问密钥 // 因此我们自行在请求头中携带ID Token API: { graphql_headers: async () => { try { const session = await Auth.currentSession(); if(session) { return { Authorization: session.getIdToken().getJwtToken(), }; } } catch(e) { console.warn(e) } }, } });
报错信息
PubSub模块尝试实例化MQTT IoT客户端时失败,报错如下:
index.ts:939 Uncaught TypeError: Object prototype may only be an Object or null: undefined at setPrototypeOf (<anonymous>) at extendStatics (index.ts:939:1) at __extends (index.ts:939:1) at AWSIotProvider.ts:23:1 at ./node_modules/@aws-amplify/pubsub/lib-esm/Providers/AWSIotProvider.js (AWSIotProvider.ts:59:1) at options.factory (react refresh:6:1) at __webpack_require__ (bootstrap:24:1) at fn (hot module replacement:62:1) at ./node_modules/@aws-amplify/pubsub/lib-esm/Providers/index.js (constants.ts:100:1) at options.factory (react refresh:6:1)
疑问
我并不需要IoT客户端,同时对以下几点存在疑问:
- AWS配置属性混乱:支持作用域配置,但在模块化包中无法生效(模块会跨属性查找,作用域配置下无法找到对应属性),为何不彻底移除作用域配置?
- 依赖关系未文档化:没有任何说明提到"使用订阅功能需要PubSub包"。
- PubSub实例为何要实例化MQTT IoT客户端?
更新
已在Github提交相关issue。
内容的提问来源于stack exchange,提问作者pfried
相关产品推荐
相关产品推荐

