Created
June 15, 2018 03:26
-
-
Save CodySwannGT/8f03ef95a4a4fb547b8103a7924a15c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=====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