graphbrainz/src/types/node.js

37 lines
1 KiB
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')
const TYPE_MODULES = {
discid: 'disc'
}
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, context, info) => {
const type = TYPE_MODULES[obj._type] || obj._type
if (type) {
let originalType
try {
originalType = require(`./${type}`).default
} catch (err) {
debug(`Failed to load type: ${type}`)
return
}
// Don't use `originalType`! The schema may have been extended in which
// case the types have all been replaced. Instead, find the current type
// of the same name.
const typeMap = info.schema.getTypeMap()
return typeMap[originalType.name]
2016-08-31 06:33:29 +00:00
}
}
)
export default nodeInterface
export { nodeInterface, nodeField }