Skip to content

Commit

Permalink
feat: enable configuring scalars (#712)
Browse files Browse the repository at this point in the history
  • Loading branch information
Weakky authored Jun 16, 2020
1 parent 18e017d commit fe9611e
Show file tree
Hide file tree
Showing 26 changed files with 1,354 additions and 4,258 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"author": "Flavian Desverne <desverne@prisma.io>",
"files": [
"dist",
"src"
"src",
"typegen.js",
"typegen.d.ts"
],
"scripts": {
"release:preview": "dripip preview",
Expand Down Expand Up @@ -53,6 +55,7 @@
"get-port": "5.1.1",
"graphql": "14.6.0",
"graphql-request": "1.8.2",
"graphql-scalars": "^1.1.5",
"graphql-yoga": "1.18.3",
"jest": "25.2.4",
"jest-watch-typeahead": "0.4.2",
Expand Down
16 changes: 14 additions & 2 deletions src/builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Nexus from '@nexus/schema'
import { DynamicOutputPropertyDef } from '@nexus/schema/dist/dynamicProperty'
import { defaultFieldResolver, GraphQLFieldResolver } from 'graphql'
import { defaultFieldResolver, GraphQLFieldResolver, GraphQLScalarType } from 'graphql'
import * as path from 'path'
import * as Constraints from './constraints'
import { addComputedInputs, DmmfDocument, DmmfTypes, getTransformedDmmf } from './dmmf'
Expand Down Expand Up @@ -143,12 +143,21 @@ export interface Options {
* @default false
*/
experimentalCRUD?: boolean
/**
* Map of GraphQL scalar types to be used by the library for the Prisma scalars
*
* When not provided, the scalar types will be passthrough.
*
* @default {}
*/
scalars?: Partial<Record<Typegen.GetGen<'scalars'>, GraphQLScalarType>>
computedInputs?: GlobalComputedInputs
}

export interface InternalOptions extends Options {
dmmf?: DmmfDocument // For testing
nexusBuilder: Nexus.PluginBuilderLens
nexusPrismaImportId?: string,
onUnknownFieldName?: OnUnknownFieldName // For pumpkins
onUnknownFieldType?: OnUnknownFieldType // For pumpkins
onUnknownArgName?: OnUnknownArgName // For pumpkins
Expand Down Expand Up @@ -220,6 +229,7 @@ export class SchemaBuilder {
protected paginationStrategy: PaginationStrategyTypes
protected getPrismaClient: PrismaClientFetcher
protected publisher: Publisher
protected scalars: Record<string, GraphQLScalarType>
protected globallyComputedInputs: GlobalComputedInputs
protected unknownFieldsByModel: Index<string[]>
public wasCrudUsedButDisabled: boolean
Expand All @@ -240,7 +250,8 @@ export class SchemaBuilder {
globallyComputedInputs: this.globallyComputedInputs,
paginationStrategy: this.paginationStrategy,
})
this.publisher = new Publisher(this.dmmf, config.nexusBuilder)
this.scalars = options.scalars as any ?? {}
this.publisher = new Publisher(this.dmmf, config.nexusBuilder, this.scalars)
this.unknownFieldsByModel = {}

this.argsNamingStrategy = defaultArgsNamingStrategy
Expand All @@ -257,6 +268,7 @@ export class SchemaBuilder {
prismaClientPath: config.inputs.prismaClient,
typegenPath: config.outputs.typegen,
paginationStrategy: this.paginationStrategy,
nexusPrismaImportId: options.nexusPrismaImportId
})
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/dmmf/DmmfDocument.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DmmfTypes } from './DmmfTypes'
import { indexBy, Index } from '../utils'
import { scalarsNameValues } from '../graphql'

export class DmmfDocument implements DmmfTypes.Document {
public datamodel: DmmfTypes.Datamodel
Expand All @@ -13,6 +14,7 @@ export class DmmfDocument implements DmmfTypes.Document {
public enumsIndex: Index<DmmfTypes.Enum>
public modelsIndex: Index<DmmfTypes.Model>
public inputTypesIndexWithFields: InputTypeIndexWithField
public customScalars: Array<string>

constructor({ datamodel, schema, mappings }: DmmfTypes.Document) {
// ExternalDMMF
Expand All @@ -27,6 +29,7 @@ export class DmmfDocument implements DmmfTypes.Document {
this.outputTypesIndex = indexBy(schema.outputTypes, 'name')
this.mappingsIndex = indexBy(mappings, 'model')
this.inputTypesIndexWithFields = indexInputTypeWithFields(schema.inputTypes)
this.customScalars = findCustomScalars(datamodel.models)

// Entrypoints
this.queryObject = this.getOutputType('Query')
Expand Down Expand Up @@ -168,3 +171,17 @@ function indexInputTypeWithFields(inputTypes: DmmfTypes.InputType[]) {

return indexedInputTypes
}

function findCustomScalars(models: DmmfTypes.Model[]): Array<string> {
const customScalars = new Set<string>()

for (const model of models) {
for (const field of model.fields) {
if (field.kind === 'scalar' && !scalarsNameValues.includes(field.type as any)) {
customScalars.add(field.type)
}
}
}

return [...customScalars]
}
12 changes: 9 additions & 3 deletions src/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { CustomInputArg } from './builder'
import { DmmfDocument, DmmfTypes } from './dmmf'
import { scalarsNameValues } from './graphql'
import { dmmfFieldToNexusFieldConfig, Index } from './utils'
import { GraphQLScalarType } from 'graphql'

export class Publisher {
typesPublished: Index<boolean> = {}
constructor(public dmmf: DmmfDocument, public nexusBuilder: Nexus.PluginBuilderLens) {}
constructor(public dmmf: DmmfDocument, public nexusBuilder: Nexus.PluginBuilderLens, public scalars: Record<string, GraphQLScalarType>) {}

inputType(
customArg: CustomInputArg
Expand All @@ -15,7 +16,8 @@ export class Publisher {
| Nexus.core.NexusInputObjectTypeDef<string>
| Nexus.core.NexusEnumTypeDef<string>
| Nexus.core.NexusScalarTypeDef<string>
| Nexus.core.NexusArgDef<any> {
| Nexus.core.NexusArgDef<any>
| GraphQLScalarType {
const typeName = customArg.type.name

// If type is already published, just reference it
Expand Down Expand Up @@ -91,6 +93,10 @@ export class Publisher {

this.markTypeAsPublished(typeName)

if (this.scalars[typeName]) {
return this.scalars[typeName]
}

return Nexus.scalarType({
name: typeName,
serialize(value) {
Expand Down Expand Up @@ -161,7 +167,7 @@ export class Publisher {

isPublished(typeName: string) {
// If the user's app has published a type of the same name treat it as an
// overide to us auto publishing.
// override to us auto publishing.
return this.nexusBuilder.hasType(typeName) || this.typesPublished[typeName]
}

Expand Down
Loading

0 comments on commit fe9611e

Please sign in to comment.