graphql - react-apollo 突变不会重新获取
问题描述
我有一个组件RecipeList和另一个AddRecipe 。添加食谱后,它应该被重定向到 RecipeList 并显示所有项目。这是 AddRecipe 组件的代码。
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>)
}
我的添加配方突变是:
import gql from 'graphql-tag';
//RECIPE QUERY
export const GET_ALL_RECIPIES =gql`
query {
getAllRecipes{
name
_id
category
}
}
`
最后得到配方列表查询是:
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
}
}`
提交表单并添加食谱后,我希望 第一个组件被重定向到食谱列表, 第二个组件的食谱列表包括新添加的食谱。 但我看到旧列表不包含任何新添加的食谱,并且为了显示新组件,我应该刷新页面。
解决方案
Apollo Client 缓存数据以提高性能,它不会重新获取您的食谱列表,因为它已经在缓存中。
为了克服这个问题,您可以fetchPolicy
从默认更改cache-first
为cache-and-network
或network-only
但我不建议在您的情况下这样做,或者在突变getAllRecipes
后重新获取查询,如下所示:addRecipe
<Mutation
mutation={ADD_RECIPE}
variables={{name,category,description,instruction,username}}
refetchQueries=['getAllRecipes']
>
...
</Mutation>
推荐阅读
- google-apps-script - 如何使用 Google 表格中的格式水平合并单元格/列?
- php - PHP - 提取数组中的倒数第二个值
- javascript - 在 javascript 上选择的数据不显示从数据库中选择的数据
- asp.net-mvc - 如何修复:无法对空引用执行运行时绑定
- gnupg - 将带有“--status-id 1”的gpg管道传输到文件后如何修复“找不到有效的OpenPGP数据”
- java - 在添加 dataBinding 标记时找不到资源 android:style/TextAppearance.Material.Notification
- c++ - 从头到尾迭代基于范围的 for 循环
- node.js - 无法在 Vue 组件中导入自定义 NodeJs 模块
- vue.js - VueJS 设置 axios 标头持久化
- android - 如何在android中制作调色板的水平列表