orderBy: the results from update cache query

610 views Asked by At

Working on a react apollo graphcool project

I've got my mutation update working, however I would like to filter the results, the results only filter on page refresh?

Looking at cache.writeQuery() the docs say get the query and concat to that so i guess thats why its not filtering. Is there anyway to query after?

Here the code from my CreatePost component

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
1

There are 1 answers

2
Benjamin Charais On

When you do your writeQuery you also need to pass in any variables used, to make sure you receive the same information from the cache.

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 */ }
    })
}