import { Network } from 'relay-runtime' ; import { Store , Environment , RecordSource } from 'relay-runtime' ; import fetch from 'isomorphic-unfetch' ; import { loadLazyQuery } from 'relay-hooks' ; let relayEnvironment ; function fetchQuery ( operation , variables , cacheConfig , uploadables ) { const endpoint = 'http://localhost:3000/graphql' ; return fetch ( endpoint , { method : 'POST' , headers : { Accept : 'application/json' , 'Content-Type' : 'application/json' , } , body : JSON . stringify ( { query : operation . text , variables , } ) , } ) . then ( response => response . json ( ) ) ; } type InitProps = { records ? : any ; } ; const network = Network . create ( fetchQuery ) ; function createEnvironment ( records ) { const recordSource = new RecordSource ( records ) ; const store = new Store ( recordSource ) ; const environment = new Environment ( { network , store , } ) ; return environment ; } const prefetch = loadLazyQuery ( ) ; export default function initEnvironment ( options = { } ) { const { records = { } } = options ; if ( typeof window === 'undefined' ) { prefetch . dispose ( ) ; return { environment : createEnvironment ( records ) , prefetch } ; } if ( ! relayEnvironment ) { relayEnvironment = createEnvironment ( records ) ; } return { environment : relayEnvironment , prefetch } ; }