Skip to content

Instantly share code, notes, and snippets.

@mwarger
Last active July 21, 2018 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwarger/e87724618be33e37daff51f016b15614 to your computer and use it in GitHub Desktop.
Save mwarger/e87724618be33e37daff51f016b15614 to your computer and use it in GitHub Desktop.
Typed Query Component
// @flow
import * as React from 'react'
import { Query } from 'react-apollo'
type TypedQueryProps<D, V> = {
query: any,
variables?: V,
children: (data: D) => React.Node,
fetchPolicy?: string
}
export default class TypedQuery<
D: ?any = {},
V: ?any = {}
> extends React.Component<TypedQueryProps<D, V>> {
render() {
const { query, variables, children, fetchPolicy } = this.props
return (
<Query
query={query}
variables={variables}
fetchPolicy={fetchPolicy || 'cache-and-network'}
>
{({
loading,
error,
data
}: {
loading: boolean,
error: {},
data: D
}) => {
if (loading) {
return <div>Loading...</div>
}
if (error) {
// send to logger
return <div>{JSON.stringify(error)}</div>
}
return children(data)
}}
</Query>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment