Skip to content

Commit

Permalink
refactor: remove photon terminology (#778)
Browse files Browse the repository at this point in the history
closes #764
  • Loading branch information
Jason Kuhrt authored Aug 4, 2020
1 parent e96587c commit 1a6db99
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 88 deletions.
60 changes: 0 additions & 60 deletions preview.json

This file was deleted.

40 changes: 21 additions & 19 deletions src/schema/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { proxifyModelFunction, proxifyPublishers } from './proxifier'
import { Publisher } from './publisher'
import * as Typegen from './typegen'
import {
assertPhotonInContext,
assertPrismaClientInContext,
GlobalComputedInputs,
Index,
indexBy,
Expand Down Expand Up @@ -94,10 +94,10 @@ const dmmfListFieldTypeToNexus = (fieldType: DmmfTypes.SchemaField['outputType']
type PrismaClientFetcher = (ctx: Nexus.core.GetGen<'context'>) => any

export interface Options {
// TODO return type should be Photon
// TODO return type should be Prisma Client
/**
* nexus-prisma will call this to get a reference to an instance of the Prisma Client.
* The function is passed the context object. Typically a Photon instance will
* The function is passed the context object. Typically a Prisma Client instance will
* be available on the context to support your custom resolvers. Therefore the
* default getter returns `ctx.prisma`.
*/
Expand Down Expand Up @@ -267,9 +267,9 @@ export class SchemaBuilder {
this.wasCrudUsedButDisabled = false

this.getPrismaClient = (ctx: any) => {
const photon = config.prismaClient(ctx)
assertPhotonInContext(photon)
return photon
const prismaClient = config.prismaClient(ctx)
assertPrismaClientInContext(prismaClient)
return prismaClient
}
if (config.shouldGenerateArtifacts) {
Typegen.generateSync({
Expand Down Expand Up @@ -322,7 +322,7 @@ export class SchemaBuilder {
const schemaArgsIndex = indexBy(mappedField.field.args, 'name')

const originalResolve: GraphQLFieldResolver<any, any, any> = (_root, args, ctx, info) => {
const photon = this.getPrismaClient(ctx)
const prismaClient = this.getPrismaClient(ctx)
if (
typeName === 'Mutation' &&
(!isEmptyObject(publisherConfig.locallyComputedInputs) ||
Expand All @@ -343,7 +343,7 @@ export class SchemaBuilder {

args = this.paginationStrategy.resolve(args)

return photon[mappedField.photonAccessor][mappedField.operation](args)
return prismaClient[mappedField.prismaClientAccessor][mappedField.operation](args)
}

const fieldConfig = this.buildFieldConfig({
Expand Down Expand Up @@ -513,12 +513,12 @@ export class SchemaBuilder {
)
}

const photon = this.getPrismaClient(ctx)
const prismaClient = this.getPrismaClient(ctx)

args = transformNullsToUndefined(args, schemaArgsIndex, this.dmmf)
args = this.paginationStrategy.resolve(args)

return photon[lowerFirst(mapping.model)]
return prismaClient[lowerFirst(mapping.model)]
.findOne({
where: Constraints.buildWhereUniqueInput(root, uniqueIdentifiers),
})
Expand Down Expand Up @@ -609,18 +609,20 @@ export class SchemaBuilder {

argsFromMutationField({ publisherConfig, field }: FieldConfigData): CustomInputArg[] {
return field.args.map((arg) => {
const photonInputType = this.dmmf.getInputType(arg.inputType.type)
const prismaClientInputType = this.dmmf.getInputType(arg.inputType.type)
/*
Since globallyComputedInputs were already filtered during schema transformation,
at this point we just need to filter at the resolver-level.
*/
return {
arg,
type: {
...photonInputType,
...prismaClientInputType,
fields: publisherConfig.locallyComputedInputs
? photonInputType.fields.filter((field) => !(field.name in publisherConfig.locallyComputedInputs))
: photonInputType.fields,
? prismaClientInputType.fields.filter(
(field) => !(field.name in publisherConfig.locallyComputedInputs)
)
: prismaClientInputType.fields,
},
}
})
Expand Down Expand Up @@ -738,28 +740,28 @@ export class SchemaBuilder {
fieldName: string,
graphQLTypeName: string
): DmmfTypes.InputType {
const photonObject = this.dmmf.getInputType(inputTypeName)
const prismaClientObject = this.dmmf.getInputType(inputTypeName)

// If the publishing for this field feature (filtering, ordering, ...)
// has not been tailored then we may simply pass through the backing
// version as-is.
//
if (fieldWhitelist === true) {
return photonObject
return prismaClientObject
}

// REFACTOR use an intersection function
const whitelistedFieldNames = Object.keys(fieldWhitelist)
const userExposedObjectFields = photonObject.fields.filter((field) =>
const userExposedObjectFields = prismaClientObject.fields.filter((field) =>
whitelistedFieldNames.includes(field.name)
)

const uniqueName = photonObject.isWhereType
const uniqueName = prismaClientObject.isWhereType
? this.argsNamingStrategy.whereInput(graphQLTypeName, fieldName)
: this.argsNamingStrategy.orderByInput(graphQLTypeName, fieldName)

return {
...photonObject,
...prismaClientObject,
name: uniqueName,
fields: userExposedObjectFields,
}
Expand Down
10 changes: 5 additions & 5 deletions src/schema/dmmf/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { paginationStrategies, PaginationStrategy } from '../pagination'
import { GlobalComputedInputs, GlobalMutationResolverParams, LocalComputedInputs } from '../utils'
import { DmmfDocument } from './DmmfDocument'
import { DmmfTypes } from './DmmfTypes'
import { getPhotonDmmf } from './utils'
import { getPrismaClientDmmf } from './utils'

export type TransformOptions = {
globallyComputedInputs?: GlobalComputedInputs
paginationStrategy?: PaginationStrategy
}

export const getTransformedDmmf = (
photonClientPackagePath: string,
prismaClientPackagePath: string,
options?: TransformOptions
): DmmfDocument => new DmmfDocument(transform(getPhotonDmmf(photonClientPackagePath), options))
): DmmfDocument => new DmmfDocument(transform(getPrismaClientDmmf(prismaClientPackagePath), options))

const addDefaultOptions = (givenOptions?: TransformOptions): Required<TransformOptions> => ({
globallyComputedInputs: {},
Expand Down Expand Up @@ -84,7 +84,7 @@ function transformSchema(
/**
* Conversion from a Prisma Client arg type to a GraphQL arg type using
* heuristics. A conversion is needed because GraphQL does not
* support union types on args, but Photon does.
* support union types on args, but Prisma Client does.
*/
function transformArg(arg: DMMF.SchemaArg): DmmfTypes.SchemaArg {
// FIXME: *Enum*Filter are currently empty
Expand Down Expand Up @@ -222,7 +222,7 @@ function transformInputType(
}

/**
* Make the "return type" property type always be a string. In Photon
* Make the "return type" property type always be a string. In Prisma Client
* it is allowed to be a nested structured object but we want only the
* reference-by-name form.
*
Expand Down
2 changes: 1 addition & 1 deletion src/schema/dmmf/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DMMF } from '@prisma/client/runtime'

export const getPhotonDmmf = (packagePath: string): DMMF.Document => {
export const getPrismaClientDmmf = (packagePath: string): DMMF.Document => {
let dmmf: undefined | DMMF.Document = undefined

try {
Expand Down
4 changes: 2 additions & 2 deletions src/schema/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface BaseMappedField {
field: string
operation: OperationName
model: string
photonAccessor: string
prismaClientAccessor: string
}

export interface MappedField extends Omit<BaseMappedField, 'field'> {
Expand All @@ -22,7 +22,7 @@ const buildField = (mapping: DmmfTypes.Mapping, operation: OperationName): BaseM
operation,
field: mapping[operation]!,
model: mapping.model,
photonAccessor: lowerFirst(mapping.model),
prismaClientAccessor: lowerFirst(mapping.model),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/schema/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function dmmfFieldToNexusFieldConfig(param: {
}
}

export function assertPhotonInContext(prismaClient: any) {
export function assertPrismaClientInContext(prismaClient: any) {
if (!prismaClient) {
throw new Error('Could not find Prisma Client JS in context (ctx.prisma)')
}
Expand Down

0 comments on commit 1a6db99

Please sign in to comment.