2016-08-08 07:54:06 +00:00
|
|
|
import dashify from 'dashify'
|
2016-08-31 06:33:29 +00:00
|
|
|
import pascalCase from 'pascalcase'
|
2016-08-20 05:59:32 +00:00
|
|
|
import {
|
|
|
|
|
GraphQLObjectType,
|
|
|
|
|
GraphQLString,
|
|
|
|
|
GraphQLInt,
|
|
|
|
|
GraphQLList,
|
|
|
|
|
GraphQLNonNull
|
2016-08-31 06:33:29 +00:00
|
|
|
} from 'graphql'
|
2016-09-01 04:31:48 +00:00
|
|
|
import { globalIdField, forwardConnectionArgs } from 'graphql-relay'
|
2016-08-31 06:33:29 +00:00
|
|
|
import { MBID } from './scalars'
|
2016-08-20 05:59:32 +00:00
|
|
|
import { ReleaseGroupType, ReleaseStatus } from './enums'
|
|
|
|
|
import ArtistCredit from './artist-credit'
|
|
|
|
|
import Artist from './artist'
|
|
|
|
|
import Event from './event'
|
|
|
|
|
import Label from './label'
|
|
|
|
|
import LifeSpan from './life-span'
|
|
|
|
|
import Place from './place'
|
|
|
|
|
import Recording from './recording'
|
|
|
|
|
import Relation from './relation'
|
|
|
|
|
import Release from './release'
|
|
|
|
|
import ReleaseGroup from './release-group'
|
|
|
|
|
import Work from './work'
|
|
|
|
|
import {
|
|
|
|
|
lookupResolver,
|
|
|
|
|
linkedResolver,
|
|
|
|
|
relationResolver,
|
|
|
|
|
searchResolver,
|
|
|
|
|
includeRelations
|
|
|
|
|
} from '../resolvers'
|
|
|
|
|
|
2016-08-31 06:33:29 +00:00
|
|
|
export const toNodeType = pascalCase
|
|
|
|
|
export const toEntityType = dashify
|
|
|
|
|
|
2016-08-20 05:59:32 +00:00
|
|
|
export function getByline (data) {
|
|
|
|
|
const credit = data['artist-credit']
|
|
|
|
|
if (credit && credit.length) {
|
|
|
|
|
return credit.reduce((byline, credit) => {
|
|
|
|
|
return byline + credit.name + credit.joinphrase
|
|
|
|
|
}, '')
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-08 07:54:06 +00:00
|
|
|
|
|
|
|
|
export function fieldWithID (name, config = {}) {
|
|
|
|
|
config = {
|
|
|
|
|
type: GraphQLString,
|
|
|
|
|
resolve: getHyphenated,
|
|
|
|
|
...config
|
|
|
|
|
}
|
|
|
|
|
const isPlural = config.type instanceof GraphQLList
|
|
|
|
|
const singularName = isPlural && name.endsWith('s') ? name.slice(0, -1) : name
|
|
|
|
|
const idName = isPlural ? `${singularName}IDs` : `${name}ID`
|
|
|
|
|
const idConfig = {
|
|
|
|
|
type: isPlural ? new GraphQLList(MBID) : MBID,
|
|
|
|
|
resolve: getHyphenated
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
[name]: config,
|
|
|
|
|
[idName]: idConfig
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getHyphenated (source, args, context, info) {
|
|
|
|
|
const name = dashify(info.fieldName)
|
|
|
|
|
return source[name]
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-20 05:59:32 +00:00
|
|
|
export function getFallback (keys) {
|
|
|
|
|
return (source) => {
|
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
|
const key = keys[i]
|
|
|
|
|
if (key in source) {
|
|
|
|
|
return source[key]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-08 07:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-20 05:59:32 +00:00
|
|
|
export function lookupQuery (entity, params) {
|
|
|
|
|
return {
|
|
|
|
|
type: entity,
|
|
|
|
|
description: `Look up a specific ${entity.name} by its MBID.`,
|
|
|
|
|
args: { id },
|
|
|
|
|
resolve: lookupResolver(dashify(entity.name), params)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-01 04:31:48 +00:00
|
|
|
export function searchQuery (connectionType) {
|
2016-08-20 05:59:32 +00:00
|
|
|
return {
|
2016-09-01 04:31:48 +00:00
|
|
|
type: connectionType,
|
2016-08-20 05:59:32 +00:00
|
|
|
args: {
|
|
|
|
|
query: { type: new GraphQLNonNull(GraphQLString) },
|
2016-09-01 04:31:48 +00:00
|
|
|
...forwardConnectionArgs
|
2016-08-20 05:59:32 +00:00
|
|
|
},
|
|
|
|
|
resolve: searchResolver()
|
2016-08-08 07:54:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-08-20 05:59:32 +00:00
|
|
|
|
2016-08-31 06:33:29 +00:00
|
|
|
export const id = globalIdField()
|
2016-09-01 04:31:48 +00:00
|
|
|
export const mbid = {
|
|
|
|
|
type: new GraphQLNonNull(MBID),
|
|
|
|
|
resolve: source => source.id
|
|
|
|
|
}
|
2016-08-20 05:59:32 +00:00
|
|
|
export const name = { type: GraphQLString }
|
|
|
|
|
export const sortName = { type: GraphQLString, resolve: getHyphenated }
|
|
|
|
|
export const title = { type: GraphQLString }
|
|
|
|
|
export const disambiguation = { type: GraphQLString }
|
|
|
|
|
export const lifeSpan = { type: LifeSpan, resolve: getHyphenated }
|
|
|
|
|
|
|
|
|
|
export const relation = {
|
|
|
|
|
type: new GraphQLList(Relation),
|
|
|
|
|
args: {
|
|
|
|
|
limit: { type: GraphQLInt },
|
|
|
|
|
offset: { type: GraphQLInt },
|
|
|
|
|
direction: { type: GraphQLString },
|
|
|
|
|
type: { type: GraphQLString },
|
|
|
|
|
typeID: { type: MBID }
|
|
|
|
|
},
|
|
|
|
|
resolve: relationResolver()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const relations = {
|
|
|
|
|
type: new GraphQLObjectType({
|
|
|
|
|
name: 'Relations',
|
|
|
|
|
fields: () => ({
|
|
|
|
|
area: relation,
|
|
|
|
|
artist: relation,
|
|
|
|
|
event: relation,
|
|
|
|
|
instrument: relation,
|
|
|
|
|
label: relation,
|
|
|
|
|
place: relation,
|
|
|
|
|
recording: relation,
|
|
|
|
|
release: relation,
|
|
|
|
|
releaseGroup: relation,
|
|
|
|
|
series: relation,
|
|
|
|
|
url: relation,
|
|
|
|
|
work: relation
|
|
|
|
|
})
|
|
|
|
|
}),
|
|
|
|
|
resolve: (source, args, { lookupLoader }, info) => {
|
|
|
|
|
if (source.relations != null) {
|
|
|
|
|
return source.relations
|
|
|
|
|
}
|
|
|
|
|
const entityType = dashify(info.parentType.name)
|
|
|
|
|
const id = source.id
|
|
|
|
|
const params = includeRelations({}, info)
|
|
|
|
|
return lookupLoader.load([entityType, id, params]).then(entity => {
|
|
|
|
|
return entity.relations
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const artistCredit = {
|
|
|
|
|
type: new GraphQLList(ArtistCredit),
|
|
|
|
|
resolve: (source, args, { lookupLoader }, info) => {
|
|
|
|
|
const key = 'artist-credit'
|
|
|
|
|
if (key in source) {
|
|
|
|
|
return source[key]
|
|
|
|
|
} else {
|
|
|
|
|
const { entityType, id } = source
|
|
|
|
|
const params = { inc: ['artists'] }
|
|
|
|
|
return lookupLoader.load([entityType, id, params]).then(entity => {
|
|
|
|
|
return entity[key]
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const artists = {
|
|
|
|
|
type: new GraphQLList(Artist),
|
|
|
|
|
args: {
|
|
|
|
|
limit: { type: GraphQLInt },
|
|
|
|
|
offset: { type: GraphQLInt }
|
|
|
|
|
},
|
|
|
|
|
resolve: linkedResolver()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const events = {
|
|
|
|
|
type: new GraphQLList(Event),
|
|
|
|
|
args: {
|
|
|
|
|
limit: { type: GraphQLInt },
|
|
|
|
|
offset: { type: GraphQLInt }
|
|
|
|
|
},
|
|
|
|
|
resolve: linkedResolver()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const labels = {
|
|
|
|
|
type: new GraphQLList(Label),
|
|
|
|
|
args: {
|
|
|
|
|
limit: { type: GraphQLInt },
|
|
|
|
|
offset: { type: GraphQLInt }
|
|
|
|
|
},
|
|
|
|
|
resolve: linkedResolver()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const places = {
|
|
|
|
|
type: new GraphQLList(Place),
|
|
|
|
|
args: {
|
|
|
|
|
limit: { type: GraphQLInt },
|
|
|
|
|
offset: { type: GraphQLInt }
|
|
|
|
|
},
|
|
|
|
|
resolve: linkedResolver()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const recordings = {
|
|
|
|
|
type: new GraphQLList(Recording),
|
|
|
|
|
args: {
|
|
|
|
|
limit: { type: GraphQLInt },
|
|
|
|
|
offset: { type: GraphQLInt }
|
|
|
|
|
},
|
|
|
|
|
resolve: linkedResolver()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const releases = {
|
|
|
|
|
type: new GraphQLList(Release),
|
|
|
|
|
args: {
|
|
|
|
|
limit: { type: GraphQLInt },
|
|
|
|
|
offset: { type: GraphQLInt },
|
|
|
|
|
type: { type: ReleaseGroupType },
|
|
|
|
|
types: { type: new GraphQLList(ReleaseGroupType) },
|
|
|
|
|
status: { type: ReleaseStatus },
|
|
|
|
|
statuses: { type: new GraphQLList(ReleaseStatus) }
|
|
|
|
|
},
|
|
|
|
|
resolve: linkedResolver()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const releaseGroups = {
|
|
|
|
|
type: new GraphQLList(ReleaseGroup),
|
|
|
|
|
args: {
|
|
|
|
|
limit: { type: GraphQLInt },
|
|
|
|
|
offset: { type: GraphQLInt },
|
|
|
|
|
type: { type: ReleaseGroupType },
|
|
|
|
|
types: { type: new GraphQLList(ReleaseGroupType) }
|
|
|
|
|
},
|
|
|
|
|
resolve: linkedResolver()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const works = {
|
|
|
|
|
type: new GraphQLList(Work),
|
|
|
|
|
args: {
|
|
|
|
|
limit: { type: GraphQLInt },
|
|
|
|
|
offset: { type: GraphQLInt }
|
|
|
|
|
},
|
|
|
|
|
resolve: linkedResolver()
|
|
|
|
|
}
|