graphql-compose-mongoose
Version:
Plugin for `graphql-compose` which derive a graphql types from a mongoose model.
35 lines • 1.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const graphql_1 = require("graphql-compose/lib/graphql");
function parseStringWithRegExp(str) {
if (str.startsWith('/')) {
const m = str.match(/^\/(.+)\/([gimsuy]*)$/);
if (m) {
return new RegExp(m[1], m[2]);
}
throw new TypeError('Field error: cannot parse provided string as RegExp object');
}
else {
return new RegExp(str);
}
}
const GraphQLRegExpAsString = new graphql_1.GraphQLScalarType({
name: 'RegExpAsString',
specifiedByURL: 'http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf',
description: 'The string representation of JavaScript regexp. You may provide it with flags "/^abc.*/i" or without flags like "^abc.*". More info about RegExp characters and flags: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions',
serialize: String,
parseValue(value) {
if (typeof value !== 'string') {
throw new TypeError('Field error: GraphQL RegExpAsString value should be provided as a string');
}
return parseStringWithRegExp(value);
},
parseLiteral(ast) {
if (ast.kind !== graphql_1.Kind.STRING) {
throw new TypeError('Field error: GraphQL RegExpAsString value should be provided as a string');
}
return parseStringWithRegExp(ast.value);
},
});
exports.default = GraphQLRegExpAsString;
//# sourceMappingURL=RegExpAsString.js.map