我正在使用Amplify / AppSync创建一个应用程序,希望该应用程序能够在发生任何事件时刷新视频列表。
这是我第一次使用GraphQL,所以我可能会遗漏一些东西,我已经在Google上搜索了类似的问题,其中大多数实际上都没有答案。 我正在使用轻量级的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 />
,而是在useEffect调用上使用subscribe
方法(针对componentWillMount
事件),如显示的here一样,没有进行任何操作差异。
知道为什么会发生吗?
PS:我也想在Connect上集成更多订阅,但是我找不到关于是否可能的任何信息,这里只有一个问题,到目前为止没有答案。