2017-09-24 91 views
0

我遇到了graphQL apollo中的突變問題。當頁面加載時,它將運行查詢lectureResponseQuery,並且如果運行查詢== null變量fetchLectureResponseMutation以創建新文檔。這個變異返回新的結果,我做了一個更新的查詢,我期望組件將重新渲染與新的數據,但它不會。有誰知道這是爲什麼?謝謝!突變後的阿波羅更新不會觸發重投

@graphql(fetchLectureResponseMutation, { 
    options: ownProps => ({ 
    variables: { lectureName: ownProps.match.params.lectureName }, 
    update: (proxy, { data: { fetchLectureResponse } }) => { 
     const data = proxy.readQuery({ 
     query: lectureResponseQuery, 
     variables: { lectureName: ownProps.match.params.lectureName }, 
     }); 
     data.lectureResponse = fetchLectureResponse; 
     proxy.writeQuery({ 
     query: lectureResponseQuery, 
     data, 
     }); 
    }, 
    }), 
    name: 'fetchLectureResponse', 
}) 
@graphql(lectureResponseQuery, { 
    options: ownProps => ({ 
    variables: { lectureName: ownProps.match.params.lectureName }, 
    }), 
}) 
class LecturePage extends React.PureComponent { 
    componentWillUpdate(nextProps) { 
    if (nextProps.data.lectureResponse === null) { 
     this.props.fetchLectureResponse(); 
    } 
    } 


    render() { 
    const { data } = this.props; 
    if (data.loading || data.lectureResponse === null) { 
     return <Loading />; 
    } 

    return <LectureLayout lectureResponse={data.lectureResponse} /> 
    } 
} 

回答

0

對於任何正在研究這個問題在面向未來的中心問題是,我想做一個查找或創建操作。如果查詢只是返回新對象(如果該對象不存在),則這會更好,因爲那樣只會產生1個後端調用,這意味着您不必同步查詢和變體之間的時間。

TLDR:使用查詢進行findOrCreate操作!