uzen
Version:
General-purpose GraphQL subscription server library
41 lines (40 loc) • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JSONString = void 0;
const graphql_1 = require("graphql");
exports.JSONString = new graphql_1.GraphQLScalarType({
name: "JSONString",
description: "Custom scalar for handling JSON objects stored as strings in PostgreSQL",
serialize(value) {
if (typeof value === "string") {
try {
return JSON.parse(value);
}
catch (error) {
return value;
}
}
return value;
},
parseValue(value) {
if (typeof value === "object" && value !== null) {
return JSON.stringify(value);
}
return value;
},
parseLiteral(ast) {
switch (ast.kind) {
case graphql_1.Kind.STRING:
try {
return JSON.parse(ast.value);
}
catch (error) {
return ast.value;
}
case graphql_1.Kind.OBJECT:
return ast;
default:
return null;
}
},
});