import { Network , CacheConfig , RequestNode , UploadableMap , Variables , Observable , Sink , GraphQLResponse , } from 'relay-runtime' ; import Config from '../../core/Config' ; import CacheResolver from './CacheResolver' ; import FetchResolver from './FetchResolver' ; import SSRResolver from './SSRResolver' ; export interface NetworkResolverInterface { resolve ( request , variables , cacheConfig , uploadables , sink ) ; } const createNetwork = ( config ) : Network => { const resolvers = [ new SSRResolver ( ) , new CacheResolver ( config ) , new FetchResolver ( config ) , ] ; return Network . create ( ( request , variables , cacheConfig , uploadables ? ) => new Promise ( ( resolve , reject ) => { let sink ; let data ; let currentResolver = - 1 ; Observable . create ( ( _sink ) => { sink = _sink ; _sink . next ( ) ; } ) . subscribe ( { next : ( _data ) => { if ( _data ) { data = _data ; return ; } currentResolver += 1 ; const resolver = resolvers [ currentResolver ] ; if ( ! resolver ) { sink . complete ( ) ; return ; } resolver . resolve ( request , variables , cacheConfig , uploadables , sink ) ; } , complete : ( ) => resolve ( data ) , error : reject , } ) ; } ) ) ; } ; export default createNetwork ;