import React from "react"; import gql from "graphql-tag"; import { Mutation } from "react-apollo"; import IMPORT_PERSON from "../queries/import_person"; import LIST_PEOPLE from "../queries/list_people"; class AddFace extends React.Component { // I extracted the cache updating logic from the JSX because there were too // many nested braces and it was making me confused. cacheUpdater(cache, { data: { importPerson } }) { let { people } = cache.readQuery({ query: LIST_PEOPLE }); // If the person already exists in the cache then replace them with the new data. let existingPersonIndex = people.findIndex(person => person.username === importPerson.username); if (existingPersonIndex >= 0) { people.splice(existingPersonIndex, 1, importPerson); } // Otherwise add them to the cache. else { people = people.concat([importPerson]); } cache.writeQuery({ query: LIST_PEOPLE, data: { people: people } }); } render() { let input; return ( {(importPerson, { data }) => (
Add Face
{ e.preventDefault(); importPerson({ variables: { username: input.value } }); input.value = ""; }}>
input = node} />
)}
); } } export default AddFace;