Unverified Commit 6c0091fb authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add new apollo example using _app.js (#4286)

* Add new apollo example

* Update readme

* Update with-apollo to use _app.js

* Move withData to _app

* Make SSR work again

* Remove withData

* Use HOC to provide apolloClient

* Spread pageprops
parent 4881cd34
...@@ -46,11 +46,8 @@ now ...@@ -46,11 +46,8 @@ now
[Apollo](http://dev.apollodata.com) is a GraphQL client that allows you to easily query the exact data you need from a GraphQL server. In addition to fetching and mutating data, Apollo analyzes your queries and their results to construct a client-side cache of your data, which is kept up to date as further queries and mutations are run, fetching more results from the server. [Apollo](http://dev.apollodata.com) is a GraphQL client that allows you to easily query the exact data you need from a GraphQL server. In addition to fetching and mutating data, Apollo analyzes your queries and their results to construct a client-side cache of your data, which is kept up to date as further queries and mutations are run, fetching more results from the server.
In this simple example, we integrate Apollo seamlessly with Next by wrapping our *pages* inside a [higher-order component (HOC)](https://facebook.github.io/react/docs/higher-order-components.html). Using the HOC pattern we're able to pass down a central store of query result data created by Apollo into our React component hierarchy defined inside each page of our Next application. In this simple example, we integrate Apollo seamlessly with Next by wrapping our *pages/_app.js* inside a [higher-order component (HOC)](https://facebook.github.io/react/docs/higher-order-components.html). Using the HOC pattern we're able to pass down a central store of query result data created by Apollo into our React component hierarchy defined inside each page of our Next application.
On initial page load, while on the server and inside `getInitialProps`, we invoke the Apollo method, [`getDataFromTree`](http://dev.apollodata.com/react/server-side-rendering.html#getDataFromTree). This method returns a promise; at the point in which the promise resolves, our Apollo Client store is completely initialized. On initial page load, while on the server and inside `getInitialProps`, we invoke the Apollo method, [`getDataFromTree`](http://dev.apollodata.com/react/server-side-rendering.html#getDataFromTree). This method returns a promise; at the point in which the promise resolves, our Apollo Client store is completely initialized.
This example relies on [graph.cool](https://www.graph.cool) for its GraphQL backend. This example relies on [graph.cool](https://www.graph.cool) for its GraphQL backend.
### Note:
In these *with-apollo* examples, the ```withData()``` HOC must wrap a top-level component from within the ```pages``` directory. Wrapping a child component with the HOC will result in a ```Warning: Failed prop type: The prop 'serverState' is marked as required in 'WithData(Apollo(Component))', but its value is 'undefined'``` error. Down-tree child components will have access to Apollo, and can be wrapped with any other sort of ```graphql()```, ```compose()```, etc HOC's.
...@@ -102,23 +102,25 @@ export default graphql(allPosts, { ...@@ -102,23 +102,25 @@ export default graphql(allPosts, {
options: { options: {
variables: allPostsQueryVars variables: allPostsQueryVars
}, },
props: ({ data }) => ({ props: ({ data }) => {
data, return ({
loadMorePosts: () => { data,
return data.fetchMore({ loadMorePosts: () => {
variables: { return data.fetchMore({
skip: data.allPosts.length variables: {
}, skip: data.allPosts.length
updateQuery: (previousResult, { fetchMoreResult }) => { },
if (!fetchMoreResult) { updateQuery: (previousResult, { fetchMoreResult }) => {
return previousResult if (!fetchMoreResult) {
return previousResult
}
return Object.assign({}, previousResult, {
// Append the new posts results to the old one
allPosts: [...previousResult.allPosts, ...fetchMoreResult.allPosts]
})
} }
return Object.assign({}, previousResult, { })
// Append the new posts results to the old one }
allPosts: [...previousResult.allPosts, ...fetchMoreResult.allPosts] })
}) }
}
})
}
})
})(PostList) })(PostList)
import React from 'react' import initApollo from './init-apollo'
import PropTypes from 'prop-types'
import { ApolloProvider, getDataFromTree } from 'react-apollo'
import Head from 'next/head' import Head from 'next/head'
import initApollo from './initApollo' import { getDataFromTree } from 'react-apollo'
import propTypes from 'prop-types'
// Gets the display name of a JSX component for dev tools export default (App) => {
function getComponentDisplayName(Component) { return class Apollo extends React.Component {
return Component.displayName || Component.name || 'Unknown' static displayName = 'withApollo(App)'
} static async getInitialProps (ctx) {
const { Component, router } = ctx
export default ComposedComponent => { let appProps = {}
return class WithData extends React.Component { if (App.getInitialProps) {
static displayName = `WithData(${getComponentDisplayName( appProps = await App.getInitialProps(ctx)
ComposedComponent
)})`
static propTypes = {
serverState: PropTypes.object.isRequired
}
static async getInitialProps(ctx) {
// Initial serverState with apollo (empty)
let serverState
// Evaluate the composed component's getInitialProps()
let composedInitialProps = {}
if (ComposedComponent.getInitialProps) {
composedInitialProps = await ComposedComponent.getInitialProps(ctx)
} }
const apolloState = {}
// Run all GraphQL queries in the component tree // Run all GraphQL queries in the component tree
// and extract the resulting data // and extract the resulting data
const apollo = initApollo() const apollo = initApollo()
try { try {
// create the url prop which is passed to every page
const url = {
query: ctx.query,
asPath: ctx.asPath,
pathname: ctx.pathname,
};
// Run all GraphQL queries // Run all GraphQL queries
await getDataFromTree( await getDataFromTree(
<ComposedComponent ctx={ctx} url={url} {...composedInitialProps} />, <App
{ {...appProps}
router: { Component={Component}
asPath: ctx.asPath, router={router}
pathname: ctx.pathname, apolloState={apolloState}
query: ctx.query apolloClient={apollo}
}, />
client: apollo
}
) )
} catch (error) { } catch (error) {
// Prevent Apollo Client GraphQL errors from crashing SSR. // Prevent Apollo Client GraphQL errors from crashing SSR.
// Handle them in components via the data.error prop: // Handle them in components via the data.error prop:
// http://dev.apollodata.com/react/api-queries.html#graphql-query-data-error // http://dev.apollodata.com/react/api-queries.html#graphql-query-data-error
console.error('Error while running `getDataFromTree`', error)
} }
if (!process.browser) { if (!process.browser) {
...@@ -64,29 +44,23 @@ export default ComposedComponent => { ...@@ -64,29 +44,23 @@ export default ComposedComponent => {
} }
// Extract query data from the Apollo store // Extract query data from the Apollo store
serverState = { apolloState.data = apollo.cache.extract()
apollo: {
data: apollo.cache.extract()
}
}
return { return {
serverState, ...appProps,
...composedInitialProps apolloState
} }
} }
constructor(props) { constructor (props) {
super(props) super(props)
this.apollo = initApollo(this.props.serverState.apollo.data) // `getDataFromTree` renders the component first, the client is passed off as a property.
// After that rendering is done using Next's normal rendering pipeline
this.apolloClient = props.apolloClient || initApollo(props.apolloState.data)
} }
render() { render () {
return ( return <App {...this.props} apolloClient={this.apolloClient} />
<ApolloProvider client={this.apollo}>
<ComposedComponent {...this.props} />
</ApolloProvider>
)
} }
} }
} }
\ No newline at end of file
import App, {Container} from 'next/app'
import React from 'react'
import withApolloClient from '../lib/with-apollo-client'
import { ApolloProvider } from 'react-apollo'
class MyApp extends App {
render () {
const {Component, pageProps, apolloClient} = this.props
return <Container>
<ApolloProvider client={apolloClient}>
<Component {...pageProps} />
</ApolloProvider>
</Container>
}
}
export default withApolloClient(MyApp)
...@@ -2,12 +2,11 @@ import App from '../components/App' ...@@ -2,12 +2,11 @@ import App from '../components/App'
import Header from '../components/Header' import Header from '../components/Header'
import Submit from '../components/Submit' import Submit from '../components/Submit'
import PostList from '../components/PostList' import PostList from '../components/PostList'
import withData from '../lib/withData'
export default withData(() => ( export default () => (
<App> <App>
<Header /> <Header />
<Submit /> <Submit />
<PostList /> <PostList />
</App> </App>
)) )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment