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的更多相关文章

  1. [GraphQL] Apollo React Query Component

    In this lesson I refactor a React component that utilizes the graphql higher-order component to the ...

  2. GraphQL + React Apollo + React Hook 大型项目实战(32 个视频)

    GraphQL + React Apollo + React Hook 大型项目实战(32 个视频) GraphQL + React Apollo + React Hook 大型项目实战 #1 介绍「 ...

  3. [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 ...

  4. GraphQL + React Apollo + React Hook + Express + Mongodb 大型前后端分离项目实战之后端(19 个视频)

    GraphQL + React Apollo + React Hook + Express + Mongodb 大型前后端分离项目实战之后端(19 个视频) GraphQL + React Apoll ...

  5. React & styled component

    React & styled component https://www.styled-components.com/#your-first-styled-component tagged t ...

  6. GraphQL & Apollo & Vue

    GraphQL & Apollo & Vue https://www.howtographql.com/vue-apollo/0-introduction/ https://githu ...

  7. react slot component with args

    react slot component with args how to pass args to react props child component https://codesandbox.i ...

  8. react hooks & component will unmount & useEffect & clear up

    react hooks & component will unmount & useEffect & clear up useEffect & return === u ...

  9. meteor 为基础,联合 Apollo + React + React-Router

    Graphql with Apollo, Meteor and React: https://janikvonrotz.ch/2016/10/09/graphql-with-apollo-meteor ...

随机推荐

  1. 移动App服务端架构设计

      我从事手机app服务端开发现在已经是3个年头,自己也整理出了一套相对好用的服务架构,写出来,跟大家一起分享.如有不足,还请多指教. 一:基础流程图. 其实有一点还需要加上,就是对json的压缩和加 ...

  2. python 关于文件操作的一些理解

    在用python进行数据处理编程中,往往涉及到文件IO口读写,IO口的读写性能会极大的影响程序的运行时间.在进行文件写入时,一般会存在两种情况.第一种是数据到来马上进行数据写入,即来一条写一条,第二种 ...

  3. 微信小程序获取当前所在城市

    本篇文章主要讲解在微信小程序中,如何利用微信自带的api(wx.getLocation())结合百度地图的逆地址解析api来获取当前所在城市名. 实现起来也比较简单,步骤为: 1--利用微信小程序接口 ...

  4. Blender之UILayout

    目标 [x] 总结Blender面板布局 总结 Blender面板中界面组件是通过UILayout进行组织的. 其主要属性如下: row() 定义横向子布局. column() 定义竖向子布局. sp ...

  5. 使用Attiny 85开发板制作BadUSB

    什么是BadUSB?请查看:http://www.baike.com/wiki/BadUSB 或者看看腾讯这个视频!https://v.qq.com/x/page/l01425u2igw.html   ...

  6. 自学Python十一 Python爬虫总结

    通过几天的学习与尝试逐渐对python爬虫有了一些小小的心得,我们渐渐发现他们有很多共性,总是要去获取一系列的链接,读取网页代码,获取所需内容然后重复上面的工作,当自己运用的越来越熟练之后我们就会尝试 ...

  7. 利用POPAnimatableProperty属性来实现动画倒计时

    POPAnimatableProperty *prop = [POPAnimatableProperty propertyWithName:@"countdown" initial ...

  8. windows 下安装mysql 成功版

    mysql 下载地址 http://dev.mysql.com/downloads/ zip版下载 解压到本地 假设文件保存在C:\mysql-5.7.17-winx64 1.以管理员身份运行cmd. ...

  9. jquery相关常用的工具函数

    1.弹出提示框: function prompt(msg){ $("<div>" + msg + "</div>").css({ &qu ...

  10. BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板 贪心 + 堆 + 反向思考

    Description Farmer John想修理牧场栅栏的某些小段.为此,他需要N(1<=N<=20,000)块特定长度的木板,第i块木板的长度为Li(1<=Li<=50, ...