我正在使用 Amplify/AppSync 创建一个应用程序,我希望该应用程序能够在发生时刷新视频列表。
这是我第一次使用 GraphQL,所以我可能遗漏了一些东西,我已经在谷歌上搜索过类似的问题,但其中大多数实际上都没有答案。我正在使用轻型 AWS Amplify GraphQL 客户端库,想知道它是否值得迁移到 AppSync/Apollo,但是,文档指出,如果您不需要缓存/离线功能,则不需要
我在 AppSync 上创建了我的应用程序,然后使用生成了我的查询/突变/订阅amplify codegen
我的订阅模式:
type Subscription {
onCreateVideoOnDemand(
guid: String,
thumbNailUrl: [String],
startTime: String,
workflowStatus: String,
srcVideo: String
): VideoOnDemand
@aws_subscribe(mutations: ["createVideoOnDemand"])
onUpdateVideoOnDemand(
guid: String,
thumbNailUrl: [String],
startTime: String,
workflowStatus: String,
srcVideo: String
): VideoOnDemand
@aws_subscribe(mutations: ["updateVideoOnDemand"])
onDeleteVideoOnDemand(
guid: String,
thumbNailUrl: [String],
startTime: String,
workflowStatus: String,
srcVideo: String
): VideoOnDemand
@aws_subscribe(mutations: ["deleteVideoOnDemand"])
}
然后在我的代码上,我执行以下操作:
import { Connect } from 'aws-amplify-react';
import { listVideoOnDemands } from './graphql/queries';
import { onCreateVideoOnDemand } from './graphql/subscriptions';
...
<Connect
query={graphqlOperation(listVideoOnDemands)}
subscription={graphqlOperation(onCreateVideoOnDemand)}
onSubscriptionMsg={(prev, { onCreateVideoOnDemand }) => {
prev.listVideoOnDemands.items.push(onCreateVideoOnDemand);
console.log(prev.listVideoOnDemands.items);
return prev;
}}
>
{({ data: { listVideoOnDemands }, loading, error }) => {
if (loading) return "Loading"
if (error) return "Error"
return <List data={listVideoOnDemands.items} />
</Connect>
该列表正在正确显示,除了当我在数据库上创建新项目时它不会更新(当然,如果我手动刷新页面它会起作用)。要在数据库上创建一个新项目,我将转到 AWS AppSync 控制台 -> 查询 -> 然后在那里运行 createVideoOnDemand ...该行已成功添加到 DynamoDB 表中,但是,它不会触发我的事件代码,该行console.log(prev.listVideoOnDemands.items);
永远不会被调用。
我也尝试不使用<Connect />
,而是使用subscribe
useEffect 调用上的方法(对于componentWillMount
事件),就像这里显示的那样,没有任何区别。
知道为什么会这样吗?
PS:我也想在Connect上整合更多的订阅,但是我找不到任何关于是否可以的信息,这里只有一个问题,目前没有答案。