import { ApolloConsumer } from 'react-apollo' import gql from 'graphql-tag' import { allPostsQuery, allPostsQueryVars } from './PostList' export default function Submit () { return ( {client => (
handleSubmit(event, client)}>

Submit

)}
) } function handleSubmit (event, client) { event.preventDefault() const form = event.target const formData = new window.FormData(form) const title = formData.get('title') const url = formData.get('url') form.reset() client.mutate({ mutation: gql` mutation createPost($title: String!, $url: String!) { createPost(title: $title, url: $url) { id title votes url createdAt } } `, variables: { title, url }, update: (proxy, { data: { createPost } }) => { const data = proxy.readQuery({ query: allPostsQuery, variables: allPostsQueryVars }) proxy.writeQuery({ query: allPostsQuery, data: { ...data, allPosts: [createPost, ...data.allPosts] }, variables: allPostsQueryVars }) } }) }