@jokio/graphql
Version:
High level, pre-configured, GraphQL Server
42 lines (36 loc) • 898 B
text/typescript
import { GraphQLScalarType } from 'graphql';
import { Kind } from 'graphql/language';
function identity(value: any) {
return value;
}
function parseLiteral(ast: any) {
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: {
const value = Object.create(null);
ast.fields.forEach((field: any) => {
value[field.name.value] = parseLiteral(field.value);
});
return value;
}
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,
});