diff --git a/examples/with-react-relay-network-modern/lib/createEnvironment/client.js b/examples/with-react-relay-network-modern/lib/createEnvironment/client.js
index 5e4e28e44c369..ae31eaa715d19 100644
--- a/examples/with-react-relay-network-modern/lib/createEnvironment/client.js
+++ b/examples/with-react-relay-network-modern/lib/createEnvironment/client.js
@@ -3,18 +3,16 @@ import {
cacheMiddleware,
urlMiddleware,
} from 'react-relay-network-modern/node8'
+import RelaySSR from 'react-relay-network-modern-ssr/node8/client'
import { Environment, RecordSource, Store } from 'relay-runtime'
-let store, source
+const source = new RecordSource()
+const store = new Store(source)
let storeEnvironment = null
export default {
- createEnvironment: (records) => {
- if (!store) {
- source = new RecordSource(records)
- store = new Store(source)
- }
+ createEnvironment: (relayData) => {
if (storeEnvironment) return storeEnvironment
storeEnvironment = new Environment({
@@ -24,6 +22,9 @@ export default {
size: 100,
ttl: 60 * 1000,
}),
+ new RelaySSR(relayData).getMiddleware({
+ lookup: false,
+ }),
urlMiddleware({
url: (req) => process.env.NEXT_PUBLIC_RELAY_ENDPOINT,
}),
diff --git a/examples/with-react-relay-network-modern/lib/createEnvironment/server.js b/examples/with-react-relay-network-modern/lib/createEnvironment/server.js
index cf8c719c8aa5f..72f162996826b 100644
--- a/examples/with-react-relay-network-modern/lib/createEnvironment/server.js
+++ b/examples/with-react-relay-network-modern/lib/createEnvironment/server.js
@@ -2,35 +2,35 @@ import {
RelayNetworkLayer,
urlMiddleware,
} from 'react-relay-network-modern/node8'
-import { Environment, RecordSource, Store } from 'relay-runtime'
+import RelaySSR from 'react-relay-network-modern-ssr/node8/server'
+import { Network, Environment, RecordSource, Store } from 'relay-runtime'
export default {
initEnvironment: () => {
const source = new RecordSource()
const store = new Store(source)
+ const relaySSR = new RelaySSR()
return {
+ relaySSR,
environment: new Environment({
store,
network: new RelayNetworkLayer([
urlMiddleware({
url: (req) => process.env.NEXT_PUBLIC_RELAY_ENDPOINT,
}),
+ relaySSR.getMiddleware(),
]),
}),
}
},
- createEnvironment: (records) => {
- const source = new RecordSource(records)
+ createEnvironment: (relayData) => {
+ const source = new RecordSource()
const store = new Store(source)
return new Environment({
store,
- network: new RelayNetworkLayer([
- urlMiddleware({
- url: (req) => process.env.NEXT_PUBLIC_RELAY_ENDPOINT,
- }),
- ]),
+ network: Network.create(() => relayData?.[0][1] || Promise.resolve()),
})
},
}
diff --git a/examples/with-react-relay-network-modern/package.json b/examples/with-react-relay-network-modern/package.json
index 01bc838a31149..32695eafc3236 100644
--- a/examples/with-react-relay-network-modern/package.json
+++ b/examples/with-react-relay-network-modern/package.json
@@ -18,7 +18,8 @@
"react-dom": "^16.13.0",
"react-relay": "^9.0.0",
"react-relay-network-modern": "^4.5.0",
- "react-relay-network-modern-ssr": "^1.4.0"
+ "react-relay-network-modern-ssr": "^1.4.0",
+ "relay-hooks": "3.5.2"
},
"devDependencies": {
"babel-plugin-relay": "^9.0.0",
diff --git a/examples/with-react-relay-network-modern/pages/_app.js b/examples/with-react-relay-network-modern/pages/_app.js
index 67005c7ffc503..5709ef97a1fc6 100644
--- a/examples/with-react-relay-network-modern/pages/_app.js
+++ b/examples/with-react-relay-network-modern/pages/_app.js
@@ -1,6 +1,13 @@
+import { RelayEnvironmentProvider } from 'relay-hooks'
+
import { createEnvironment } from '../lib/createEnvironment'
export default function App({ Component, pageProps }) {
- const environment = createEnvironment(pageProps.records)
- return
+ return (
+
+
+
+ )
}
diff --git a/examples/with-react-relay-network-modern/pages/index.js b/examples/with-react-relay-network-modern/pages/index.js
index 9c843ea2a025a..917e329c70e13 100644
--- a/examples/with-react-relay-network-modern/pages/index.js
+++ b/examples/with-react-relay-network-modern/pages/index.js
@@ -1,4 +1,6 @@
-import { graphql, QueryRenderer, fetchQuery } from 'react-relay'
+import { graphql, fetchQuery } from 'react-relay'
+import { useQuery } from 'relay-hooks'
+
import { initEnvironment } from '../lib/createEnvironment'
import BlogPosts from '../components/BlogPosts'
@@ -10,27 +12,28 @@ const query = graphql`
}
`
-const Index = ({ environment }) => (
- {
- if (error) return {error.message}
- else if (props) return
- return Loading
- }}
- />
-)
+const Index = ({ environment }) => {
+ const { error, props } = useQuery(query)
+
+ if (error) return {error.message}
+
+ if (!props) return Loading
+
+ return
+}
export async function getStaticProps() {
- const { environment } = initEnvironment()
+ const { environment, relaySSR } = initEnvironment()
await fetchQuery(environment, query)
- const records = environment.getStore().getSource().toJSON()
+ const relayData = (await relaySSR.getCache())?.[0]
- return { props: { records } }
+ return {
+ props: {
+ relayData: !relayData ? null : [[relayData[0], relayData[1].json]],
+ },
+ }
}
export default Index