graphql-types-non-empty-string
Version:
GraphQL Non-empty String
18 lines (15 loc) • 448 B
JavaScript
const { GraphQLScalarType, Kind } = require('graphql')
const parse = v => {
if (v === undefined || v === null) return null
const string = String(v)
return string.length ? string : null
}
module.exports = new GraphQLScalarType({
name: 'NonEmptyString',
description: "String where '' is treated as null",
serialize: parse,
parseValue: parse,
parseLiteral(ast) {
return ast.kind === Kind.STRING ? parse(ast.value) : null;
}
})