Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CodySwannGT/8f03ef95a4a4fb547b8103a7924a15c6 to your computer and use it in GitHub Desktop.
Save CodySwannGT/8f03ef95a4a4fb547b8103a7924a15c6 to your computer and use it in GitHub Desktop.
=====HOC====
AddFoo.js
const AddFoo = props =>
<Button onClick={() => props.onAdd(props.data)} title={props.title} />
export default graphql(CreateFoo, {
  props: props => ({
    onAdd: foo => props.mutate({
      variables: foo,
      optimisticResponse: {
        __typename: 'Mutation',
        createFoo: { ...foo,  __typename: 'Foo' }
      },
      update: (proxy, { data: { createFoo } }) => {
        const data = proxy.readQuery({ query: ListFoos });
        data.listFoos.items.push(createFoo);
        proxy.writeQuery({ query: ListFoos, data });
      }
    })
  })
})(AddFoo)
Page.js
import AddFoo from './AddFoo.js';
const Page = props =>
<AddFoo title="click me" data={{me: "cody"}} />
====== Render Props =====
Page.js
const Page = props =>
<Mutation
mutation={ createFoo }
>
{createFoo =>
<Button onClick={
createFoo({
      variables: {me: "cody"},
      optimisticResponse: {
        __typename: 'Mutation',
        createFoo: { ...{me: "cody"},  __typename: 'Foo' }
      },
      update: (proxy, { data: { createFoo } }) => {
        const data = proxy.readQuery({ query: ListFoos });
        data.listFoos.items.push(createFoo);
        proxy.writeQuery({ query: ListFoos, data });
      }
    })
} />
</Mutation>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment