如何在不丢失 "this" 上下文的情况下从 React 组件写入 apollo 缓存
How to write to apollo cache from within React component with out losing context of "this"
我在尝试从 react
组件中读取 apollo 缓存时遇到问题,突变起作用并写入我的服务器和 returns 数据但是当传递到我的更新函数时似乎在 inMemoryCache.js
中失去上下文
- "apollo-cache-inmemory": "^1.2.5"
- "react-apollo": "^2.1.4"
- "apollo-boost": "^0.1.7"
TypeError: Cannot read property 'read' of undefined
at ./node_modules/apollo-cache-inmemory/lib/inMemoryCache.js.InMemoryCache.readQuery
import React, { Component } from "react";
import { graphql } from "react-apollo";
import trim from "lodash/trim";
import AuthorForm from '../components/author-form';
import ALL_AUTHORS from "../graphql/getPosts.query";
import CREATE_AUTHOR from "../graphql/createAuthor.mutation";
class CreateAuthor extends Component {
state = {
errors: false
};
onSubmit(event) {
event.preventDefault();
const form = new FormData(event.target);
const data = {
firstName: form.get("firstName"),
lastName: form.get("lastName")
};
if (!data.firstName || !data.lastName) {
return this.setState({ errors: true });
}
this.create({
firstName: trim(data.firstName),
lastName: trim(data.lastName)
});
}
async create(variables) {
const { createAuthor } = this.props;
this.setState({ errors: false });
try {
await createAuthor({
variables,
update: (cache, data) => this.updateCache(cache, data)
})
} catch (e) {
this.setState({ errors: true })
}
}
updateCache({ readQuery, writeQuery }, { data: { createAuthor }, errors }) {
if (errors) {
return;
}
const { allAuthors } = readQuery({
query: ALL_AUTHORS,
defaults: {
allAuthors: []
}
});
/*eslint-disable*/ console.log(allAuthors);
}
render() {
return (
<div>
<AuthorForm onSubmit={this.onSubmit.bind(this)}/>
<OnError/>
</div>
);
}
}
export default graphql(CREATE_AUTHOR, { name: "createAuthor" })(CreateAuthor);
跟我绑定onSubmit按钮有关系吗?如果是这样,在不丢失组件内 this 上下文的情况下将函数附加到元素的正确方法是什么,并且仍然允许 apollo 缓存正常运行。
我失去了这方面的背景,因为我正在解构第一个论点。这是我最后确定的。
当 ROOT_QUERY 对象上没有 allAuthors 时会抛出错误,因此将其添加到我的 return 语句中。
这不是更新缓存的理想方式,不应将默认参数传递给 readQuery 以防止引发错误。
updateCache(cache, { data: { createAuthor }, errors }) {
if (errors || !cache.data.data.ROOT_QUERY.allAuthors) {
return;
}
const query = ALL_AUTHORS;
const { allAuthors } = cache.readQuery({
query,
defaults: {
allAuthors: []
}
});
const data = {
allAuthors: allAuthors.concat([createAuthor])
};
cache.writeQuery({
query,
data
});
}
我在尝试从 react
组件中读取 apollo 缓存时遇到问题,突变起作用并写入我的服务器和 returns 数据但是当传递到我的更新函数时似乎在 inMemoryCache.js
- "apollo-cache-inmemory": "^1.2.5"
- "react-apollo": "^2.1.4"
- "apollo-boost": "^0.1.7"
TypeError: Cannot read property 'read' of undefined at ./node_modules/apollo-cache-inmemory/lib/inMemoryCache.js.InMemoryCache.readQuery
import React, { Component } from "react";
import { graphql } from "react-apollo";
import trim from "lodash/trim";
import AuthorForm from '../components/author-form';
import ALL_AUTHORS from "../graphql/getPosts.query";
import CREATE_AUTHOR from "../graphql/createAuthor.mutation";
class CreateAuthor extends Component {
state = {
errors: false
};
onSubmit(event) {
event.preventDefault();
const form = new FormData(event.target);
const data = {
firstName: form.get("firstName"),
lastName: form.get("lastName")
};
if (!data.firstName || !data.lastName) {
return this.setState({ errors: true });
}
this.create({
firstName: trim(data.firstName),
lastName: trim(data.lastName)
});
}
async create(variables) {
const { createAuthor } = this.props;
this.setState({ errors: false });
try {
await createAuthor({
variables,
update: (cache, data) => this.updateCache(cache, data)
})
} catch (e) {
this.setState({ errors: true })
}
}
updateCache({ readQuery, writeQuery }, { data: { createAuthor }, errors }) {
if (errors) {
return;
}
const { allAuthors } = readQuery({
query: ALL_AUTHORS,
defaults: {
allAuthors: []
}
});
/*eslint-disable*/ console.log(allAuthors);
}
render() {
return (
<div>
<AuthorForm onSubmit={this.onSubmit.bind(this)}/>
<OnError/>
</div>
);
}
}
export default graphql(CREATE_AUTHOR, { name: "createAuthor" })(CreateAuthor);
跟我绑定onSubmit按钮有关系吗?如果是这样,在不丢失组件内 this 上下文的情况下将函数附加到元素的正确方法是什么,并且仍然允许 apollo 缓存正常运行。
我失去了这方面的背景,因为我正在解构第一个论点。这是我最后确定的。
当 ROOT_QUERY 对象上没有 allAuthors 时会抛出错误,因此将其添加到我的 return 语句中。
这不是更新缓存的理想方式,不应将默认参数传递给 readQuery 以防止引发错误。
updateCache(cache, { data: { createAuthor }, errors }) {
if (errors || !cache.data.data.ROOT_QUERY.allAuthors) {
return;
}
const query = ALL_AUTHORS;
const { allAuthors } = cache.readQuery({
query,
defaults: {
allAuthors: []
}
});
const data = {
allAuthors: allAuthors.concat([createAuthor])
};
cache.writeQuery({
query,
data
});
}