mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
982 B
37 lines
982 B
2 years ago
|
import { Kind, GraphQLScalarType } from 'graphql'
|
||
3 years ago
|
|
||
|
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
||
|
const nilUUID = '00000000-0000-0000-0000-000000000000'
|
||
|
|
||
|
function isUUID (value) {
|
||
|
return uuidRegex.test(value) || nilUUID === value
|
||
|
}
|
||
|
|
||
2 years ago
|
export default new GraphQLScalarType({
|
||
3 years ago
|
name: 'UUID',
|
||
|
description: 'The `UUID` scalar type represents UUID values as specified by [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122).',
|
||
|
serialize: (value) => {
|
||
|
if (!isUUID(value)) {
|
||
|
throw new TypeError(`UUID cannot represent non-UUID value: ${value}`)
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
return value.toLowerCase()
|
||
|
},
|
||
|
parseValue: (value) => {
|
||
|
if (!isUUID(value)) {
|
||
|
throw new TypeError(`UUID cannot represent non-UUID value: ${value}`)
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
return value.toLowerCase()
|
||
|
},
|
||
|
parseLiteral: (ast) => {
|
||
|
if (ast.kind === Kind.STRING) {
|
||
|
if (isUUID(ast.value)) {
|
||
|
return ast.value
|
||
3 years ago
|
}
|
||
|
}
|
||
3 years ago
|
|
||
|
return undefined
|
||
|
}
|
||
|
})
|