graphql-compose
Version:
GraphQL schema builder from different data sources with middleware extensions.
48 lines (41 loc) • 1.48 kB
JavaScript
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
// copied from https://github.com/taion/graphql-type-json
import { GraphQLScalarType } from 'graphql';
import { Kind } from 'graphql/language';
function identity(value) {
return value;
}
function parseLiteral(ast) {
switch (ast.kind) {
case Kind.STRING:
case Kind.BOOLEAN:
return ast.value;
case Kind.INT:
case Kind.FLOAT:
return parseFloat(ast.value);
case Kind.OBJECT:
{
var _ret = function () {
var value = Object.create(null);
ast.fields.forEach(function (field) {
value[field.name.value] = parseLiteral(field.value);
});
return {
v: value
};
}();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
case Kind.LIST:
return ast.values.map(parseLiteral);
default:
return null;
}
}
export default new GraphQLScalarType({
name: 'JSON',
description: 'The `JSON` scalar type represents JSON values as specified by ' + '[ECMA-404](http://www.ecma-international.org/' + 'publications/files/ECMA-ST/ECMA-404.pdf).',
serialize: identity,
parseValue: identity,
parseLiteral: parseLiteral
});