![](/img/trans.png)
[英]`updater` not working with Relay Modern because `ConnectionHandler.getConnection()` returns `undefined`
[英]Relay Modern - Subscription updater(connection handler) method (no documentation on this matter)
我正在嘗試為查詢執行訂閱。 因此,我懷疑連接(來自ConnectionHandler)無法正常工作。 我找不到與此有關的任何適當文檔。
我的訂閱如下所示:
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);
};
正如您可能在代碼中看到的那樣,我運行了很多日志以查看我做錯了什么。
記錄DEBUG觸發: RelayRecordSourceSelectorProxy
,
日志DEBUG2觸發: RelayRecordProxy
// //創建了特定ID(59f88d417fae441eb567c453),
日志DEBUG3觸發: RelayRecordProxy
// //客戶端:root,
日志DEBUG4觸發: RelayRecordProxy
// //客戶端:root:59f88d417fae441eb567c453,
日志DEBUG5: undefined
,
記錄DEBUG6: ConnectionHandler
方法,
登錄DEBUG7: user.id
誰請求的查詢。
問題1:能否請您提供一些連接建議?
訂閱: 在每個響應上更新客戶端
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.