Skip to content

Instantly share code, notes, and snippets.

@bogoslavskiy
Created March 5, 2020 15:06
Show Gist options
  • Save bogoslavskiy/7c62320bb42599b536d165ced8e23fe1 to your computer and use it in GitHub Desktop.
Save bogoslavskiy/7c62320bb42599b536d165ced8e23fe1 to your computer and use it in GitHub Desktop.
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