UNPKG

@nestjs/graphql

Version:

Nest - modern, fast, powerful node.js web framework (@graphql)

26 lines (25 loc) • 1.1 kB
import { GraphQLScalarType } from 'graphql'; function bindInstanceContext(instance, funcKey) { return instance[funcKey] ? instance[funcKey].bind(instance) : undefined; } function bindInputParser(instance, funcKey) { const parse = bindInstanceContext(instance, funcKey); // "CustomScalar" parsers may return "null" for input they cannot parse, but // graphql-js only treats "undefined" (or a thrown error) as invalid and never // passes "null" to a parser, so a "null" result would slip through non-null // positions. Report it to graphql-js as "undefined" instead. return parse && ((...args) => parse(...args) ?? undefined); } export function createScalarType(name, instance) { return new GraphQLScalarType({ name, description: instance.description, specifiedByURL: instance.specifiedByURL, extensions: instance.extensions, parseValue: bindInputParser(instance, 'parseValue'), serialize: bindInstanceContext(instance, 'serialize'), parseLiteral: bindInputParser(instance, 'parseLiteral'), }); }