我的组件正在调用订阅查询,但由于某种原因未访问订阅解析器:其中的断点从未被激活。然而在客户端我得到 GraphQL 订阅错误:
“订阅必须返回异步迭代。收到:未定义”
这可能是什么原因造成的?
提前感谢所有人提供的任何信息。
订阅查询
const IM_SUBSCRIPTION_QUERY = gql`
subscription getIMsViaSubscription($fromID: String!, $toID: String!){
IMAdded(fromID:$fromID, toID: $toID){
id,
fromID,
toID,
msgText,
myUserData{
id,
name_first,
name_last,
picture_large,
picture_medium,
picture_thumbnail
my_category
}
}
}
`;
解析器
Subscription: {
IMAdded(IMThatWasAdded) {
debugger; <== IS NEVER ACTIVATED
subscribe: withFilter(() => SubscriptionServer.pubsub.asyncIterator(IM_ADDED_CHANNEL), (newIM, args) => {
const callIsFromMsgFoldersComponent = args.toID == 0;
var result;
if (callIsFromMsgFoldersComponent){
result = (newIM.fromID === args.fromID || newIM.toID === args.fromID);
} else {
result = ((newIM.fromID === args.fromID && newIM.toID === args.toID) || (newIM.fromID === args.toID && newIM.toID === args.fromID));
}
return result;
})
零件
const withDataAndSubscription = graphql(GETIMS_QUERY, {
options({toID}) {
console.log(GETIMS_QUERY);
const fromID = Meteor.userId();
return {
fetchPolicy: 'cache-and-network',
variables: {fromID: `${fromID}`, toID: `${toID}`}
};
}
,
props: props => {
debugger;
return {
loading: props.data.loading,
instant_message: props.data.instant_message,
subscribeToMore: props.data.subscribeToMore,
subscribeToNewIMs: params => {
debugger; <==IS ACTIVATED AS EXPECTED
console.log(IM_SUBSCRIPTION_QUERY); <==IS OKAY
const fromID = Meteor.userId();
const toID = params.toID;
return props.data.subscribeToMore({
document: IM_SUBSCRIPTION_QUERY,
variables: {fromID: `${fromID}`, toID: `${toID}`},
updateQuery: (previousResult, {subscriptionData}) => {
if (!subscriptionData.data) {
return previousResult;
}
const newMsg = subscriptionData.data.createIM;
return update(previousResult, {
instant_message: {
$push: [newMsg],
},
});
}
});
}
};
},
})
;