graphbrainz/src/types/entity.js
Brian Beck c3be2a2e98
More resolveType fixes, this time for Relay's nodeDefinitions (#51)
* More resolveType fixes, this time for Relay's nodeDefinitions

* Add a loadExtension helper

* Use createContext in test helpers
2017-11-18 00:35:28 -08:00

29 lines
927 B
JavaScript

import { GraphQLInterfaceType } from 'graphql'
import { mbid, connectionWithExtras } from './helpers'
const debug = require('debug')('graphbrainz:types/entity')
const Entity = new GraphQLInterfaceType({
name: 'Entity',
description: 'An entity in the MusicBrainz schema.',
resolveType(value, context, info) {
if (value._type) {
let originalType
try {
originalType = require(`./${value._type}`).default
} catch (err) {
debug(`Failed to load type: ${value._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]
}
},
fields: () => ({ mbid })
})
export const EntityConnection = connectionWithExtras(Entity)
export default Entity