2016-09-01 04:31:48 +00:00
|
|
|
import { toEntityType } 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'
|
|
|
|
|
|
|
|
|
|
export function includeRelations (params, info) {
|
|
|
|
|
let fields = getFields(info)
|
|
|
|
|
if (info.fieldName !== 'relations') {
|
|
|
|
|
if (fields.relations) {
|
|
|
|
|
fields = getFields(fields.relations)
|
|
|
|
|
} else {
|
|
|
|
|
return params
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (fields) {
|
|
|
|
|
const relations = Object.keys(fields)
|
2016-09-01 04:31:48 +00:00
|
|
|
const includeRels = relations.map(key => `${toEntityType(key)}-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 () {
|
|
|
|
|
return (root, { mbid }, { lookupLoader }, info) => {
|
|
|
|
|
const entityType = toEntityType(info.fieldName)
|
|
|
|
|
const params = includeRelations({}, info)
|
|
|
|
|
return lookupLoader.load([entityType, mbid, params])
|
2016-08-20 05:59:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function browseResolver () {
|
2016-09-01 08:39:27 +00:00
|
|
|
return (source, { first = 25, after, ...args }, { browseLoader }, info) => {
|
2016-09-01 04:31:48 +00:00
|
|
|
const pluralName = toEntityType(info.fieldName)
|
2016-08-20 05:59:32 +00:00
|
|
|
let singularName = pluralName
|
|
|
|
|
if (pluralName.endsWith('s')) {
|
|
|
|
|
singularName = pluralName.slice(0, -1)
|
|
|
|
|
}
|
2016-09-01 08:39:27 +00:00
|
|
|
const { type, types, status, statuses, ...moreParams } = args
|
|
|
|
|
let params = {
|
|
|
|
|
...moreParams,
|
|
|
|
|
type: [],
|
|
|
|
|
status: [],
|
|
|
|
|
limit: first,
|
|
|
|
|
offset: getOffsetWithDefault(after, 0)
|
|
|
|
|
}
|
|
|
|
|
params = includeSubqueries(params, info)
|
|
|
|
|
params = includeRelations(params, info)
|
|
|
|
|
if (type) {
|
|
|
|
|
params.type.push(type)
|
|
|
|
|
}
|
|
|
|
|
if (types) {
|
|
|
|
|
params.type.push(...types)
|
|
|
|
|
}
|
|
|
|
|
if (status) {
|
|
|
|
|
params.status.push(status)
|
|
|
|
|
}
|
|
|
|
|
if (statuses) {
|
|
|
|
|
params.status.push(...statuses)
|
|
|
|
|
}
|
|
|
|
|
return browseLoader.load([singularName, params]).then(list => {
|
|
|
|
|
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-09-01 08:39:27 +00:00
|
|
|
return (source, { first = 25, after, ...args }, { searchLoader }, info) => {
|
2016-09-01 04:31:48 +00:00
|
|
|
const pluralName = toEntityType(info.fieldName)
|
2016-08-20 05:59:32 +00:00
|
|
|
let singularName = pluralName
|
|
|
|
|
if (pluralName.endsWith('s')) {
|
|
|
|
|
singularName = pluralName.slice(0, -1)
|
|
|
|
|
}
|
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)
|
|
|
|
|
return searchLoader.load([singularName, query, params]).then(list => {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function relationResolver () {
|
2016-09-01 08:39:27 +00:00
|
|
|
return (source, args, context, info) => {
|
2016-09-01 04:31:48 +00:00
|
|
|
const targetType = toEntityType(info.fieldName).replace('-', '_')
|
2016-09-01 08:39:27 +00:00
|
|
|
const relations = source.filter(relation => {
|
2016-08-20 05:59:32 +00:00
|
|
|
if (relation['target-type'] !== targetType) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2016-09-01 08:39:27 +00:00
|
|
|
if (args.direction != null && relation.direction !== args.direction) {
|
2016-08-20 05:59:32 +00:00
|
|
|
return false
|
|
|
|
|
}
|
2016-09-01 08:39:27 +00:00
|
|
|
if (args.type != null && relation.type !== args.type) {
|
2016-08-20 05:59:32 +00:00
|
|
|
return false
|
|
|
|
|
}
|
2016-09-01 08:39:27 +00:00
|
|
|
if (args.typeID != null && relation['type-id'] !== args.typeID) {
|
2016-08-20 05:59:32 +00:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
2016-09-01 08:39:27 +00:00
|
|
|
})
|
|
|
|
|
return connectionFromArray(relations, 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-09-01 04:31:48 +00:00
|
|
|
const parentEntity = toEntityType(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
|
|
|
}
|
|
|
|
|
}
|