Skip to content

Commit

Permalink
feat: add bytes scalar support (graphql-nexus#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
iddan authored Jun 22, 2021
1 parent b7c63c7 commit 88fd092
Show file tree
Hide file tree
Showing 12 changed files with 330 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const schema = makeSchema({

##### Midterm

- [ ] Support for Prisma Model field types of remaining scalars (`Bytes`, etc.)
- [ ] Support for Prisma Model field types of remaining scalars (`Decimal`, etc.)
- [ ] Support for Prisma Model field types relating to other Models n:n
- [ ] Support for relation field ordering parameters

Expand Down Expand Up @@ -215,8 +215,9 @@ However some of the Prisma scalars do not have a natural standard representation
| ---------- | ---------- | ---------------- | ----------------------------------------------------------------- |
| `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/) |

> **Note:** Not all Prisma scalar mappings are implemented yet: `Bytes`, `BigInt`, `Decimal`, `Unsupported`
> **Note:** Not all Prisma scalar mappings are implemented yet: `BigInt`, `Decimal`, `Unsupported`
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
4 changes: 3 additions & 1 deletion src/entrypoints/scalars.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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
Expand Down Expand Up @@ -69,10 +70,11 @@ import { Json } from '../scalars/Json'
* API. For convenience you can use these ones.
*/
const NexusPrismaScalars = {
Bytes,
DateTime,
Json,
}

export default NexusPrismaScalars

export { DateTime, Json }
export { Bytes, DateTime, Json }
2 changes: 1 addition & 1 deletion src/generator/models/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export function fieldTypeToGraphQLType(
return 'Json'
}
case 'Bytes': {
return StandardgraphQLScalarTypes.String
return 'Bytes'
}
case 'Decimal': {
return StandardgraphQLScalarTypes.String
Expand Down
40 changes: 40 additions & 0 deletions src/scalars/Bytes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { GraphQLScalarType } from 'graphql'
import { ByteResolver } from 'graphql-scalars'
import { asNexusMethod } from 'nexus'

/**
* A Nexus scalar type definition for the `Bytes` scalar type represents byte value as specified by [NodeJS
* Buffer type](https://nodejs.org/api/buffer.html)
*
* Contributes a scalar to your GraphQL schema called `Bytes`.
*
* Contributes a `t` `[1]` helper method called `bytes`
*
* `[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 { Bytes } from 'nexus-prisma/scalars'
*
* SomeObject = objectType({
* name: 'SomeObject',
* definition(t) {
* t.bytes('someBytesField')
* },
* })
*
* makeSchema({
* types: [Bytes, SomeObject],
* })
*
*/
export const Bytes = asNexusMethod(
new GraphQLScalarType({
...ByteResolver,
// Override the default 'Byte' name with one that matches what Nexus Prisma expects.
name: 'Bytes',
}),
'bytes'
)
23 changes: 23 additions & 0 deletions tests/e2e/__snapshots__/e2e.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ Object {
"bars": Array [
Object {
"foo": Object {
"BytesManually": null,
"DateTimeManually": null,
"JsonManually": null,
"someBytesField": Object {
"data": Array [],
"type": "Buffer",
},
"someDateTimeField": "2021-05-10T20:42:46.609Z",
"someEnumA": "alpha",
},
Expand All @@ -24,14 +29,19 @@ type Bar {
foo: Foo
}
\\"\\"\\"The \`Byte\` scalar type represents byte value as a Buffer\\"\\"\\"
scalar Bytes
\\"\\"\\"
A date-time string at UTC, such as 2007-12-03T10:15:30Z, compliant with the \`date-time\` format outlined in section 5.6 of the RFC 3339 profile of the ISO 8601 standard for representation of dates and times using the Gregorian calendar.
\\"\\"\\"
scalar DateTime
type Foo {
BytesManually: Bytes
DateTimeManually: DateTime
JsonManually: Json
someBytesField: Bytes!
someDateTimeField: DateTime!
someEnumA: SomeEnumA
someJsonField: Json!
Expand Down Expand Up @@ -64,6 +74,10 @@ import * as PrismaClient from \\".prisma/client\\"
import { core } from \\"nexus\\"
declare global {
interface NexusGenCustomInputMethods<TypeName extends string> {
/**
* The \`Byte\` scalar type represents byte value as a Buffer
*/
bytes<FieldName extends string>(fieldName: FieldName, opts?: core.CommonInputFieldConfig<TypeName, FieldName>): void // \\"Bytes\\";
/**
* A date-time string at UTC, such as 2007-12-03T10:15:30Z, compliant with the \`date-time\` format outlined in section 5.6 of the RFC 3339 profile of the ISO 8601 standard for representation of dates and times using the Gregorian calendar.
*/
Expand All @@ -76,6 +90,10 @@ declare global {
}
declare global {
interface NexusGenCustomOutputMethods<TypeName extends string> {
/**
* The \`Byte\` scalar type represents byte value as a Buffer
*/
bytes<FieldName extends string>(fieldName: FieldName, ...opts: core.ScalarOutSpread<TypeName, FieldName>): void // \\"Bytes\\";
/**
* A date-time string at UTC, such as 2007-12-03T10:15:30Z, compliant with the \`date-time\` format outlined in section 5.6 of the RFC 3339 profile of the ISO 8601 standard for representation of dates and times using the Gregorian calendar.
*/
Expand Down Expand Up @@ -105,6 +123,7 @@ export interface NexusGenScalars {
Float: number
Boolean: boolean
ID: string
Bytes: any
DateTime: any
Json: any
}
Expand All @@ -130,8 +149,10 @@ export interface NexusGenFieldTypes {
foo: NexusGenRootTypes['Foo'] | null; // Foo
}
Foo: { // field return type
BytesManually: NexusGenScalars['Bytes'] | null; // Bytes
DateTimeManually: NexusGenScalars['DateTime'] | null; // DateTime
JsonManually: NexusGenScalars['Json'] | null; // Json
someBytesField: NexusGenScalars['Bytes']; // Bytes!
someDateTimeField: NexusGenScalars['DateTime']; // DateTime!
someEnumA: NexusGenEnums['SomeEnumA'] | null; // SomeEnumA
someJsonField: NexusGenScalars['Json']; // Json!
Expand All @@ -146,8 +167,10 @@ export interface NexusGenFieldTypeNames {
foo: 'Foo'
}
Foo: { // field return type name
BytesManually: 'Bytes'
DateTimeManually: 'DateTime'
JsonManually: 'Json'
someBytesField: 'Bytes'
someDateTimeField: 'DateTime'
someEnumA: 'SomeEnumA'
someJsonField: 'Json'
Expand Down
6 changes: 6 additions & 0 deletions tests/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ it('When bundled custom scalars are used the project type checks and generates e
id String @id
someJsonField Json
someDateTimeField DateTime
someBytesField Bytes
someEnumA SomeEnumA
bar Bar?
}
Expand Down Expand Up @@ -152,6 +153,7 @@ it('When bundled custom scalars are used the project type checks and generates e
data: {
id: 'foo1',
someDateTimeField: new Date("2021-05-10T20:42:46.609Z"),
someBytesField: Buffer.from([]),
someJsonField: JSON.stringify({}),
someEnumA: 'alpha',
bar: {
Expand Down Expand Up @@ -206,8 +208,10 @@ it('When bundled custom scalars are used the project type checks and generates e
})
t.json('JsonManually')
t.dateTime('DateTimeManually')
t.bytes('BytesManually')
t.field(Foo.someJsonField.name, Foo.someJsonField)
t.field(Foo.someDateTimeField.name, Foo.someDateTimeField)
t.field(Foo.someBytesField.name, Foo.someBytesField)
},
}),
]
Expand Down Expand Up @@ -357,8 +361,10 @@ it('When bundled custom scalars are used the project type checks and generates e
foo {
JsonManually
DateTimeManually
BytesManually
someEnumA
someDateTimeField
someBytesField
}
}
}
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 @@ -15,6 +15,7 @@ testIntegration({
`,
apiSchema({ Foo }) {
return [
NexusPrismaScalars.Bytes,
NexusPrismaScalars.DateTime,
NexusPrismaScalars.Json,
objectType({
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/customScalarsModule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ assertBuildPresent()
it('scalars can be accessed via namespace import', () => {
expect(Object.keys(NexusPrismaScalarsNS)).toMatchInlineSnapshot(`
Array [
"Bytes",
"DateTime",
"Json",
"default",
Expand All @@ -16,6 +17,7 @@ it('scalars can be accessed via namespace import', () => {
it('scalars can be accessed via a default import', () => {
expect(Object.keys(NexusPrismaScalars)).toMatchInlineSnapshot(`
Array [
"Bytes",
"DateTime",
"Json",
]
Expand Down
3 changes: 3 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,9 @@

exports[`When a JSON field is defined in the Prisma schema it can be projected into the GraphQL API: graphqlSchema 1`] = `
"
\\"\\"\\"The \`Byte\` scalar type represents byte value as a Buffer\\"\\"\\"
scalar Bytes
\\"\\"\\"
A date-time string at UTC, such as 2007-12-03T10:15:30Z, compliant with the \`date-time\` format outlined in section 5.6 of the RFC 3339 profile of the ISO 8601 standard for representation of dates and times using the Gregorian calendar.
\\"\\"\\"
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.Bytes,
NexusPrismaScalars.DateTime,
NexusPrismaScalars.Json,
objectType({
Expand Down
Loading

0 comments on commit 88fd092

Please sign in to comment.