forked from vercel/next.js
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Examples] Move with-graphql-hooks to SSG (vercel#13858)
Related to vercel#11014 The setup is very similar to the one used for Apollo examples. @Joezo I've removed the demo example that you added to the readme (vercel#6482) as it will be outdated after this PR is merged.
- Loading branch information
Showing
12 changed files
with
144 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,30 @@ | ||
import Link from 'next/link' | ||
import { withRouter } from 'next/router' | ||
import { useRouter } from 'next/router' | ||
|
||
const Header = ({ router: { pathname } }) => ( | ||
<header> | ||
<Link href="/"> | ||
<a className={pathname === '/' ? 'is-active' : ''}>Home</a> | ||
</Link> | ||
<Link href="/about"> | ||
<a className={pathname === '/about' ? 'is-active' : ''}>About</a> | ||
</Link> | ||
<style jsx>{` | ||
header { | ||
margin-bottom: 25px; | ||
} | ||
a { | ||
font-size: 14px; | ||
margin-right: 15px; | ||
text-decoration: none; | ||
} | ||
.is-active { | ||
text-decoration: underline; | ||
} | ||
`}</style> | ||
</header> | ||
) | ||
export default function Header() { | ||
const { pathname } = useRouter() | ||
|
||
export default withRouter(Header) | ||
return ( | ||
<header> | ||
<Link href="/"> | ||
<a className={pathname === '/' ? 'is-active' : ''}>Home</a> | ||
</Link> | ||
<Link href="/about"> | ||
<a className={pathname === '/about' ? 'is-active' : ''}>About</a> | ||
</Link> | ||
<style jsx>{` | ||
header { | ||
margin-bottom: 25px; | ||
} | ||
a { | ||
font-size: 14px; | ||
margin-right: 15px; | ||
text-decoration: none; | ||
} | ||
.is-active { | ||
text-decoration: underline; | ||
} | ||
`}</style> | ||
</header> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useMemo } from 'react' | ||
import { GraphQLClient } from 'graphql-hooks' | ||
import memCache from 'graphql-hooks-memcache' | ||
|
||
let graphQLClient | ||
|
||
function createClient(initialState) { | ||
return new GraphQLClient({ | ||
ssrMode: typeof window === 'undefined', | ||
url: 'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn', // Server URL (must be absolute) | ||
cache: memCache({ initialState }), | ||
}) | ||
} | ||
|
||
export function initializeGraphQL(initialState = null) { | ||
const _graphQLClient = graphQLClient ?? createClient(initialState) | ||
|
||
// After navigating to a page with an initial GraphQL state, create a new cache with the | ||
// current state merged with the incoming state and set it to the GraphQL client. | ||
// This is necessary because the initial state of `memCache` can only be set once | ||
if (initialState && graphQLClient) { | ||
graphQLClient.cache = memCache({ | ||
initialState: Object.assign( | ||
graphQLClient.cache.getInitialState(), | ||
initialState | ||
), | ||
}) | ||
} | ||
// For SSG and SSR always create a new GraphQL Client | ||
if (typeof window === 'undefined') return _graphQLClient | ||
// Create the GraphQL Client once in the client | ||
if (!graphQLClient) graphQLClient = _graphQLClient | ||
|
||
return _graphQLClient | ||
} | ||
|
||
export function useGraphQLClient(initialState) { | ||
const store = useMemo(() => initializeGraphQL(initialState), [initialState]) | ||
return store | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const defaultOpts = { useCache: true } | ||
/** | ||
* Returns the result of a GraphQL query. It also adds the result to the | ||
* cache of the GraphQL client for better initial data population in pages. | ||
* | ||
* Note: This helper tries to imitate what the query hooks of `graphql-hooks` | ||
* do internally to make sure we generate the same cache key | ||
*/ | ||
export default async function graphQLRequest(client, query, options) { | ||
const opts = { ...defaultOpts, ...options } | ||
const operation = { | ||
query, | ||
variables: opts.variables, | ||
operationName: opts.operationName, | ||
persisted: opts.persisted, | ||
} | ||
|
||
if (opts.persisted || (client.useGETForQueries && !opts.isMutation)) { | ||
opts.fetchOptionsOverrides = { | ||
...opts.fetchOptionsOverrides, | ||
method: 'GET', | ||
} | ||
} | ||
|
||
const cacheKey = client.getCacheKey(operation, opts) | ||
const cacheValue = await client.request(operation, opts) | ||
|
||
client.saveCache(cacheKey, cacheValue) | ||
return cacheValue | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,18 @@ | ||
{ | ||
"name": "with-graphql-hooks", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"dev": "next", | ||
"build": "next build", | ||
"start": "next start" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"graphql-hooks": "^4.4.4", | ||
"graphql-hooks-memcache": "^1.3.2", | ||
"graphql-hooks-ssr": "^1.1.5", | ||
"next": "latest", | ||
"prop-types": "^15.7.2", | ||
"react": "^16.8.2", | ||
"react-dom": "^16.8.2" | ||
}, | ||
"browser": { | ||
"graphql-hooks-ssr": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,12 @@ | ||
import App from 'next/app' | ||
import withGraphQLClient from '../lib/with-graphql-client' | ||
import { ClientContext } from 'graphql-hooks' | ||
import { useGraphQLClient } from '../lib/graphql-client' | ||
|
||
class MyApp extends App { | ||
render() { | ||
const { Component, pageProps, graphQLClient } = this.props | ||
return ( | ||
<ClientContext.Provider value={graphQLClient}> | ||
<Component {...pageProps} /> | ||
</ClientContext.Provider> | ||
) | ||
} | ||
} | ||
export default function App({ Component, pageProps }) { | ||
const graphQLClient = useGraphQLClient(pageProps.initialGraphQLState) | ||
|
||
export default withGraphQLClient(MyApp) | ||
return ( | ||
<ClientContext.Provider value={graphQLClient}> | ||
<Component {...pageProps} /> | ||
</ClientContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters