graphbrainz/src/resolvers.js

131 lines
3.9 KiB
JavaScript
Raw Normal View History

2016-11-26 01:38:32 +00:00
import { toDashed, toSingular } from './types/helpers'
2016-09-01 08:39:27 +00:00
import {
getOffsetWithDefault,
connectionFromArray,
connectionFromArraySlice
} from 'graphql-relay'
2016-08-20 05:59:32 +00:00
import { getFields, extendIncludes } from './util'
2016-11-26 01:38:32 +00:00
export function includeRelationships (params, info) {
2016-08-20 05:59:32 +00:00
let fields = getFields(info)
2016-11-26 01:38:32 +00:00
if (info.fieldName !== 'relationships') {
if (fields.relationships) {
fields = getFields(fields.relationships)
2016-08-20 05:59:32 +00:00
} else {
return params
}
}
if (fields) {
2016-11-26 01:38:32 +00:00
const relationships = Object.keys(fields)
const includeRels = relationships.map(field => {
return `${toDashed(toSingular(field))}-rels`
})
2016-08-20 05:59:32 +00:00
if (includeRels.length) {
params = {
...params,
inc: extendIncludes(params.inc, includeRels)
}
}
}
return params
}
export function includeSubqueries (params, info) {
const fields = getFields(info)
if (fields.artistCredit) {
params = {
...params,
inc: extendIncludes(params.inc, ['artist-credits'])
}
}
return params
}
2016-09-01 08:39:27 +00:00
export function lookupResolver () {
2016-11-26 01:38:32 +00:00
return (root, { mbid }, { loaders }, info) => {
const entityType = toDashed(info.fieldName)
const params = includeRelationships({}, info)
return loaders.lookup.load([entityType, mbid, params])
2016-08-20 05:59:32 +00:00
}
}
export function browseResolver () {
2016-11-26 01:38:32 +00:00
return (source, { first = 25, after, type = [], status = [], ...args }, { loaders }, info) => {
const pluralName = toDashed(info.fieldName)
const singularName = toSingular(pluralName)
2016-09-01 08:39:27 +00:00
let params = {
2016-11-26 01:38:32 +00:00
...args,
type,
status,
2016-09-01 08:39:27 +00:00
limit: first,
offset: getOffsetWithDefault(after, 0)
}
params = includeSubqueries(params, info)
2016-11-26 01:38:32 +00:00
params = includeRelationships(params, info)
const formatValue = value => value.toLowerCase().replace(/ /g, '')
params.type = params.type.map(formatValue)
params.status = params.status.map(formatValue)
return loaders.browse.load([singularName, params]).then(list => {
2016-11-26 10:37:23 +00:00
// Grab the list, offet, and count from the response and use them to build
// a Relay connection object.
2016-09-01 08:39:27 +00:00
const {
[pluralName]: arraySlice,
[`${singularName}-offset`]: sliceStart,
[`${singularName}-count`]: arrayLength
} = list
const meta = { sliceStart, arrayLength }
return connectionFromArraySlice(arraySlice, { first, after }, meta)
})
2016-08-20 05:59:32 +00:00
}
}
export function searchResolver () {
2016-11-26 01:38:32 +00:00
return (source, { first = 25, after, ...args }, { loaders }, info) => {
const pluralName = toDashed(info.fieldName)
const singularName = toSingular(pluralName)
2016-09-01 08:39:27 +00:00
const { query, ...params } = args
2016-09-01 04:31:48 +00:00
params.limit = first
params.offset = getOffsetWithDefault(after, 0)
2016-11-26 01:38:32 +00:00
return loaders.search.load([singularName, query, params]).then(list => {
2016-09-01 04:31:48 +00:00
const {
[pluralName]: arraySlice,
offset: sliceStart,
count: arrayLength
} = list
const meta = { sliceStart, arrayLength }
return connectionFromArraySlice(arraySlice, { first, after }, meta)
})
2016-08-20 05:59:32 +00:00
}
}
2016-11-26 01:38:32 +00:00
export function relationshipResolver () {
2016-09-01 08:39:27 +00:00
return (source, args, context, info) => {
2016-11-26 01:38:32 +00:00
const targetType = toDashed(toSingular(info.fieldName)).replace('-', '_')
2016-11-26 10:37:23 +00:00
// There's no way to filter these at the API level, so do it here.
2016-11-26 01:38:32 +00:00
const relationships = source.filter(rel => {
if (rel['target-type'] !== targetType) {
2016-08-20 05:59:32 +00:00
return false
}
2016-11-26 01:38:32 +00:00
if (args.direction != null && rel.direction !== args.direction) {
2016-08-20 05:59:32 +00:00
return false
}
2016-11-26 01:38:32 +00:00
if (args.type != null && rel.type !== args.type) {
2016-08-20 05:59:32 +00:00
return false
}
2016-11-26 01:38:32 +00:00
if (args.typeID != null && rel['type-id'] !== args.typeID) {
2016-08-20 05:59:32 +00:00
return false
}
return true
2016-09-01 08:39:27 +00:00
})
2016-11-26 01:38:32 +00:00
return connectionFromArray(relationships, args)
2016-08-20 05:59:32 +00:00
}
}
export function linkedResolver () {
2016-09-01 08:39:27 +00:00
return (source, args, context, info) => {
2016-11-26 01:38:32 +00:00
const parentEntity = toDashed(info.parentType.name)
2016-09-01 08:39:27 +00:00
args = { ...args, [parentEntity]: source.id }
return browseResolver()(source, args, context, info)
2016-08-20 05:59:32 +00:00
}
}