graphql-scalars
Version:
A collection of scalar types not included in base GraphQL.
25 lines • 1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const definition_1 = require("graphql/type/definition");
const error_1 = require("graphql/error");
const language_1 = require("graphql/language");
const coerceURL = (value) => {
// tslint:disable-next-line: no-eval
let URLCtor = typeof URL === 'undefined' ? eval(`require('url')`).URL : URL;
return new URLCtor(value.toString());
};
exports.default = new definition_1.GraphQLScalarType({
name: 'URL',
description: 'A field whose value conforms to the standard URL format as specified in RFC3986: https://www.ietf.org/rfc/rfc3986.txt.',
serialize(value) {
return coerceURL(value).toString();
},
parseValue: coerceURL,
parseLiteral(ast) {
if (ast.kind !== language_1.Kind.STRING) {
throw new error_1.GraphQLError(`Can only validate strings as URLs but got a: ${ast.kind}`);
}
return coerceURL(ast.value);
},
});
//# sourceMappingURL=URL.js.map