@neo4j/graphql
Version:
A GraphQL to Cypher query execution layer for Neo4j and JavaScript GraphQL implementations
62 lines • 2.61 kB
JavaScript
;
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphQLTime = exports.validateTime = exports.TIME_REGEX = void 0;
const graphql_1 = require("graphql");
const neo4j_driver_1 = __importDefault(require("neo4j-driver"));
exports.TIME_REGEX = /^(?<hour>[01]\d|2[0-3]):(?<minute>[0-5]\d)(:(?<second>[0-5]\d)(\.(?<fraction>\d{1}(?:\d{0,8})))?((?:[Zz])|((?<offsetDirection>[-|+])(?<offsetHour>[01]\d|2[0-3]):(?<offsetMinute>[0-5]\d)))?)?$/;
const validateTime = (value) => {
if (typeof value !== "string") {
throw new TypeError(`Value must be of type string: ${value}`);
}
const match = exports.TIME_REGEX.exec(value);
if (!match) {
throw new TypeError(`Value must be formatted as Time: ${value}`);
}
return value;
};
exports.validateTime = validateTime;
exports.GraphQLTime = new graphql_1.GraphQLScalarType({
name: "Time",
description: "A time, represented as an RFC3339 time string",
serialize: (value) => {
if (typeof value !== "string" && !(value instanceof neo4j_driver_1.default.types.Time)) {
throw new TypeError(`Value must be of type string: ${value}`);
}
const stringifiedValue = value.toString();
if (!exports.TIME_REGEX.test(stringifiedValue)) {
throw new TypeError(`Value must be formatted as Time: ${stringifiedValue}`);
}
return stringifiedValue;
},
parseValue: (value) => {
return (0, exports.validateTime)(value);
},
parseLiteral: (ast) => {
if (ast.kind !== graphql_1.Kind.STRING) {
throw new graphql_1.GraphQLError(`Only strings can be validated as Time, but received: ${ast.kind}`);
}
return (0, exports.validateTime)(ast.value);
},
});
//# sourceMappingURL=Time.js.map