Relay Modern - метод обновления подписки (обработчик соединения) (нет документации по этому вопросу)
Я пытаюсь оформить подписку на мой запрос. Поэтому я подозреваю, что соединение (из 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
// для конкретного идентификатора (59f88d417fae441eb567c453) CREATED,
Журнал DEBUG3 зажигает: RelayRecordProxy
// для клиента:root,
Журнал DEBUG4 зажигает: RelayRecordProxy
// для клиента:root:59f88d417fae441eb567c453,
Журнал DEBUG5: undefined
,
Журнал DEBUG6: ConnectionHandler
методы,
Журнал DEBUG7: user.id
кто запросил запрос.
Вопрос 1: Не могли бы вы помочь с некоторыми предложениями подключения?
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);
};