Created
October 26, 2018 12:44
-
-
Save alanleite/18898d6f50d0858c610b9dc670be00b6 to your computer and use it in GitHub Desktop.
Helper for generate Query and Mutation groups using Apollo
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
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> | |
) | |
} | |
} |
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
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