-
Notifications
You must be signed in to change notification settings - Fork 27.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Examples] Move with-graphql-hooks to SSG #13858
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bmullan91 Would be nice to have a better alternative to do this directly in
graphql-hooks
Context:
graphql-hooks-ssr
doesn't work well with data fetching methods in Next.js, as we don't give access to theApp
component, instead the query or queries that are required to be server rendered get requested with this method before render, and the data is later added (by using the cache) to the GraphQL client in the browser as initial state.