突变后如何更新反应-阿波罗钩子的缓存?

时间:2019-05-18 14:52:50

标签: javascript reactjs graphql apollo react-apollo

我正在尝试在对react-apollo-hooks进行突变后实施缓存更新。

const onDeleteHandler = useMutation(DELETE_POST, {
        update: (proxy, mutationResult) => {
            /* custom update logic */
            try {
                const { deletePost } = mutationResult.data;
                const postFeed = proxy.readQuery({
                    query: GET_POSTS, variables
                })
                console.log("postFeed", postFeed)
                proxy.writeQuery({ query: GET_POSTS })
            }
            catch(error){
                console.log("Cache update error", error)
            }
          },
        })

我当前收到以下错误消息:

  

将结果写入存储以供查询时出错   “ kind”:“ SelectionSet”,“ selections”:[{“ kind”:“ Field”,“ name”:{“ kind”:“ Name”,“ value”:“ id”},“ arguments”:[] ,“ directives”:[]},{“ kind”:“ Field”,“ name”:{“ kind”:“ Name”,“ value”:“ firstName”},“ arguments”:[],“ directives” :[]},{“种类”:“字段”,“名称”:{“种类”:“名称”,“值”:“姓氏”},“参数”:[],“指令”:[]} ,{“种类”:“字段”,“名称”:{“种类”:“名称”,“值”:“ __ typename”}}}}},{“种类”:“字段”,“名称”:{ “ kind”:“名称”,“值”:“ __ typename”}}]}}}}}}],“ loc”:{“开始”:0,“结束”:434}

我的直觉是writeQuery()方法需要一个如下所示的辅助变量:

proxy.writeQuery({ query: GET_POSTS, data })

但是,我不确定应该包含什么内容,或者甚至是问题所在

我的客户端查询是这样的:

export const GET_POSTS = gql`
    query Posts($query: String, $skip: Int, $first: Int, $orderBy: UserOrderByInput){
        posts (
            query: $query
            skip: $skip
            first: $first
            orderBy: $orderBy
        ){
            id
            title
            body
            location
            published
            author {
                id
                firstName
                lastName
            }
        }
    }
`

和变量:

const variables = { ...queryVariables, query: value.state.searchTerm, orderBy: filter }

0 个答案:

没有答案