[英]Relay Modern - Subscription updater(connection handler) method (no documentation on this matter)
I am trying to perform a subscription for my query. 我正在尝试为查询执行订阅。 So my suspicion is that the connection (from the ConnectionHandler) is not working.
因此,我怀疑连接(来自ConnectionHandler)无法正常工作。 I cannot find any proper documentation on this one.
我找不到与此有关的任何适当文档。
My subscription looks like: 我的订阅如下所示:
const LinkSubscription = graphql`
subscription LinkSubscription {
Link(filter:{
mutation_in:[CREATED]
}){
node{
id
}
}
}
`;
export default () => {
const subscriptionConfig = {
subscription: LinkSubscription,
variables: {},
onCompleted: () => { alert('done!'); },
updater: (Store, data) => {
const newLink = Store.getRootField('Link').getLinkedRecord('node');
const allLinks = Store.getRoot();
const edge = ConnectionHandler.createEdge(Store, allLinks, newLink, 'allLinks');
const userId = localStorage.getItem('user_id');
const connection = ConnectionHandler.getConnection(allLinks, newLink, 'allLinks');
if (connection) {
ConnectionHandler.insertEdgeAfter(connection, newLink);
console.log('DONE');
}
console.log('DEBUG', Store);
console.log('DEBUG2', newLink);
console.log('DEBUG3', allLinks);
console.log('DEBUG4', edge);
console.log('DEBUG5', connection);
console.log('DEBUG6', ConnectionHandler);
console.log('DEBUG7', userId);
console.log('Debug8', data);
},
onError: error => console.log('An error occured:', error),
};
requestSubscription(Environment, subscriptionConfig);
};
As you may see in the code i ran a lot of logs to see what i did wrong. 正如您可能在代码中看到的那样,我运行了很多日志以查看我做错了什么。
Log DEBUG fires: RelayRecordSourceSelectorProxy
, 记录DEBUG触发:
RelayRecordSourceSelectorProxy
,
Log DEBUG2 fires: RelayRecordProxy
// for the specific id(59f88d417fae441eb567c453) CREATED, 日志DEBUG2触发:
RelayRecordProxy
// //创建了特定ID(59f88d417fae441eb567c453),
Log DEBUG3 fires: RelayRecordProxy
// for client:root, 日志DEBUG3触发:
RelayRecordProxy
// //客户端:root,
Log DEBUG4 fires: RelayRecordProxy
// for client:root:59f88d417fae441eb567c453, 日志DEBUG4触发:
RelayRecordProxy
// //客户端:root:59f88d417fae441eb567c453,
Log DEBUG5: undefined
, 日志DEBUG5:
undefined
,
Log DEBUG6: ConnectionHandler
methods, 记录DEBUG6:
ConnectionHandler
方法,
Log DEBUG7: user.id
who requested the query. 登录DEBUG7:
user.id
谁请求的查询。
Question1:Can you please help with some connection suggestions? 问题1:能否请您提供一些连接建议?
Subscriptions: Updating the client on each response 订阅: 在每个响应上更新客户端
const LinkSubscription = graphql`
subscription LinkSubscription {
Link(filter: { mutation_in: [CREATED] }) {
node {
id
}
}
}
`;
export const test = () => {
const subscriptionConfig = {
subscription: LinkSubscription,
variables: {},
onCompleted: () => {
alert('done!');
},
updater: (Store, data) => {
const newLink = Store.getRootField('Link').getLinkedRecord('node');
const viewerProxy = Store.getRoot().getLinkedRecord('viewer');
const connection = ConnectionHandler.getConnection(
viewerProxy,
'<nameConnection>', // 'Viewer_links' or <Name_links>
{ mutation_in: ['CREATED'] }
);
const edge = ConnectionHandler.createEdge(
Store,
connection,
newLink,
'LinkEdge'
);
if (connection) {
ConnectionHandler.insertEdgeBefore(connection, edge);
console.log('DONE');
}
},
onError: error => console.log('An error occured:', error)
};
requestSubscription(Environment, subscriptionConfig);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.