import { fromGlobalId , nodeDefinitions } from 'graphql-relay' ; import DataLoader from 'dataloader' ; import { GraphQLObjectType } from 'graphql' ; import * as loaders from '../loader' ; import { GraphQLContext } from '../TypeDefinition' ; type RegisteredTypes = { [ key ] : GraphQLObjectType ; } ; const registeredTypes = { } ; export function registerType ( type ) { registeredTypes [ type . name ] = type ; return type ; } type Loader = { load : ( context : GraphQLContext , id : string ) => Promise < any > ; getLoader : ( ) => DataLoader < string , any > ; } ; export type Loaders = { [ key ] : Loader ; } ; export const { nodeField , nodeInterface } = nodeDefinitions ( ( globalId , context ) => { const { type , id } = fromGlobalId ( globalId ) ; const loader = ( loaders as Loaders ) [ `${ type }Loader` ] ; return ( loader && loader . load ( context , id ) ) || null ; } , object => registeredTypes [ object . constructor . name ] || null ) ;