graphbrainz/src/types/helpers.js

171 lines
4.5 KiB
JavaScript
Raw Normal View History

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-08-20 05:59:32 +00:00
import Relation from './relation'
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,
relationResolver,
includeRelations
} from '../resolvers'
2016-08-31 06:33:29 +00:00
export const toNodeType = pascalCase
export const toEntityType = dashify
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-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-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 }
2016-09-01 08:39:27 +00:00
function linkedQuery (connectionType, args) {
return {
type: connectionType,
args: {
...forwardConnectionArgs,
...args
},
resolve: linkedResolver()
}
}
2016-08-20 05:59:32 +00:00
export const relation = {
type: new GraphQLList(Relation),
args: {
2016-09-01 08:39:27 +00:00
...connectionArgs,
2016-08-20 05:59:32 +00:00
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
}
2016-09-01 08:39:27 +00:00
const entityType = toEntityType(info.parentType.name)
2016-08-20 05:59:32 +00:00
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]
})
}
}
}
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)
2016-08-20 05:59:32 +00:00
2016-09-01 08:39:27 +00:00
export const releases = linkedQuery(ReleaseConnection, {
type: { type: ReleaseGroupType },
types: { type: new GraphQLList(ReleaseGroupType) },
status: { type: ReleaseStatus },
statuses: { type: new GraphQLList(ReleaseStatus) }
})
2016-08-20 05:59:32 +00:00
2016-09-01 08:39:27 +00:00
export const releaseGroups = linkedQuery(ReleaseGroupConnection, {
type: { type: ReleaseGroupType },
types: { type: new GraphQLList(ReleaseGroupType) }
})
2016-08-20 05:59:32 +00:00
2016-09-01 08:39:27 +00:00
export const works = linkedQuery(WorkConnection)