Skip to content

Instantly share code, notes, and snippets.

@alanleite
Created October 26, 2018 12:44
Show Gist options
  • Save alanleite/18898d6f50d0858c610b9dc670be00b6 to your computer and use it in GitHub Desktop.
Save alanleite/18898d6f50d0858c610b9dc670be00b6 to your computer and use it in GitHub Desktop.
Helper for generate Query and Mutation groups using Apollo
export const withQueryWrapper = (params) => {
return (Component) => {
return (props) => (
<Query {...params}>
{({ data, loading }) => (
loading ? null :
<Component
{...props}
data={data}
/>
)}
</Query>
)
}
}
export const withMutations = (mutations) => {
const mutationsNames = Object.keys(mutations)
const mapper = mutationsNames
.reduce((acc, name) => {
acc[name] = ({ render }) => (
<Mutation {...mutations[name]}>{render}</Mutation>
)
return acc
}, {})
const Mutations = adopt(mapper)
return (Component) => {
return (props) => (
<Mutations>
{(props) => (
<Component {...props} />
)}
</Mutations>
)
}
}
export default compose(
withQueryWrapper({
query: GET_QUERY
}),
withMutations({
myActionName: {
mutation: M_ACTION,
refetchQueries: (mutationResult) => [
{ query: M_ACTION_REFETCH, variables: {} }
]
}
})
)(ViewContainer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment