forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
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-relay-modern to SSG (vercel#13882)
Related to vercel#11014
- Loading branch information
Showing
12 changed files
with
97 additions
and
134 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
RELAY_ENDPOINT=https://api.graph.cool/relay/v1/next-js-with-relay-modern-example | ||
NEXT_PUBLIC_RELAY_ENDPOINT=https://api.graph.cool/relay/v1/next-js-with-relay-modern-example |
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 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useMemo } from 'react' | ||
import { Environment, Network, RecordSource, Store } from 'relay-runtime' | ||
|
||
let relayEnvironment | ||
|
||
// Define a function that fetches the results of an operation (query/mutation/etc) | ||
// and returns its results as a Promise | ||
function fetchQuery(operation, variables, cacheConfig, uploadables) { | ||
return fetch(process.env.NEXT_PUBLIC_RELAY_ENDPOINT, { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, // Add authentication and other headers here | ||
body: JSON.stringify({ | ||
query: operation.text, // GraphQL text from input | ||
variables, | ||
}), | ||
}).then((response) => response.json()) | ||
} | ||
|
||
function createEnvironment(initialRecords) { | ||
return new Environment({ | ||
// Create a network layer from the fetch function | ||
network: Network.create(fetchQuery), | ||
store: new Store(new RecordSource()), | ||
}) | ||
} | ||
|
||
export function initEnvironment(initialRecords) { | ||
// Create a network layer from the fetch function | ||
const environment = relayEnvironment ?? createEnvironment(initialRecords) | ||
|
||
// If your page has Next.js data fetching methods that use Relay, the initial records | ||
// will get hydrated here | ||
if (initialRecords) { | ||
environment.getStore().publish(new RecordSource(initialRecords)) | ||
} | ||
// For SSG and SSR always create a new Relay environment | ||
if (typeof window === 'undefined') return environment | ||
// Create the Relay environment once in the client | ||
if (!relayEnvironment) relayEnvironment = environment | ||
|
||
return relayEnvironment | ||
} | ||
|
||
export function useEnvironment(initialRecords) { | ||
const store = useMemo(() => initEnvironment(initialRecords), [initialRecords]) | ||
return store | ||
} |
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
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,12 @@ | ||
import { ReactRelayContext } from 'react-relay' | ||
import { useEnvironment } from '../lib/relay' | ||
|
||
export default function App({ Component, pageProps }) { | ||
const environment = useEnvironment(pageProps.initialRecords) | ||
|
||
return ( | ||
<ReactRelayContext.Provider value={{ environment, variables: {} }}> | ||
<Component {...pageProps} /> | ||
</ReactRelayContext.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Link from 'next/link' | ||
|
||
export default function About() { | ||
return ( | ||
<div> | ||
<Link href="/"> | ||
<a>Home</a> | ||
</Link> | ||
<p>This is the about page</p> | ||
</div> | ||
) | ||
} |
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,13 +1,29 @@ | ||
import withData from '../lib/withData' | ||
import Link from 'next/link' | ||
import { fetchQuery } from 'react-relay' | ||
import { initEnvironment } from '../lib/relay' | ||
import BlogPosts from '../components/BlogPosts' | ||
import indexPageQuery from '../queries/indexPage' | ||
|
||
const Index = ({ viewer }) => ( | ||
<div> | ||
<Link href="/about"> | ||
<a>About</a> | ||
</Link> | ||
<BlogPosts viewer={viewer} /> | ||
</div> | ||
) | ||
|
||
export default withData(Index, { | ||
query: indexPageQuery, | ||
}) | ||
export async function getStaticProps() { | ||
const environment = initEnvironment() | ||
const queryProps = await fetchQuery(environment, indexPageQuery) | ||
const initialRecords = environment.getStore().getSource().toJSON() | ||
|
||
return { | ||
props: { | ||
...queryProps, | ||
initialRecords, | ||
}, | ||
} | ||
} | ||
|
||
export default Index |