Skip to content

Instantly share code, notes, and snippets.

@hoodwink73
Created July 11, 2019 07:00
Show Gist options
  • Save hoodwink73/ac2624677b50d234201b79d2242f2490 to your computer and use it in GitHub Desktop.
Save hoodwink73/ac2624677b50d234201b79d2242f2490 to your computer and use it in GitHub Desktop.
React Suspense with Apollo Cache (as presented in the talk, State of React by Jared Palmer @reacteurope 2019)
const UserDetailRepos = ({login}) => {
const {data} = useQuery(USER_REPOS, {
variables: {login}
})
const {
user: {
reposositories: { nodes }
}
} = data
return (
<>{nodes && nodes.length > 0 ? nodes.map((node) => <Node node={node} /> ) : null}</>
)
}
export const useQuery = (query, props) => {
try {
// try to read data from the cache
const data = Client.readQuery({query, ...props})
return {data}
} catch (error) {
throw Client.query({query, ...props})
.then(data => ({data}))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment