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. Python 40 数据库-外键约束 、多对一与多对多的处理

    mysql提供了 foreign key,专门用于为表和表之间 建立物理关联 思考 表里存储的是一条条的记录,两个表之间能产生的关系有哪些? 现有 A B两张表 1.多对一         2.一对一 ...

  2. Vue 函数

    1.转换为大写字符 .toUpperCase() 2.字符串反转  this.message = this.message.split('').reverse().join('') 3.从index开 ...

  3. 最简单的多线程死锁案例代码(Java语言)

    package com.thread.test; public class DeadLock { private static Object firstMonitor = new Object(); ...

  4. PDO获取数据乱码的解决方法

    确保PHP文件编码格式为UTF8 确保数据字段格式为UTF8 PDO中设置编码格式,有如下三种方式: 方式1: 写在初始化dsn中 define( 'DB_DSN', 'mysql:host=loca ...

  5. Deutsch lernen (10)

    Dieser Weg Dieser Weg wird kein leichter sein. Dieser Weg wird steinig und schwer. Nicht mit vielen ...

  6. c#动态类型Dynamic

    需引用System.Dynamic命名空间 来源:http://www.cnblogs.com/ryanding/archive/2010/12/09/1900106.html dynamic Cus ...

  7. dispatch_sync:As an optimization, this function invokes the block on the current thread when possible

    两件事情: 1.是否是一个线程: 2.queue task 的目标线程是否有未完成的task. 模型:一个线程处理当前的task还有通过gc d派发来的待执行task. 猜测: 如果目标thread上 ...

  8. sql_3 join

    http://www.cnblogs.com/rush/archive/2012/03/27/2420246.html  

  9. node jsonwebtoken

     jsonwebtoken是node版本的JWT(JSON Web Tokens)的实现.1.什么是JWT?Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于J ...

  10. Arrays工具类的使用

    1.包: java.util 导包 2.此类包含用来操作数组(比如排序和搜索)的各种方法 特点: 该类中的方法都是静态方法,所以可以直接使用类名.方法名(实参)调用 3.查看成员方法: public ...