graphbrainz/src/types/node.js

24 lines
618 B
JavaScript
Raw Normal View History

2016-08-31 06:33:29 +00:00
import { nodeDefinitions, fromGlobalId } from 'graphql-relay'
2016-11-26 01:38:32 +00:00
import { toDashed } from './helpers'
2016-08-31 06:33:29 +00:00
2016-12-12 05:56:28 +00:00
const debug = require('debug')('graphbrainz:types/node')
2016-08-31 06:33:29 +00:00
const { nodeInterface, nodeField } = nodeDefinitions(
2016-11-26 01:38:32 +00:00
(globalID, { loaders }) => {
2016-08-31 06:33:29 +00:00
const { type, id } = fromGlobalId(globalID)
2016-11-26 01:38:32 +00:00
const entityType = toDashed(type)
return loaders.lookup.load([entityType, id])
2016-08-31 06:33:29 +00:00
},
(obj) => {
try {
return require(`./${obj._type}`).default
2016-08-31 06:33:29 +00:00
} catch (err) {
2016-12-12 05:56:28 +00:00
debug(`Failed to load type: ${obj._type}`)
2016-08-31 06:33:29 +00:00
return null
}
}
)
export default nodeInterface
export { nodeInterface, nodeField }