[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 ...
随机推荐
- JS文件中的中文在网页上显示为乱码解决方法
转自:http://www.pc6.com/infoview/Article_63835.html 如果JS文件中的中文在网页上显示为乱码,不妨采用本文章中的方法来试一试,或许能解决使你很头疼的问题. ...
- HTML+CSS+JS总结
==================HTML(超文本标记语言)========== <!DOCTYPE> 声明位于文档中的最前面的位置,处于 <html> 标签之前.此标签可告 ...
- BootStrap 资源包的下载和使用
将附件中的包解压,放置webroot中,并在jsp页面中引用它们,即可以使用 bootstrap.rar (78.9 KB) 下载次数: 0
- mongoose 操作 mongodb 笔记 (自己的笔记,自己看的)
mongodb下载/安装 mongoose npm install --save mongoose mongoose 数据库连接 const mongoose = require('mongoos ...
- 安装rails卡住很慢 出现302 Moved Temporarily
在MAC上安装rails的时候,使用命令$ gem install rails 发现一直没响应,使用$ gem install rails-V命令发现,安装会在中间卡住,出现302 Moved Tem ...
- OpenCV中的模板匹配/Filter2d
1.模板匹配 模板匹配是在图像中寻找目标的方法之一.Come On, Boy.我们一起来看看模板匹配到底是怎么回事. 参考链接:http://www.opencv.org.cn/opencvdoc/2 ...
- 《计算机图形学基础(OpenGL版)》使用院校(更新)
从清华大学出版社责任编辑处获悉,很多高等院校选用了我们这本教材,读者反应不错! 另外,编辑提供了一份详细的使用院校名单如下: 河南科技学院 中原工学院 河北工程大学 防空兵学院 伊犁师院电信学院 吉林 ...
- mvc自定义控件
//自定义一个DatePicker.cshtml文件@helper Init() { <link href="~/Content/mobiscroll.custom-2.5.0.min ...
- 【转载】JavaWeb之DBUtils QueryRunner类对数据表的增、删、查(8种结果集处理方式)、改操作
一.使用QueryRunner类,实现对数据表的 insert delete update package com.shuhuadream.queryrunner; import java.sql.C ...
- Html 页面刷新后出现闪动
Html 页面刷新后,或跳转后,出现闪动,抖动问题 1.查看有没有用到新字体,新字体链接位置是否存在 如: @font-face { font-family: "AvantGarde-Dem ...