orderBy:更新缓存查询的结果

问题描述 投票:0回答:1

参与反应阿波罗graphcool项目

我的变异更新工作正常,但我想过滤结果,结果只过滤页面刷新?

看看cache.writeQuery(),文档说得到查询和concat到那所以我想这就是为什么它不过滤。反正有问题吗?

这里是我的CreatePost组件的代码

import React from 'react';
import gql from "graphql-tag";
import { Mutation } from "react-apollo";

const GET_POSTS = gql`
  {
    allPosts(orderBy: createdAt_DESC) {
      id
      title
      content
    }
  }
`;

const CREATE_POST = gql`
  mutation createPost($title: String!, $content: String!){
      createPost(title: $title, content: $content){
        id  
        title
        content
      }    
    }   
`;

const udpateCache = (cache, { data: { createPost } }) => {
    const { allPosts } = cache.readQuery({ query: GET_POSTS });
    cache.writeQuery({
        query: GET_POSTS,
        data: { allPosts: allPosts.concat([createPost]) }
    })
}

const CreatePost = () => {
    //vars for the input refs
    let titleInput
    let contentInput

    return (
        <Mutation
            mutation={CREATE_POST}
            update={udpateCache}
        >
           {createPost => ( //##form and onSubmit ##// ) }
        </Mutation>
    )
}

export default CreatePost
react-apollo graphcool
1个回答
0
投票

当您执行writeQuery时,还需要传入所使用的任何变量,以确保从缓存中收到相同的信息。

const udpateCache = (cache, { data: { createPost } }) => {
    const { allPosts } = cache.readQuery({ query: GET_POSTS });
    cache.writeQuery({
        query: GET_POSTS,
        data: { allPosts: allPosts.concat([createPost]) },
        variables: { orderBy: /* input */ }
    })
}
© www.soinside.com 2019 - 2024. All rights reserved.