summaryrefslogtreecommitdiffstats
path: root/frontend/lib/withData.js
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-03-28 15:22:34 -0400
committerWes Bos <wesbos@gmail.com>2018-03-28 15:22:34 -0400
commit65c524251d49ade9d44a62337f3c1c1fed6eedd0 (patch)
treed504ba9cea8d89bbde4b2e809fddb048bd660548 /frontend/lib/withData.js
parenta1fd7eb33bb08610aaf381a64b7f963cd98bfdaa (diff)
Permissions
Diffstat (limited to 'frontend/lib/withData.js')
-rw-r--r--frontend/lib/withData.js63
1 files changed, 38 insertions, 25 deletions
diff --git a/frontend/lib/withData.js b/frontend/lib/withData.js
index b784653..5c12ff0 100644
--- a/frontend/lib/withData.js
+++ b/frontend/lib/withData.js
@@ -12,13 +12,13 @@ function getComponentDisplayName(Component) {
export default ComposedComponent =>
class WithData extends React.Component {
static displayName = `WithData(${getComponentDisplayName(ComposedComponent)})`;
-
static propTypes = {
serverState: PropTypes.object.isRequired,
};
static async getInitialProps(ctx) {
- let serverState = { apollo: {} };
+ // Initial serverState with apollo (empty)
+ let serverState;
// Evaluate the composed component's getInitialProps()
let composedInitialProps = {};
@@ -28,41 +28,54 @@ export default ComposedComponent =>
// Run all GraphQL queries in the component tree
// and extract the resulting data
+ console.log('Trying...');
+ console.log(ctx.query);
+ const apollo = initApollo();
+ try {
+ // Run all GraphQL queries
+ const data = await getDataFromTree(<ComposedComponent ctx={ctx} {...composedInitialProps} />, {
+ router: {
+ asPath: ctx.asPath,
+ pathname: ctx.pathname,
+ query: ctx.query,
+ },
+ client: apollo,
+ }).catch(err => {
+ console.log('CAUGHT HERE');
+ console.log(err);
+ });
+ } catch (error) {
+ // console.log(ctx.query);
+ console.log('An error Happened SSR');
+ console.log(error);
+ // Prevent Apollo Client GraphQL errors from crashing SSR.
+ // Handle them in components via the data.error prop:
+ // http://dev.apollodata.com/react/api-queries.html#graphql-query-data-error
+ }
+
if (!process.browser) {
- const apollo = initApollo();
- // Provide the `url` prop data in case a GraphQL query uses it
- const url = { query: ctx.query, pathname: ctx.pathname };
- try {
- // Run all GraphQL queries
- await getDataFromTree(
- <ApolloProvider client={apollo}>
- <ComposedComponent url={url} {...composedInitialProps} />
- </ApolloProvider>
- );
- } catch (error) {
- // Prevent Apollo Client GraphQL errors from crashing SSR.
- // Handle them in components via the data.error prop:
- // http://dev.apollodata.com/react/api-queries.html#graphql-query-data-error
- }
// getDataFromTree does not call componentWillUnmount
// head side effect therefore need to be cleared manually
Head.rewind();
-
- // Extract query data from the Apollo store
- serverState = {
- apollo: {
- data: apollo.cache.extract(),
- },
- };
}
+ // Extract query data from the Apollo store
+ serverState = {
+ apollo: {
+ data: apollo.cache.extract(),
+ },
+ };
+
return {
serverState,
...composedInitialProps,
};
}
- apollo = initApollo(this.props.serverState.apollo.data);
+ constructor(props) {
+ super(props);
+ this.apollo = initApollo(this.props.serverState.apollo.data);
+ }
render() {
return (