graphql recognizes decimal type now and successfully starts

This commit is contained in:
Ulf Gebhardt 2022-02-27 02:07:33 +01:00
parent 85b9fb9b86
commit 67cddadd1e
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9
2 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1,26 @@
import { GraphQLScalarType, Kind } from 'graphql'
import Decimal from 'decimal.js-light'
export default new GraphQLScalarType({
name: 'Decimal',
description: 'The `Decimal` scalar type to represent currency values',
serialize(value: unknown) {
if (!(value instanceof Decimal)) {
throw new Error('DecimalScalar can only serialize Decimal values')
}
return value.toString()
},
parseValue(value) {
return new Decimal(value)
},
parseLiteral(ast) {
if (ast.kind !== Kind.STRING) {
throw new TypeError(`${String(ast)} is not a valid decimal value.`)
}
return new Decimal(ast.value)
},
})

View File

@ -1,13 +1,16 @@
import { GraphQLSchema } from 'graphql'
import { buildSchema } from 'type-graphql'
import Decimal from 'decimal.js-light'
import path from 'path'
import isAuthorized from './directive/isAuthorized'
import DecimalScalar from './scalar/Decimal'
const schema = async (): Promise<GraphQLSchema> => {
return buildSchema({
resolvers: [path.join(__dirname, 'resolver', `!(*.test).{js,ts}`)],
authChecker: isAuthorized,
scalarsMap: [{ type: Decimal, scalar: DecimalScalar }],
})
}