Skip to content

Commit

Permalink
use records to init store (#11343)
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhsaxena authored Mar 25, 2020
1 parent 523680c commit b46abed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ 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'

const source = new RecordSource()
const store = new Store(source)
let store, source

let storeEnvironment = null

export default {
createEnvironment: relayData => {
createEnvironment: records => {
if (!store) {
source = new RecordSource(records)
store = new Store(source)
}
if (storeEnvironment) return storeEnvironment

storeEnvironment = new Environment({
Expand All @@ -22,9 +24,6 @@ export default {
size: 100,
ttl: 60 * 1000,
}),
new RelaySSR(relayData).getMiddleware({
lookup: false,
}),
urlMiddleware({
url: req => process.env.RELAY_ENDPOINT,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,35 @@ import {
RelayNetworkLayer,
urlMiddleware,
} from 'react-relay-network-modern/node8'
import RelaySSR from 'react-relay-network-modern-ssr/node8/server'
import { Network, Environment, RecordSource, Store } from 'relay-runtime'
import { 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.RELAY_ENDPOINT,
}),
relaySSR.getMiddleware(),
]),
}),
}
},
createEnvironment: (relayData, key) => {
const source = new RecordSource()
createEnvironment: records => {
const source = new RecordSource(records)
const store = new Store(source)

return new Environment({
store,
network: Network.create(
() => relayData.find(([dataKey]) => dataKey === key)[1]
),
network: new RelayNetworkLayer([
urlMiddleware({
url: req => process.env.RELAY_ENDPOINT,
}),
]),
})
},
}
20 changes: 10 additions & 10 deletions examples/with-react-relay-network-modern/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ export default class App extends NextApp {

try {
if (initEnvironment && Component.query) {
const { environment, relaySSR } = initEnvironment()
const { environment } = initEnvironment()

await fetchQuery(environment, Component.query, variables)

const records = environment
.getStore()
.getSource()
.toJSON()

return {
variables,
relayData: await relaySSR.getCache(),
records,
}
}
} catch (e) {
Expand All @@ -31,17 +36,12 @@ export default class App extends NextApp {
}

render() {
const { Component, variables = {}, relayData } = this.props
const environment = createEnvironment(
relayData,
JSON.stringify({
queryID: Component.query ? Component.query.params.name : undefined,
variables,
})
)
const { Component, variables = {}, records } = this.props
const environment = createEnvironment(records)

return (
<QueryRenderer
fetchPolicy="store-and-network"
environment={environment}
query={Component.query}
variables={variables}
Expand Down

0 comments on commit b46abed

Please sign in to comment.