import { useMutation, useQuery } from '@apollo/react-hooks';
import gql from 'graphql-tag';
const ADD_TODO = gql`
  mutation AddTodo($text: String!) {
    addTodo(text: $text) {
      id
      text
      completed
    }
  }
`;
const GET_TODOS = gql`
  query GetTodos {
    todos {
      id
      text
      completed
    }
  }
`;
const Todos = () => {
  const { loading, error, data } = useQuery(GET_TODOS);
  const [addTodo] = useMutation(ADD_TODO, {
    update(cache, { data: { addTodo } }) {
      const { todos } = cache.readQuery({ query: GET_TODOS });
      cache.writeQuery({
        query: GET_TODOS,
        data: { todos: todos.concat([addTodo]) },
      });
    },
  });
  if (loading) return 'Loading...';
  if (error) return `Error! ${error.message}`;
  return (
    
      {data.todos.map(({ id, text, completed }) => (
        {text}
      ))}
      
    
  );
};