0
현재 릴레이 현대에서 돌연변이를 일으키는 문제를 발견했습니다. 우리는 많은 항목을 포함하는 일기를 가지고 있습니다. 사용자가 일기가없는 항목을 추가 할 때마다 항목을 만들기 전에 일기를 만듭니다. 모든 것이 예상대로 작동하지만 UI가 변형 된 직후 업데이트되지 않습니다. 여기에 코드가 있습니다.후속 돌연변이를 수행 한 후 릴레이가 업데이트되지 않음
AddDiaryMutation
import { commitMutation, graphql } from 'react-relay';
const mutation = graphql`
mutation AddDiaryMutation($input: AddDiaryInput!) {
createDiary(input: $input) {
diary {
id
...EntryList_entries
}
}
}
`;
let clientMutationId = 0;
const commit = (environment, { date }, callback) =>
commitMutation(environment, {
mutation,
variables: {
input: {
date,
clientMutationId: clientMutationId++
}
},
onCompleted: response => {
const id = response.createDiary.diary.id;
callback(id);
}
});
export default { commit };
AddEntryMutation는 항목을 추가 AddDiaryMutation 응답에서 ID를 얻을 것이다.
import { commitMutation, graphql } from 'react-relay';
import { ConnectionHandler } from 'relay-runtime';
const mutation = graphql`
mutation AddEntryMutation($input: AddEntryInput!) {
createEntry(input: $input) {
entryEdge {
node {
id
project {
name
}
speaker {
name
}
}
}
}
}
`;
function sharedUpdater(store, diaryId, newEdge) {
const diaryProxy = store.get(diaryId);
const conn = ConnectionHandler.getConnection(diaryProxy,
'EntryList_entries');
ConnectionHandler.insertEdgeAfter(conn, newEdge);
}
let clientMutationId = 0;
const commit = (environment, { diaryId, ...rest }, callback) =>
commitMutation(environment, {
mutation,
variables: {
input: {
...rest,
clientMutationId: clientMutationId++
}
},
updater: store => {
const payload = store.getRootField('createEntry');
const newEdge = payload.getLinkedRecord('entryEdge');
sharedUpdater(store, diaryId, newEdge);
},
onCompleted:() => callback()
});
export default { commit };
EntryList 조각
fragment EntryList_entries on Diary {
entries(first: 20) @connection(key: "EntryList_entries", filters: [])
{
edges {
node {
...Entry_entry
}
}
}
}
항목 조각
fragment Entry_entry on Entry {
id
project {
name
}
speaker {
name
}
}