Created
March 31, 2018 15:02
-
-
Save kitze/7df9d11e845ecedfbb34fe4e9441a9da to your computer and use it in GitHub Desktop.
apollo shenanigans
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
import React, { Fragment } from 'react'; | |
import { Query, Mutation } from 'react-apollo'; | |
import get from 'lodash/get'; | |
//components | |
import Loading from 'components/LoadingScreen'; | |
export const QueryWithLoading = ({ children, query, dataPath, loadingComponent = <Loading /> }) => ( | |
<Query query={query}> | |
{({ data, loading }) => (loading ? loadingComponent : children(get(data, dataPath)))} | |
</Query> | |
); | |
export const UpdateOrDeleteEntity = ({ | |
deleteMutation, | |
updateMutation, | |
query, | |
variables, | |
children | |
}) => ( | |
<Query query={query} variables={variables}> | |
{({ data, loading: isLoading }) => ( | |
<Fragment> | |
<Mutation mutation={deleteMutation}> | |
{(deleteEntityMutation, { loading: isDeleting }) => ( | |
<Mutation mutation={updateMutation}> | |
{(updateEntityMutation, { loading: isUpdating }) => | |
children({ | |
mutations: { delete: deleteEntityMutation, update: updateEntityMutation }, | |
isDeleting, | |
isLoading, | |
isUpdating, | |
data | |
}) | |
} | |
</Mutation> | |
)} | |
</Mutation> | |
</Fragment> | |
)} | |
</Query> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment