Skip to content

Instantly share code, notes, and snippets.

@akiran
Created September 4, 2018 16:41
Show Gist options
  • Save akiran/dd7cc1e9f2435dcd8c74e9732c7714c3 to your computer and use it in GitHub Desktop.
Save akiran/dd7cc1e9f2435dcd8c74e9732c7714c3 to your computer and use it in GitHub Desktop.
Reuse GraphQL queries
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