UNPKG

graphql-tools-type-uuid

Version:
43 lines (35 loc) 1.15 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _graphql = require('graphql'); var _language = require('graphql/language'); var _error = require('graphql/error'); var _isUuid = require('is-uuid'); var _isUuid2 = _interopRequireDefault(_isUuid); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } exports.default = new _graphql.GraphQLScalarType({ name: 'UUID', description: `UUID scalar type for [graphql-tools](https://github.com/apollographql/graphql-tools)`, serialize(value) { if (_isUuid2.default.anyNonNil(value)) { return value; } return null; }, parseValue(value) { if (_isUuid2.default.anyNonNil(value)) { return value; } throw new _error.GraphQLError('', []); }, parseLiteral(ast) { if (ast.kind !== _language.Kind.STRING) { throw new _error.GraphQLError(`Type should be "String", found ${ast.kind}.`, [ast]); } if (_isUuid2.default.anyNonNil(ast.value)) { return ast.value; } throw new _error.GraphQLError(`Invalid UUID literal.\n${ast.value} is not UUID.`, [ast]); } });