0
votes

I have a component RecipeList and another AddRecipe.after adding a recipe it should be redirected to RecipeList and all items be displayed. here is the code of AddRecipe Component.

import React ,{Component}from 'react'
import { Mutation } from "react-apollo";
import {ADD_RECIPE} from '../../mutations';
import Error from '../Error';
import {withRouter} from 'react-router-dom';
import {GET_ALL_RECIPIES} from '../../queries'

class AddRecipe extends Component {
  ...
  onSubmit(event,addRecipe){
    event.preventDefault();
    addRecipe().then(
      ({data})=>
        {
          this.props.history.push("/")
        }
      )
  }
  render (){
    const{name,category,description,instruction,username} = this.state;
    return(<div className="App">
      <h2>Add recipe</h2>
      <Mutation
             mutation={ADD_RECIPE} 
             variables={{name,category,description,instruction,username}} 
             update={(cache, {data:{addRecipe}}) => {
                  const {getAllRecipes} = cache.readQuery({ query: GET_ALL_RECIPIES });
                  cache.writeQuery({
                    query:GET_ALL_RECIPIES,
                    data:{getAllRecipes:getAllRecipes.concat[addRecipe]}
                  })
              }} >
      {(addRecipe, {data,loading,error})=>

           (<form className="form" onSubmit={event=>this.onSubmit(event,addRecipe)}>
                  <input type="text" name="name" onChange={this.handleChange.bind(this)} placeholder="recipe name" value={name}/>
                  <select name="category" value="breakfast" id="" onChange={this.handleChange.bind(this)} value={category}>
                    <option value="breakfast">breakfast</option>
                    <option value="lunch">lunch</option>
                    <option value="dinner">dinner</option>
                    <option value="snack">snack</option>
                  </select>
                  <input type="text" name="description" onChange={this.handleChange.bind(this)} placeholder="recipe description" value={description}/>
                  <textarea name="instruction" id="instruction" cols="30" rows="10" onChange={this.handleChange.bind(this)} value={instruction}> </textarea>
                  <button type="submit" className="button-primary" disabled = {loading || this.validateForm()}>submit</button>
                  {error && <Error error={error}/>}
            </form>)

      }
      </Mutation>
    </div>)
  }

my add recipe mutation is :

import gql from 'graphql-tag';


//RECIPE QUERY
export const GET_ALL_RECIPIES =gql`
query {
    getAllRecipes{
        name
        _id
        category

    }
}
`

and finally get recipe list query is :

import gql  from "graphql-tag";

export const GET_RECIPE_ITEM =gql`
query($id:ID!){
        getRecipeItem(_id:$id){

          _id
          name
          category
          description
          instruction
          createdDate
          likes
          username
        }
}`

after submitting form and adding recipe I expect that first component is redirected to recipe list and second component the list of recipes includes the newly added recipe . but I see the old list that does not contain any of newly added recipes and for displaying new components I should refresh the page .

1

1 Answers

1
votes

Apollo Client caches data for performance improvements and it does not refetch a list of your recipes because it already in cache.

In order to overcome this, you can either change fetchPolicy from default cache-first to cache-and-network or network-only but I don't recommend this in your case or refetch getAllRecipes query after addRecipe mutation like so:

<Mutation
 mutation={ADD_RECIPE} 
 variables={{name,category,description,instruction,username}}
 refetchQueries=['getAllRecipes']
>
 ...
</Mutation>