Created
March 5, 2020 15:06
-
-
Save bogoslavskiy/7c62320bb42599b536d165ced8e23fe1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { GraphQLScalarType } from 'graphql'; | |
import { Kind, ValueNode } from 'graphql/language'; | |
import { gql } from 'apollo-server'; | |
const parse = (v: string) => { | |
if (v === undefined || v === null) { | |
throw new Error('field should be String'); | |
} | |
const str = String(v); | |
if (str.length) { | |
return str.trim(); | |
} | |
throw new Error("field can't be empty"); | |
}; | |
const literal = (ast: ValueNode) => { | |
if (ast.kind === Kind.STRING) { | |
return parse(ast.value); | |
} | |
throw new Error('field should be String'); | |
}; | |
export default { | |
declaration: gql` | |
scalar NEString | |
`, | |
type: { | |
NEString: new GraphQLScalarType({ | |
name: 'NEString', | |
description: 'Alias `Non Empty String`', | |
serialize: parse, | |
parseValue: parse, | |
parseLiteral: literal | |
}) | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment