-
-
Example Blog
-
- {({ data: { articles } }) => {
- return ;
- }}
-
+
+
+
+
+
Example Blog
+
+ {({ data: { articles } }) => {
+ return ;
+ }}
+
+
-
+
);
};
-export default Home
+export default withApollo(Home)
diff --git a/frontend/utils/apollo.js b/frontend/utils/apollo.js
index 87da01b..bd6e1fa 100644
--- a/frontend/utils/apollo.js
+++ b/frontend/utils/apollo.js
@@ -1,22 +1,148 @@
-import { ApolloClient } from 'apollo-client'
-import { InMemoryCache } from 'apollo-cache-inmemory'
-import withApollo from 'next-with-apollo'
-import { createHttpLink } from 'apollo-link-http'
+import React, { useMemo } from 'react'
+import Head from 'next/head'
+import { ApolloProvider } from '@apollo/react-hooks'
+import { ApolloClient, InMemoryCache, HttpLink } from 'apollo-boost'
import fetch from 'isomorphic-unfetch'
+import NProgress from 'nprogress'
+import "../assets/css/style.css";
+
+
+import Router from 'next/router'
+Router.events.on('routeChangeStart', url => NProgress.start())
+Router.events.on('routeChangeComplete', () => NProgress.done())
+Router.events.on('routeChangeError', () => NProgress.done())
const GRAPHQL_URL = process.env.BACKEND_URL || 'http://localhost:1337/graphql'
-const link = new createHttpLink({
+const link = new HttpLink({
fetch,
uri: GRAPHQL_URL
})
-export default withApollo(
- ({initialState}) =>
- new ApolloClient({
- link,
- cache: new InMemoryCache()
- .restore(initialState || {})
- })
-)
\ No newline at end of file
+
+
+ let apolloClient = null
+
+ /**
+ * Creates and provides the apolloContext
+ * to a next.js PageTree. Use it by wrapping
+ * your PageComponent via HOC pattern.
+ * @param {Function|Class} PageComponent
+ * @param {Object} [config]
+ * @param {Boolean} [config.ssr=true]
+ */
+ export function withApollo (PageComponent, { ssr = true } = {}) {
+ const WithApollo = ({ apolloClient, apolloState, ...pageProps }) => {
+ const client = useMemo(
+ () => apolloClient || initApolloClient(apolloState),
+ []
+ )
+ return (
+
+
+
+ )
+ }
+
+ // Set the correct displayName in development
+ if (process.env.NODE_ENV !== 'production') {
+ const displayName =
+ PageComponent.displayName || PageComponent.name || 'Component'
+
+ if (displayName === 'App') {
+ console.warn('This withApollo HOC only works with PageComponents.')
+ }
+
+ WithApollo.displayName = `withApollo(${displayName})`
+ }
+
+ // Allow Next.js to remove getInitialProps from the browser build
+ if (typeof window === 'undefined') {
+ if (ssr) {
+ WithApollo.getInitialProps = async ctx => {
+ const { AppTree } = ctx
+
+ let pageProps = {}
+ if (PageComponent.getInitialProps) {
+ pageProps = await PageComponent.getInitialProps(ctx)
+ }
+
+ // Run all GraphQL queries in the component tree
+ // and extract the resulting data
+ const apolloClient = initApolloClient()
+
+ try {
+ // Run all GraphQL queries
+ await require('@apollo/react-ssr').getDataFromTree(
+
+ )
+ } catch (error) {
+ // Prevent Apollo Client GraphQL errors from crashing SSR.
+ // Handle them in components via the data.error prop:
+ // https://www.apollographql.com/docs/react/api/react-apollo.html#graphql-query-data-error
+ console.error('Error while running `getDataFromTree`', error)
+ }
+
+ // getDataFromTree does not call componentWillUnmount
+ // head side effect therefore need to be cleared manually
+ Head.rewind()
+
+ // Extract query data from the Apollo store
+ const apolloState = apolloClient.cache.extract()
+
+ return {
+ ...pageProps,
+ apolloState
+ }
+ }
+ }
+ }
+
+ return WithApollo
+ }
+
+ /**
+ * Always creates a new apollo client on the server
+ * Creates or reuses apollo client in the browser.
+ * @param {Object} initialState
+ */
+ function initApolloClient (initialState) {
+ // Make sure to create a new client for every server-side request so that data
+ // isn't shared between connections (which would be bad)
+ if (typeof window === 'undefined') {
+ return createApolloClient(initialState)
+ }
+
+ // Reuse client on the client-side
+ if (!apolloClient) {
+ apolloClient = createApolloClient(initialState)
+ }
+
+ return apolloClient
+ }
+
+ /**
+ * Creates and configures the ApolloClient
+ * @param {Object} [initialState={}]
+ */
+ function createApolloClient (initialState = {}) {
+ // Check out https://github.com/zeit/next.js/pull/4611 if you want to use the AWSAppSyncClient
+ const isBrowser = typeof window !== 'undefined'
+ return new ApolloClient({
+ connectToDevTools: isBrowser,
+ ssrMode: !isBrowser, // Disables forceFetch on the server (so queries are only run once)
+ link: new HttpLink({
+ uri: GRAPHQL_URL, // Server URL (must be absolute)
+ credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers`
+ // Use fetch() polyfill on the server
+ fetch: !isBrowser && fetch
+ }),
+ cache: new InMemoryCache().restore(initialState)
+ })
+ }
\ No newline at end of file
diff --git a/package.json b/package.json
index 82dc575..022d92f 100644
--- a/package.json
+++ b/package.json
@@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"postinstall": "cd backend && npm install && cd ../frontend && npm install",
- "heroku-postbuild": "npm run --prefix ./frontend build",
+ "build": "npm run --prefix ./frontend build",
"start": "concurrently \"npm run --prefix ./backend/ start\" \"npm run --prefix ./frontend/ start\"",
"download": "concurrently \"npm run --prefix ./backend/ install\" \"npm run --prefix ./frontend/ install\"",
"dev": "concurrently \"yarn --cwd ./backend/ develop\" \"yarn --cwd ./frontend/ dev\""