Graphql mutation

更新缓存,处理错误

The Mutation component

首先创建GraphQL mutation
第一个参数:variables、optimisticResponse、refetchQueries、update
第二个参数:对象

import gql from "graphql-tag";
import { Mutation } from "react-apollo";

const ADD_TODO = gql`
  mutation addTodo($type: String!) {
    addTodo(type: $type) {
      id
      type
    }
  }
`;

const AddTodo = () => {
  let input;

  return (
    
      {(addTodo, { data }) => (
        
{ e.preventDefault(); addTodo({ variables: { type: input.value } }); input.value = ""; }} > { input = node; }} />
)}
); };

Updating the cache

第一个参数 update方法

cache.readQuery  
cache.writeQuery  
cache.readFragment   
cache.writeFragment  
cache.writeData  

第二个参数 对象

const AddTodo = () => {
  let input;

  return (
     {
        const { todos } = cache.readQuery({ query: GET_TODOS });
        cache.writeQuery({
          query: GET_TODOS,
          data: { todos: todos.concat([addTodo]) }
        });
      }}
    >
      {addTodo => (
        
{ e.preventDefault(); addTodo({ variables: { type: input.value } }); input.value = ""; }} > { input = node; }} />
)}
); };

你可能感兴趣的:(Graphql mutation)