simple-graphql
Version:
The simple way to generates GraphQL schemas and Sequelize models from your models definition.
66 lines • 2.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const graphql_1 = require("graphql");
const graphql_relay_1 = require("graphql-relay");
function defGlobalIdInputType(typeName) {
return new graphql_1.GraphQLScalarType({
name: typeName + 'Id',
description: 'Global id of ' + typeName,
serialize(value) {
if (typeof value === 'string') {
return (0, graphql_relay_1.toGlobalId)(typeName, value);
}
else {
return value;
}
},
parseValue(value) {
if (typeof value === 'string') {
const { type, id } = (0, graphql_relay_1.fromGlobalId)(value);
if (type === typeName) {
if (Number.parseInt(id).toString() === id) {
return Number.parseInt(id);
}
else {
return id;
}
}
throw new Error('Incorrect globalId type: ' + type);
}
else {
throw new Error('Incorrect globalId format: ');
}
},
parseLiteral(ast) {
if (ast.kind !== graphql_1.Kind.STRING) {
throw new graphql_1.GraphQLError('Query error: Can only parse string to GrobalId but got a: ' +
ast.kind, [ast]);
}
const value = ast.value;
if (typeof value === 'string') {
const { type, id } = (0, graphql_relay_1.fromGlobalId)(value);
if (type === typeName) {
if (Number.parseInt(id).toString() === id) {
return Number.parseInt(id);
}
else {
return id;
}
}
throw new Error('Incorrect globalId type: ' + type);
}
else {
throw new Error('Incorrect globalId format: ' + value);
}
}
});
}
const types = {};
function globalIdType(typeName) {
if (!types[typeName]) {
types[typeName] = defGlobalIdInputType(typeName);
}
return types[typeName];
}
exports.default = globalIdType;
//# sourceMappingURL=globalIdType.js.map