Created
September 4, 2018 16:41
-
-
Save akiran/dd7cc1e9f2435dcd8c74e9732c7714c3 to your computer and use it in GitHub Desktop.
Reuse GraphQL queries
This file contains hidden or 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 from 'react' | |
import {graphql, compose} from 'react-apollo' | |
import gql from 'graphql-tag' | |
const withUser = graphql( | |
gql` | |
{ | |
user { | |
id | |
firstName | |
lastName | |
} | |
} | |
`, | |
{ | |
props: ({ownProps, data}) => ({ | |
user: data.user, | |
loading: ownProps.loading || data.loading, | |
}), | |
} | |
) | |
const withProducts = graphql( | |
gql` | |
{ | |
products { | |
id | |
title | |
price | |
} | |
} | |
`, | |
{ | |
props: ({ownProps, data}) => ({ | |
user: data.user, | |
loading: ownProps.loading || data.loading, | |
}), | |
} | |
) | |
@compose(withUser, withProducts) | |
export default class ProductList extends React.Component { | |
render() { | |
const {user, products, loading} = this.props | |
if (loading) { | |
return null | |
} | |
return ( | |
<div> | |
{products.map(product => <div key={product.id}>{product.title}</div>)} | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment