2016-08-20 05:59:32 +00:00
|
|
|
import { Kind } from 'graphql/language'
|
|
|
|
|
import { GraphQLScalarType } from 'graphql/type'
|
|
|
|
|
|
2016-12-10 02:55:41 +00:00
|
|
|
function createScalar (config) {
|
|
|
|
|
return new GraphQLScalarType({
|
|
|
|
|
serialize: value => value,
|
|
|
|
|
parseValue: value => value,
|
|
|
|
|
parseLiteral (ast) {
|
|
|
|
|
if (ast.kind === Kind.STRING) {
|
|
|
|
|
return ast.value
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
},
|
|
|
|
|
...config
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-20 05:59:32 +00:00
|
|
|
const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
|
2016-12-10 02:55:41 +00:00
|
|
|
const locale = /^([a-z]{2})(_[A-Z]{2})?(\.[A-Z0-9-]+)?$/
|
|
|
|
|
// Be extremely lenient; just prevent major input errors.
|
|
|
|
|
const url = /^\w+:\/\/[\w-]+\.\w+/
|
2016-08-20 05:59:32 +00:00
|
|
|
|
|
|
|
|
function validateMBID (value) {
|
|
|
|
|
if (typeof value === 'string' && uuid.test(value)) {
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
throw new TypeError(`Malformed MBID: ${value}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function validatePositive (value) {
|
|
|
|
|
if (value >= 0) {
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
throw new TypeError(`Expected positive value: ${value}`)
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-10 02:55:41 +00:00
|
|
|
function validateLocale (value) {
|
|
|
|
|
if (typeof value === 'string' && locale.test(value)) {
|
|
|
|
|
return value
|
2016-08-20 05:59:32 +00:00
|
|
|
}
|
2016-12-10 02:55:41 +00:00
|
|
|
throw new TypeError(`Malformed locale: ${value}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function validateURL (value) {
|
|
|
|
|
if (typeof value === 'string' && url.test(value)) {
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
throw new TypeError(`Malformed URL: ${value}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const ASIN = createScalar({
|
|
|
|
|
name: 'ASIN',
|
|
|
|
|
description: `An [Amazon Standard Identification Number](https://musicbrainz.org/doc/ASIN)
|
|
|
|
|
(ASIN) is a 10-character alphanumeric unique identifier assigned by Amazon.com
|
|
|
|
|
and its partners for product identification within the Amazon organization.`
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export const DateType = createScalar({
|
|
|
|
|
name: 'Date',
|
|
|
|
|
description: 'Year, month (optional), and day (optional) in YYYY-MM-DD format.'
|
2016-08-20 05:59:32 +00:00
|
|
|
})
|
|
|
|
|
|
2016-12-10 02:55:41 +00:00
|
|
|
export const Degrees = createScalar({
|
2016-08-20 05:59:32 +00:00
|
|
|
name: 'Degrees',
|
2016-12-10 02:55:41 +00:00
|
|
|
description: 'Decimal degrees, used for latitude and longitude.'
|
2016-08-20 05:59:32 +00:00
|
|
|
})
|
|
|
|
|
|
2016-12-10 02:55:41 +00:00
|
|
|
export const Duration = createScalar({
|
2016-08-20 05:59:32 +00:00
|
|
|
name: 'Duration',
|
|
|
|
|
description: 'A length of time, in milliseconds.',
|
|
|
|
|
serialize: validatePositive,
|
|
|
|
|
parseValue: validatePositive,
|
|
|
|
|
parseLiteral (ast) {
|
|
|
|
|
if (ast.kind === Kind.INT) {
|
|
|
|
|
return validatePositive(parseInt(ast.value, 10))
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2016-12-10 02:55:41 +00:00
|
|
|
export const IPI = createScalar({
|
2016-08-20 05:59:32 +00:00
|
|
|
name: 'IPI',
|
2016-11-26 01:38:32 +00:00
|
|
|
description: `An [IPI](https://musicbrainz.org/doc/IPI) (interested party
|
|
|
|
|
information) code is an identifying number assigned by the CISAC database for
|
2016-12-10 02:55:41 +00:00
|
|
|
musical rights management.`
|
2016-08-20 05:59:32 +00:00
|
|
|
})
|
|
|
|
|
|
2016-12-10 02:55:41 +00:00
|
|
|
export const ISNI = createScalar({
|
2016-08-20 05:59:32 +00:00
|
|
|
name: 'ISNI',
|
2016-11-26 01:38:32 +00:00
|
|
|
description: `The [International Standard Name Identifier](https://musicbrainz.org/doc/ISNI)
|
|
|
|
|
(ISNI) is an ISO standard for uniquely identifying the public identities of
|
2016-12-10 02:55:41 +00:00
|
|
|
contributors to media content.`
|
2016-08-20 05:59:32 +00:00
|
|
|
})
|
|
|
|
|
|
2016-12-10 02:55:41 +00:00
|
|
|
export const ISWC = createScalar({
|
2016-08-20 05:59:32 +00:00
|
|
|
name: 'ISWC',
|
2016-11-26 01:38:32 +00:00
|
|
|
description: `The [International Standard Musical Work Code](https://musicbrainz.org/doc/ISWC)
|
|
|
|
|
(ISWC) is an ISO standard similar to ISBNs for identifying musical works /
|
2016-12-10 02:55:41 +00:00
|
|
|
compositions.`
|
2016-08-20 05:59:32 +00:00
|
|
|
})
|
|
|
|
|
|
2016-12-10 02:55:41 +00:00
|
|
|
export const Locale = createScalar({
|
2016-08-20 05:59:32 +00:00
|
|
|
name: 'Locale',
|
|
|
|
|
description: 'Language code, optionally with country and encoding.',
|
2016-12-10 02:55:41 +00:00
|
|
|
serialize: validateLocale,
|
|
|
|
|
parseValue: validateLocale,
|
2016-08-20 05:59:32 +00:00
|
|
|
parseLiteral (ast) {
|
|
|
|
|
if (ast.kind === Kind.STRING) {
|
2016-12-10 02:55:41 +00:00
|
|
|
return validateLocale(ast.value)
|
2016-08-20 05:59:32 +00:00
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2016-12-10 02:55:41 +00:00
|
|
|
export const MBID = createScalar({
|
|
|
|
|
name: 'MBID',
|
|
|
|
|
description: `The MBID scalar represents MusicBrainz identifiers, which are
|
|
|
|
|
36-character UUIDs.`,
|
|
|
|
|
serialize: validateMBID,
|
|
|
|
|
parseValue: validateMBID,
|
2016-08-20 05:59:32 +00:00
|
|
|
parseLiteral (ast) {
|
|
|
|
|
if (ast.kind === Kind.STRING) {
|
2016-12-10 02:55:41 +00:00
|
|
|
return validateMBID(ast.value)
|
2016-08-20 05:59:32 +00:00
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2016-12-10 02:55:41 +00:00
|
|
|
export const Time = createScalar({
|
|
|
|
|
name: 'Time',
|
|
|
|
|
description: 'A time of day, in 24-hour hh:mm notation.'
|
2016-08-20 05:59:32 +00:00
|
|
|
})
|
|
|
|
|
|
2016-12-10 02:55:41 +00:00
|
|
|
export const URLString = createScalar({
|
|
|
|
|
name: 'URLString',
|
|
|
|
|
description: 'A web address.',
|
|
|
|
|
serialize: validateURL,
|
|
|
|
|
parseValue: validateURL,
|
2016-08-20 05:59:32 +00:00
|
|
|
parseLiteral (ast) {
|
|
|
|
|
if (ast.kind === Kind.STRING) {
|
2016-12-10 02:55:41 +00:00
|
|
|
return validateURL(ast.value)
|
2016-08-20 05:59:32 +00:00
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
})
|