-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for Decimal scalar (#96)
Co-authored-by: Jason Kuhrt <jasonkuhrt@me.com>
- Loading branch information
1 parent
915d38e
commit 74da7c2
Showing
10 changed files
with
107 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { asNexusMethod } from 'nexus' | ||
import * as DecimalJs from 'decimal.js' | ||
|
||
import { GraphQLScalarType, Kind } from 'graphql' | ||
|
||
/** | ||
* A Nexus scalar type definition for arbitrary-precision Decimal type. | ||
* | ||
* Contributes a scalar to your GraphQL schema called `Decimal`. | ||
* | ||
* Contributes a `t` `[1]` helper method called `decimal` | ||
* | ||
* `[1]` A `t` helper method refers to a method on the argument given to a `definition` method. Helper methods | ||
* here typically help you quickly create new fields. | ||
* | ||
* @example | ||
* | ||
* import { makeSchema, objectType } from 'nexus' | ||
* import { Decimal } from 'nexus-prisma/scalars' | ||
* | ||
* SomeObject = objectType({ | ||
* name: 'SomeObject', | ||
* definition(t) { | ||
* t.decimal('someDecimalField') | ||
* }, | ||
* }) | ||
* | ||
* makeSchema({ | ||
* types: [Decimal, SomeObject], | ||
* }) | ||
* | ||
*/ | ||
export const Decimal = asNexusMethod( | ||
/** | ||
* Copied from prisma-graphql-type-decimal. | ||
* | ||
* @see https://github.com/unlight/prisma-graphql-type-decimal | ||
*/ | ||
new GraphQLScalarType({ | ||
name: 'Decimal', | ||
description: 'An arbitrary-precision Decimal type', | ||
/** | ||
* Value sent to the client | ||
*/ | ||
serialize(value: DecimalJs.Decimal) { | ||
return value.toString() | ||
}, | ||
/** | ||
* Value from the client | ||
*/ | ||
parseValue(value: DecimalJs.Decimal.Value) { | ||
return new DecimalJs.Decimal(value) | ||
}, | ||
parseLiteral(ast) { | ||
if (ast.kind === Kind.INT || ast.kind === Kind.FLOAT || ast.kind === Kind.STRING) { | ||
return new DecimalJs.Decimal(ast.value) | ||
} | ||
return null | ||
}, | ||
}), | ||
'decimal' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters