[GraphQL] Apollo React Mutation Component
In this lesson I refactor a React component that utilizes a higher-order component for mutations to the new Mutation render prop component baked into react-apollo 2.1.
Additional Resources: https://dev-blog.apollodata.com/introducing-react-apollo-2-1-c837cc23d926
If you want to mutate the server state, you can use <Mutation> component to simplify the code:
const ADD_ITEM = gql`
mutation addItem($value: String!) {
addItem(value: $value) @client
}
`; const client = new ApolloClient({
clientState: {
defaults,
resolvers: {
Mutation: {
addItem: (_, { value }, { cache }) => {
let { items } = cache.readQuery({ query: GET_ITEMS });
items = [...items, value];
cache.writeData({ data: { items } });
return null;
}
}
}
}
}); const AddItem = () => {
let input;
return (
<Mutation mutation={ADD_ITEM}>
{(addItem, { loading, error, data }) => (
<div>
<form
onSubmit={e => {
e.preventDefault();
addItem({ variables: { value: input.value } });
input.value = "";
input.focus();
}}
>
<input ref={node => (input = node)} />
<button type="submit">Add Item</button>
</form>
</div>
)}
</Mutation>
);
};
[GraphQL] Apollo React Mutation Component的更多相关文章
- [GraphQL] Apollo React Query Component
In this lesson I refactor a React component that utilizes the graphql higher-order component to the ...
- GraphQL + React Apollo + React Hook 大型项目实战(32 个视频)
GraphQL + React Apollo + React Hook 大型项目实战(32 个视频) GraphQL + React Apollo + React Hook 大型项目实战 #1 介绍「 ...
- [React + GraphQL] Use useLazyQuery to manually execute a query with Apollo React Hooks
When using useQuery from Apollo React Hooks, the request will be sent automatically after the compon ...
- GraphQL + React Apollo + React Hook + Express + Mongodb 大型前后端分离项目实战之后端(19 个视频)
GraphQL + React Apollo + React Hook + Express + Mongodb 大型前后端分离项目实战之后端(19 个视频) GraphQL + React Apoll ...
- React & styled component
React & styled component https://www.styled-components.com/#your-first-styled-component tagged t ...
- GraphQL & Apollo & Vue
GraphQL & Apollo & Vue https://www.howtographql.com/vue-apollo/0-introduction/ https://githu ...
- react slot component with args
react slot component with args how to pass args to react props child component https://codesandbox.i ...
- react hooks & component will unmount & useEffect & clear up
react hooks & component will unmount & useEffect & clear up useEffect & return === u ...
- meteor 为基础,联合 Apollo + React + React-Router
Graphql with Apollo, Meteor and React: https://janikvonrotz.ch/2016/10/09/graphql-with-apollo-meteor ...
随机推荐
- B. Jeff and Periods(cf)
B. Jeff and Periods time limit per test 1 second memory limit per test 256 megabytes input standard ...
- PHP序列化 反序列化
序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性. 1. serialize和 ...
- IBMWebsphere 使用jar包删除文件
1. 先使用ant打包一个jar包,删除其他不要的目录和文件,仅保留一个空的xxx.war文件夹("xxx"对应was上的工程安装根目录) 2. 在文件夹下新建一个META-INF ...
- [转]ORACLE EXECUTE IMMEDIATE 小结
转自:http://www.cnblogs.com/huanghai223/archive/2011/06/29/2093660.html EXECUTE IMMEDIATE 代替了以前Oracl ...
- python--2、数据类型
字符串 name='jinyudong' 按索引取值.正向取 与 反向取 name['3'] 'y' name['-3'] 'o' 切片(若要使用倒序指定步长为-1),开始或者结束不指定即为到最边上的 ...
- Android,加载离线Android API文档缓慢问题!
解决方法:在host文件末添加如下信息! 0.0.0.0 www.googleapis.com 0.0.0.0 www.google.com 0.0.0.0 www.google-analytics. ...
- c++ 虚函数,纯虚函数的本质区别
转载博客:https://mp.weixin.qq.com/s?__biz=MzAxNzYzMTU0Ng==&mid=2651289202&idx=1&sn=431ffd1fa ...
- CSS框架Bootstrap
作为一个软件开发人员,经常接触和使用框架是再平常的事情不过了.但是这些框架基本都是和语言相关的,比如WEB框架SpringMVC,JavaEE框架Spring,ORM框架Hibernate,还有Jav ...
- 三维重建面试0:*SLAM滤波方法的串联综述
知乎上的提问,高翔作了回答:能否简单并且易懂地介绍一下多个基于滤波方法的SLAM算法原理? 写的比较通顺,抄之.如有异议,请拜访原文.如有侵权,请联系删除. 我怎么会写得那么长--如果您有兴趣可以和我 ...
- AdaBoost--从原理到实现(Code:Python)
本文对原文有修改,若有疑虑,请移步原作者. 原文链接:blog.csdn.net/dark_scope/article/details/14103983 集成方法在函数模型上等价于一个多层神经网络, ...