Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for BigInt scalar #56

Merged
merged 22 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,17 @@ However some of the Prisma scalars do not have a natural standard representation

**Prisma Standard Scalar to GraphQL Custom Scalar Mapping**

| Prisma | GraphQL | Nexus `t` Helper | GraphQL Scalar Implementation |
iddan marked this conversation as resolved.
Show resolved Hide resolved
| ---------- | ---------- | ---------------- | ----------------------------------------------------------------- |
| `Json` | `Json` | `json` | [JsonObject](https://github.com/Urigo/graphql-scalars#jsonobject) |
| `DateTime` | `DateTime` | `dateTime` | [DateTime](https://github.com/Urigo/graphql-scalars#datetime) |
| `Bytes` | `Bytes` | `bytes` | [Bytes](https://www.graphql-scalars.dev/docs/scalars/byte/) |
| Prisma | GraphQL | Nexus `t` Helper | GraphQL Scalar Implementation | Additional Info |
| ---------- | ---------- | ---------------- | --------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ |
| `Json` | `Json` | `json` | [JsonObject](https://www.graphql-scalars.dev/docs/scalars/jsonobject) | |
| `DateTime` | `DateTime` | `dateTime` | [DateTime](https://www.graphql-scalars.dev/docs/scalars/datetime) | |
| `BigInt` | `BigInt` | `bigInt` | [BigInt](https://www.graphql-scalars.dev/docs/scalars/big-int) | [JavaScript BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) |
| `Bytes` | `Bytes` | `bytes` | [Byte](https://www.graphql-scalars.dev/docs/scalars/byte/) | [Node.js Buffer](https://nodejs.org/api/buffer.html#buffer_buffer) |

> **Note:** Not all Prisma scalar mappings are implemented yet: `BigInt`, `Decimal`, `Unsupported`
> **Note:** Not all Prisma scalar mappings are implemented yet: `Decimal`, `Unsupported`

> **Note:** `BigInt` is only supported since Node.js version 10.4.0. In order to support `BigInt` in `JSON.parse` and `JSON.stringify`, it is recommended to install this npm package together with this scalar. Otherwise, JavaScript will serialize the value as string.
jasonkuhrt marked this conversation as resolved.
Show resolved Hide resolved
> [json-bigint-patch](https://github.com/ardatan/json-bigint-patch).
jasonkuhrt marked this conversation as resolved.
Show resolved Hide resolved

While you are not required to use the implementations supplied by Nexus Prisma, you _are required to define custom scalars whose name matches the above mapping_.

Expand Down
20 changes: 12 additions & 8 deletions src/entrypoints/scalars.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { BigInt } from '../scalars/BigInt'
import { Bytes } from '../scalars/Bytes'
import { DateTime } from '../scalars/DateTime'
import { Json } from '../scalars/Json'
import { Bytes } from '../scalars/Bytes'

/**
* Predefined Nexus scalar type definitions to satisfy all custom scalars needed in GraphQL to map to the
* native scalars in Prisma. The mapping is as follows:
*
* | Prisma | GraphQL | Nexus `t` Helper | GraphQL Scalar Implementation |
* | ---------- | ---------- | ---- | ----------------------------------------------------------------- |
* | `Json` | `Json` | `json` | [JsonObject](https://github.com/Urigo/graphql-scalars#jsonobject) |
* | `DateTime` | `DateTime` | `datetime` | [DateTime](https://github.com/Urigo/graphql-scalars#datetime) |.
* | Prisma | GraphQL | Nexus `t` Helper | GraphQL Scalar Implementation | Additional Info |
* | ---------- | ---------- | ---------------- | --------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ |
* | `Json` | `Json` | `json` | [JsonObject](https://www.graphql-scalars.dev/docs/scalars/jsonobject) | |
* | `DateTime` | `DateTime` | `dateTime` | [DateTime](https://www.graphql-scalars.dev/docs/scalars/datetime) | |
* | `BigInt` | `BigInt` | `bigInt` | [BigInt](https://www.graphql-scalars.dev/docs/scalars/big-int) | [JavaScript BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) |
* | `Bytes` | `Bytes` | `bytes` | [Byte](https://www.graphql-scalars.dev/docs/scalars/byte/) | [Node.js Buffer](https://nodejs.org/api/buffer.html#buffer_buffer) |
*
* @example
*
* // Use this defualt export
* // Use this default export
*
* import { makeSchema, objectType } from 'nexus'
* import NexusPrismaScalars from 'nexus-prisma/scalars'
Expand All @@ -35,7 +38,7 @@ import { Bytes } from '../scalars/Bytes'
*
* @example
*
* // Use only select precefined custom scalars
* // Use only select predefined custom scalars
*
* import { makeSchema, objectType } from 'nexus'
* import { Json } from 'nexus-prisma/scalars'
Expand Down Expand Up @@ -70,11 +73,12 @@ import { Bytes } from '../scalars/Bytes'
* API. For convenience you can use these ones.
*/
const NexusPrismaScalars = {
BigInt,
Bytes,
DateTime,
Json,
}

export default NexusPrismaScalars

export { Bytes, DateTime, Json }
export { BigInt, Bytes, DateTime, Json }
16 changes: 8 additions & 8 deletions src/generator/models/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DMMF } from '@prisma/generator-helper'
import dedent from 'dindist'
import * as OS from 'os'
import { LiteralUnion } from 'type-fest'
import { StandardGraphQLScalarType, StandardgraphQLScalarTypes } from '../../helpers/graphql'
import { StandardGraphQLScalarType, StandardGraphQLScalarTypes } from '../../helpers/graphql'
import { PrismaScalarType } from '../../helpers/prisma'
import { allCasesHandled } from '../../helpers/utils'
import { Gentime } from '../gentime/settingsSingleton'
Expand Down Expand Up @@ -247,25 +247,25 @@ export function fieldTypeToGraphQLType(

if (field.isId) {
if (field.type === 'String' || (field.type === 'Int' && settings.projectIdIntToGraphQL === 'ID')) {
return StandardgraphQLScalarTypes.ID
return StandardGraphQLScalarTypes.ID
}
}

switch (typeName) {
case 'String': {
return StandardgraphQLScalarTypes.String
return StandardGraphQLScalarTypes.String
}
case 'Int': {
return StandardgraphQLScalarTypes.Int
return StandardGraphQLScalarTypes.Int
}
case 'Boolean': {
return StandardgraphQLScalarTypes.Boolean
return StandardGraphQLScalarTypes.Boolean
}
case 'Float': {
return StandardgraphQLScalarTypes.Float
return StandardGraphQLScalarTypes.Float
}
case 'BigInt': {
return StandardgraphQLScalarTypes.String
iddan marked this conversation as resolved.
Show resolved Hide resolved
return 'BigInt'
}
case 'DateTime': {
return 'DateTime'
Expand All @@ -277,7 +277,7 @@ export function fieldTypeToGraphQLType(
return 'Bytes'
}
case 'Decimal': {
return StandardgraphQLScalarTypes.String
return StandardGraphQLScalarTypes.String
}
default: {
return allCasesHandled(typeName)
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type StandardGraphQLScalarType = 'ID' | 'String' | 'Int' | 'Float' | 'Boolean'

export const StandardgraphQLScalarTypes = {
export const StandardGraphQLScalarTypes = {
ID: 'ID',
String: 'String',
Float: 'Float',
Expand Down
39 changes: 39 additions & 0 deletions src/scalars/BigInt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { GraphQLScalarType } from 'graphql'
import { BigIntResolver } from 'graphql-scalars'
import { asNexusMethod } from 'nexus'

/**
* A Nexus scalar type definition for the `BigInt` scalar type
iddan marked this conversation as resolved.
Show resolved Hide resolved
*
* Contributes a scalar to your GraphQL schema called `BigInt`.
*
* Contributes a `t` `[1]` helper method called `bigInt`
*
* `[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 { BigInt } from 'nexus-prisma/scalars'
*
* SomeObject = objectType({
* name: 'SomeObject',
* definition(t) {
* t.bigInt('someBigIntField')
* },
* })
*
* makeSchema({
* types: [BigInt, SomeObject],
* })
*
*/
export const BigInt = asNexusMethod(
new GraphQLScalarType({
...BigIntResolver,
description: `The \`BigInt\` scalar type represents non-fractional signed whole numeric values.
@see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt`,
}),
'bigInt'
)
22 changes: 22 additions & 0 deletions tests/e2e/__snapshots__/e2e.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ Object {
"bars": Array [
Object {
"foo": Object {
"BigIntManually": null,
"BytesManually": null,
"DateTimeManually": null,
"JsonManually": null,
"someBigIntField": "9007199254740991",
"someBytesField": Object {
"data": Array [],
"type": "Buffer",
Expand All @@ -29,6 +31,11 @@ type Bar {
foo: Foo
}

\\"\\"\\"
The \`BigInt\` scalar type represents non-fractional signed whole numeric values.
\\"\\"\\"
scalar BigInt

\\"\\"\\"The \`Byte\` scalar type represents byte value as a Buffer\\"\\"\\"
scalar Bytes

Expand All @@ -38,9 +45,11 @@ A date-time string at UTC, such as 2007-12-03T10:15:30Z, compliant with the \`da
scalar DateTime

type Foo {
BigIntManually: BigInt
BytesManually: Bytes
DateTimeManually: DateTime
JsonManually: Json
someBigIntField: BigInt!
someBytesField: Bytes!
someDateTimeField: DateTime!
someEnumA: SomeEnumA
Expand Down Expand Up @@ -74,6 +83,10 @@ import * as PrismaClient from \\".prisma/client\\"
import { core } from \\"nexus\\"
declare global {
interface NexusGenCustomInputMethods<TypeName extends string> {
/**
* The \`BigInt\` scalar type represents non-fractional signed whole numeric values.
iddan marked this conversation as resolved.
Show resolved Hide resolved
*/
bigInt<FieldName extends string>(fieldName: FieldName, opts?: core.CommonInputFieldConfig<TypeName, FieldName>): void // \\"BigInt\\";
/**
* The \`Byte\` scalar type represents byte value as a Buffer
*/
Expand All @@ -90,6 +103,10 @@ declare global {
}
declare global {
interface NexusGenCustomOutputMethods<TypeName extends string> {
/**
* The \`BigInt\` scalar type represents non-fractional signed whole numeric values.
*/
bigInt<FieldName extends string>(fieldName: FieldName, ...opts: core.ScalarOutSpread<TypeName, FieldName>): void // \\"BigInt\\";
/**
* The \`Byte\` scalar type represents byte value as a Buffer
*/
Expand Down Expand Up @@ -123,6 +140,7 @@ export interface NexusGenScalars {
Float: number
Boolean: boolean
ID: string
BigInt: any
Bytes: any
DateTime: any
Json: any
Expand All @@ -149,9 +167,11 @@ export interface NexusGenFieldTypes {
foo: NexusGenRootTypes['Foo'] | null; // Foo
}
Foo: { // field return type
BigIntManually: NexusGenScalars['BigInt'] | null; // BigInt
BytesManually: NexusGenScalars['Bytes'] | null; // Bytes
DateTimeManually: NexusGenScalars['DateTime'] | null; // DateTime
JsonManually: NexusGenScalars['Json'] | null; // Json
someBigIntField: NexusGenScalars['BigInt']; // BigInt!
someBytesField: NexusGenScalars['Bytes']; // Bytes!
someDateTimeField: NexusGenScalars['DateTime']; // DateTime!
someEnumA: NexusGenEnums['SomeEnumA'] | null; // SomeEnumA
Expand All @@ -167,9 +187,11 @@ export interface NexusGenFieldTypeNames {
foo: 'Foo'
}
Foo: { // field return type name
BigIntManually: 'BigInt'
BytesManually: 'Bytes'
DateTimeManually: 'DateTime'
JsonManually: 'Json'
someBigIntField: 'BigInt'
someBytesField: 'Bytes'
someDateTimeField: 'DateTime'
someEnumA: 'SomeEnumA'
Expand Down
9 changes: 9 additions & 0 deletions tests/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ it('When bundled custom scalars are used the project type checks and generates e
someJsonField Json
someDateTimeField DateTime
someBytesField Bytes
someBigIntField BigInt
someEnumA SomeEnumA
bar Bar?
}
Expand Down Expand Up @@ -155,6 +156,7 @@ it('When bundled custom scalars are used the project type checks and generates e
someDateTimeField: new Date("2021-05-10T20:42:46.609Z"),
someBytesField: Buffer.from([]),
someJsonField: JSON.stringify({}),
someBigIntField: BigInt(9007199254740991),
someEnumA: 'alpha',
bar: {
create: {
Expand Down Expand Up @@ -209,9 +211,11 @@ it('When bundled custom scalars are used the project type checks and generates e
t.json('JsonManually')
t.dateTime('DateTimeManually')
t.bytes('BytesManually')
t.bigInt('BigIntManually')
t.field(Foo.someJsonField.name, Foo.someJsonField)
t.field(Foo.someDateTimeField.name, Foo.someDateTimeField)
t.field(Foo.someBytesField.name, Foo.someBytesField)
t.field(Foo.someBigIntField.name, Foo.someBigIntField)
},
}),
]
Expand Down Expand Up @@ -319,6 +323,9 @@ it('When bundled custom scalars are used the project type checks and generates e
expect(stripAnsi(results.runFirstBuild.stdout)).toMatch(
/.*error TS2339: Property 'dateTime' does not exist on type 'ObjectDefinitionBlock<any>'.*/
)
expect(stripAnsi(results.runFirstBuild.stdout)).toMatch(
/.*error TS2339: Property 'bigInt' does not exist on type 'ObjectDefinitionBlock<any>'.*/
)

expect(results.runReflectPrisma.exitCode).toBe(0)

Expand Down Expand Up @@ -362,9 +369,11 @@ it('When bundled custom scalars are used the project type checks and generates e
JsonManually
DateTimeManually
BytesManually
BigIntManually
someEnumA
someDateTimeField
someBytesField
someBigIntField
iddan marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/integration/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ testIntegration({
apiSchema({ Foo }) {
return [
NexusPrismaScalars.Bytes,
NexusPrismaScalars.BigInt,
NexusPrismaScalars.DateTime,
NexusPrismaScalars.Json,
objectType({
Expand Down
16 changes: 9 additions & 7 deletions tests/unit/customScalarsModule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ assertBuildPresent()

it('scalars can be accessed via namespace import', () => {
expect(Object.keys(NexusPrismaScalarsNS)).toMatchInlineSnapshot(`
Array [
"Bytes",
"DateTime",
"Json",
"default",
]
`)
Array [
"BigInt",
"Bytes",
"DateTime",
"Json",
"default",
]
`)
})

it('scalars can be accessed via a default import', () => {
expect(Object.keys(NexusPrismaScalars)).toMatchInlineSnapshot(`
Array [
"BigInt",
"Bytes",
"DateTime",
"Json",
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/graphqlSchema/__snapshots__/json.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

exports[`When a JSON field is defined in the Prisma schema it can be projected into the GraphQL API: graphqlSchema 1`] = `
"
\\"\\"\\"
The \`BigInt\` scalar type represents non-fractional signed whole numeric values.
\\"\\"\\"
scalar BigInt

\\"\\"\\"The \`Byte\` scalar type represents byte value as a Buffer\\"\\"\\"
scalar Bytes

Expand Down
1 change: 1 addition & 0 deletions tests/unit/graphqlSchema/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ testGraphqlSchema({
`,
apiSchema({ Foo }) {
return [
NexusPrismaScalars.BigInt,
NexusPrismaScalars.Bytes,
NexusPrismaScalars.DateTime,
NexusPrismaScalars.Json,
Expand Down
Loading