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,
|
|
|
|
|
|
GraphQLList,
|
|
|
|
|
|
GraphQLNonNull
|
2016-08-31 06:33:29 +00:00
|
|
|
|
} from 'graphql'
|
2016-09-01 08:39:27 +00:00
|
|
|
|
import {
|
|
|
|
|
|
globalIdField,
|
|
|
|
|
|
connectionArgs,
|
|
|
|
|
|
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'
|
2016-09-01 08:39:27 +00:00
|
|
|
|
import { ArtistConnection } from './artist'
|
|
|
|
|
|
import { EventConnection } from './event'
|
|
|
|
|
|
import { LabelConnection } from './label'
|
2016-08-20 05:59:32 +00:00
|
|
|
|
import LifeSpan from './life-span'
|
2016-09-01 08:39:27 +00:00
|
|
|
|
import { PlaceConnection } from './place'
|
|
|
|
|
|
import { RecordingConnection } from './recording'
|
2016-11-26 01:38:32 +00:00
|
|
|
|
import { RelationshipConnection } from './relationship'
|
2016-09-01 08:39:27 +00:00
|
|
|
|
import { ReleaseConnection } from './release'
|
|
|
|
|
|
import { ReleaseGroupConnection } from './release-group'
|
|
|
|
|
|
import { WorkConnection } from './work'
|
2016-08-20 05:59:32 +00:00
|
|
|
|
import {
|
|
|
|
|
|
linkedResolver,
|
2016-11-26 01:38:32 +00:00
|
|
|
|
relationshipResolver,
|
|
|
|
|
|
includeRelationships
|
2016-08-20 05:59:32 +00:00
|
|
|
|
} from '../resolvers'
|
|
|
|
|
|
|
2016-11-26 01:38:32 +00:00
|
|
|
|
export const toPascal = pascalCase
|
|
|
|
|
|
export const toDashed = dashify
|
|
|
|
|
|
|
|
|
|
|
|
export function toPlural (name) {
|
|
|
|
|
|
return name.endsWith('s') ? name : name + 's'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function toSingular (name) {
|
|
|
|
|
|
return name.endsWith('s') && !/series/i.test(name) ? name.slice(0, -1) : name
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function toWords (name) {
|
|
|
|
|
|
return toPascal(name).replace(/([^A-Z])?([A-Z]+)/g, (match, tail, head) => {
|
|
|
|
|
|
tail = tail ? tail + ' ' : ''
|
|
|
|
|
|
head = head.length > 1 ? head : head.toLowerCase()
|
|
|
|
|
|
return `${tail}${head}`
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2016-08-31 06:33:29 +00:00
|
|
|
|
|
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
|
2016-11-26 01:38:32 +00:00
|
|
|
|
const singularName = isPlural ? toSingular(name) : name
|
2016-08-08 07:54:06 +00:00
|
|
|
|
const idName = isPlural ? `${singularName}IDs` : `${name}ID`
|
|
|
|
|
|
const idConfig = {
|
|
|
|
|
|
type: isPlural ? new GraphQLList(MBID) : MBID,
|
2016-11-26 01:38:32 +00:00
|
|
|
|
description: `The MBID${isPlural ? 's' : ''} associated with the
|
|
|
|
|
|
value${isPlural ? 's' : ''} of the \`${name}\` field.`,
|
2016-08-08 07:54:06 +00:00
|
|
|
|
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-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),
|
2016-09-01 08:39:27 +00:00
|
|
|
|
description: 'The MBID of the entity.',
|
2016-09-01 04:31:48 +00:00
|
|
|
|
resolve: source => source.id
|
|
|
|
|
|
}
|
2016-11-26 01:38:32 +00:00
|
|
|
|
export const name = {
|
|
|
|
|
|
type: GraphQLString,
|
|
|
|
|
|
description: 'The official name of the entity.'
|
|
|
|
|
|
}
|
|
|
|
|
|
export const sortName = {
|
|
|
|
|
|
type: GraphQLString,
|
|
|
|
|
|
description: `The string to use for the purpose of ordering by name (for
|
|
|
|
|
|
example, by moving articles like ‘the’ to the end or a person’s last name to
|
|
|
|
|
|
the front).`,
|
|
|
|
|
|
resolve: getHyphenated
|
|
|
|
|
|
}
|
|
|
|
|
|
export const title = {
|
|
|
|
|
|
type: GraphQLString,
|
|
|
|
|
|
description: 'The official title of the entity.'
|
|
|
|
|
|
}
|
|
|
|
|
|
export const disambiguation = {
|
|
|
|
|
|
type: GraphQLString,
|
|
|
|
|
|
description: 'A comment used to help distinguish identically named entitites.'
|
|
|
|
|
|
}
|
|
|
|
|
|
export const lifeSpan = {
|
|
|
|
|
|
type: LifeSpan,
|
|
|
|
|
|
description: `The begin and end dates of the entity’s existence. Its exact
|
|
|
|
|
|
meaning depends on the type of entity.`,
|
|
|
|
|
|
resolve: getHyphenated
|
|
|
|
|
|
}
|
2016-08-20 05:59:32 +00:00
|
|
|
|
|
2016-09-01 08:39:27 +00:00
|
|
|
|
function linkedQuery (connectionType, args) {
|
2016-11-26 01:38:32 +00:00
|
|
|
|
const typeName = toWords(connectionType.name.slice(0, -10))
|
2016-09-01 08:39:27 +00:00
|
|
|
|
return {
|
|
|
|
|
|
type: connectionType,
|
2016-11-26 01:38:32 +00:00
|
|
|
|
description: `A list of ${typeName} entities linked to this entity.`,
|
2016-09-01 08:39:27 +00:00
|
|
|
|
args: {
|
|
|
|
|
|
...forwardConnectionArgs,
|
|
|
|
|
|
...args
|
|
|
|
|
|
},
|
|
|
|
|
|
resolve: linkedResolver()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-26 01:38:32 +00:00
|
|
|
|
export const relationship = {
|
|
|
|
|
|
type: RelationshipConnection,
|
|
|
|
|
|
description: 'A list of relationships between these two entity types.',
|
2016-08-20 05:59:32 +00:00
|
|
|
|
args: {
|
2016-09-01 08:39:27 +00:00
|
|
|
|
...connectionArgs,
|
2016-11-26 01:38:32 +00:00
|
|
|
|
direction: {
|
|
|
|
|
|
type: GraphQLString,
|
|
|
|
|
|
description: 'Filter by the relationship direction.'
|
|
|
|
|
|
},
|
|
|
|
|
|
...fieldWithID('type', {
|
|
|
|
|
|
description: 'Filter by the relationship type.'
|
|
|
|
|
|
})
|
2016-08-20 05:59:32 +00:00
|
|
|
|
},
|
2016-11-26 01:38:32 +00:00
|
|
|
|
resolve: relationshipResolver()
|
2016-08-20 05:59:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-26 01:38:32 +00:00
|
|
|
|
export const relationships = {
|
2016-08-20 05:59:32 +00:00
|
|
|
|
type: new GraphQLObjectType({
|
2016-11-26 01:38:32 +00:00
|
|
|
|
name: 'Relationships',
|
|
|
|
|
|
description: 'Lists of entity relationships for each entity type.',
|
2016-08-20 05:59:32 +00:00
|
|
|
|
fields: () => ({
|
2016-11-26 01:38:32 +00:00
|
|
|
|
areas: relationship,
|
|
|
|
|
|
artists: relationship,
|
|
|
|
|
|
events: relationship,
|
|
|
|
|
|
instruments: relationship,
|
|
|
|
|
|
labels: relationship,
|
|
|
|
|
|
places: relationship,
|
|
|
|
|
|
recordings: relationship,
|
|
|
|
|
|
releases: relationship,
|
|
|
|
|
|
releaseGroups: relationship,
|
|
|
|
|
|
series: relationship,
|
|
|
|
|
|
urls: relationship,
|
|
|
|
|
|
works: relationship
|
2016-08-20 05:59:32 +00:00
|
|
|
|
})
|
|
|
|
|
|
}),
|
2016-11-26 01:38:32 +00:00
|
|
|
|
description: 'Relationships between this entity and other entitites.',
|
|
|
|
|
|
resolve: (source, args, { loaders }, info) => {
|
2016-08-20 05:59:32 +00:00
|
|
|
|
if (source.relations != null) {
|
|
|
|
|
|
return source.relations
|
|
|
|
|
|
}
|
2016-11-26 01:38:32 +00:00
|
|
|
|
const entityType = toDashed(info.parentType.name)
|
2016-08-20 05:59:32 +00:00
|
|
|
|
const id = source.id
|
2016-11-26 01:38:32 +00:00
|
|
|
|
const params = includeRelationships({}, info)
|
|
|
|
|
|
return loaders.lookup.load([entityType, id, params]).then(entity => {
|
2016-08-20 05:59:32 +00:00
|
|
|
|
return entity.relations
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const artistCredit = {
|
|
|
|
|
|
type: new GraphQLList(ArtistCredit),
|
2016-11-26 01:38:32 +00:00
|
|
|
|
description: 'The main credited artist(s).',
|
|
|
|
|
|
resolve: (source, args, { loaders }, info) => {
|
2016-08-20 05:59:32 +00:00
|
|
|
|
const key = 'artist-credit'
|
|
|
|
|
|
if (key in source) {
|
|
|
|
|
|
return source[key]
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const { entityType, id } = source
|
|
|
|
|
|
const params = { inc: ['artists'] }
|
2016-11-26 01:38:32 +00:00
|
|
|
|
return loaders.lookup.load([entityType, id, params]).then(entity => {
|
2016-08-20 05:59:32 +00:00
|
|
|
|
return entity[key]
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-09-01 08:39:27 +00:00
|
|
|
|
export const artists = linkedQuery(ArtistConnection)
|
|
|
|
|
|
export const events = linkedQuery(EventConnection)
|
|
|
|
|
|
export const labels = linkedQuery(LabelConnection)
|
|
|
|
|
|
export const places = linkedQuery(PlaceConnection)
|
|
|
|
|
|
export const recordings = linkedQuery(RecordingConnection)
|
|
|
|
|
|
export const releases = linkedQuery(ReleaseConnection, {
|
2016-11-26 01:38:32 +00:00
|
|
|
|
type: { type: new GraphQLList(ReleaseGroupType) },
|
|
|
|
|
|
status: { type: new GraphQLList(ReleaseStatus) }
|
2016-09-01 08:39:27 +00:00
|
|
|
|
})
|
|
|
|
|
|
export const releaseGroups = linkedQuery(ReleaseGroupConnection, {
|
2016-11-26 01:38:32 +00:00
|
|
|
|
type: { type: new GraphQLList(ReleaseGroupType) }
|
2016-09-01 08:39:27 +00:00
|
|
|
|
})
|
|
|
|
|
|
export const works = linkedQuery(WorkConnection)
|