-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: convert nulls to undefined for non-nullable Prisma fields (#695)
- Loading branch information
Showing
7 changed files
with
293 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { DmmfDocument, DmmfTypes } from './dmmf' | ||
|
||
/** | ||
* Take the incoming GraphQL args of a resolver and replaces all `null` values | ||
* that maps to a non-nullable field in the Prisma Schema, by `undefined` values. | ||
* | ||
* In Prisma, a `null` value has a specific meaning for the underlying database. | ||
* Therefore, `undefined` is used instead to express the optionality of a field. | ||
* | ||
* In GraphQL however, no difference is made between `null` and `undefined`. | ||
* This is the reason why we need to convert all `null` values that were assigned to `non-nullable` fields to `undefined`. | ||
*/ | ||
export function transformNullsToUndefined( | ||
graphqlArgs: Record<string, any>, | ||
prismaArgs: Record<string, DmmfTypes.SchemaArg>, | ||
dmmf: DmmfDocument, | ||
) { | ||
const keys = Object.keys(graphqlArgs) | ||
for (const key of keys) { | ||
const val = graphqlArgs[key] | ||
const prismaArg = prismaArgs[key] | ||
|
||
if (!prismaArg) { | ||
throw new Error(`Could not find schema arg with name: ${key}`) | ||
} | ||
|
||
const shouldConvertNullToUndefined = | ||
val === null && prismaArg.inputType.isNullable === false | ||
|
||
if (shouldConvertNullToUndefined) { | ||
graphqlArgs[key] = undefined | ||
} else if (isObject(val)) { | ||
const nestedPrismaArgs = dmmf.getInputTypeWithIndexedFields( | ||
prismaArg.inputType.type, | ||
).fields | ||
|
||
graphqlArgs[key] = transformNullsToUndefined(graphqlArgs[key], nestedPrismaArgs, dmmf) | ||
} | ||
} | ||
|
||
return graphqlArgs | ||
} | ||
|
||
function isObject(obj: any): boolean { | ||
return obj && typeof obj === 'object' | ||
} |
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,52 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`create: converts nulls to undefined when fields are not nullable 1`] = ` | ||
Object { | ||
"data": Object { | ||
"id": undefined, | ||
"posts": Object { | ||
"connect": undefined, | ||
"create": Object { | ||
"id": "titi", | ||
}, | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`findMany: converts nulls to undefined when fields are not nullable 1`] = ` | ||
Object { | ||
"after": undefined, | ||
"before": undefined, | ||
"first": 1, | ||
"orderBy": Object { | ||
"birthDate": null, | ||
"email": null, | ||
"id": "asc", | ||
}, | ||
"where": Object { | ||
"AND": undefined, | ||
"NOT": Object { | ||
"AND": Object { | ||
"birthDate": undefined, | ||
}, | ||
"posts": null, | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`model filtering: converts nulls to undefined when fields are not nullable 1`] = ` | ||
Object { | ||
"where": Object { | ||
"authors": Object { | ||
"every": Object { | ||
"birthDate": undefined, | ||
"email": null, | ||
"posts": null, | ||
}, | ||
}, | ||
"id": undefined, | ||
}, | ||
} | ||
`; |
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,149 @@ | ||
import { DmmfTypes, DmmfDocument } from '../../src/dmmf' | ||
import { getCrudMappedFields } from '../../src/mapping' | ||
import { OperationName } from '../../src/naming-strategies' | ||
import { transformNullsToUndefined } from '../../src/null' | ||
import { indexBy } from '../../src/utils' | ||
import { getDmmf } from '../__utils' | ||
|
||
const operationToRoot: Record<OperationName, 'Query' | 'Mutation'> = { | ||
findOne: 'Query', | ||
findMany: 'Query', | ||
create: 'Mutation', | ||
delete: 'Mutation', | ||
deleteMany: 'Mutation', | ||
update: 'Mutation', | ||
updateMany: 'Mutation', | ||
upsert: 'Mutation' | ||
} | ||
|
||
async function getSchemaArgsForCrud( | ||
datamodel: string, | ||
model: string, | ||
operation: OperationName, | ||
): Promise<{ | ||
schemaArgs: Record<string, DmmfTypes.SchemaArg> | ||
dmmf: DmmfDocument | ||
}> { | ||
const dmmf = await getDmmf(datamodel) | ||
const mappedField = getCrudMappedFields(operationToRoot[operation], dmmf).find( | ||
x => x.operation === operation && x.model === model, | ||
) | ||
|
||
if (!mappedField) { | ||
throw new Error( | ||
`Could not find mapped fields for model ${model} and operation ${operation}`, | ||
) | ||
} | ||
|
||
return { | ||
schemaArgs: indexBy(mappedField.field.args, 'name'), | ||
dmmf, | ||
} | ||
} | ||
|
||
test('findMany: converts nulls to undefined when fields are not nullable', async () => { | ||
const datamodel = ` | ||
model User { | ||
id String @default(cuid()) @id | ||
email String? @unique | ||
birthDate DateTime | ||
posts Post[] | ||
} | ||
model Post { | ||
id String @default(cuid()) @id | ||
authors User[] @relation(references: [id]) | ||
} | ||
` | ||
const { dmmf, schemaArgs } = await getSchemaArgsForCrud(datamodel, 'User', 'findMany') | ||
const incomingArgs = { | ||
before: null, // not nullable | ||
after: null, // not nullable | ||
first: 1, | ||
orderBy: { | ||
email: null, // nullable | ||
birthDate: null, // nullable | ||
id: 'asc', | ||
}, | ||
where: { | ||
AND: null, // not nullable | ||
NOT: { | ||
AND: { | ||
birthDate: null, // not nullable | ||
}, | ||
posts: null, | ||
}, | ||
}, | ||
} | ||
|
||
const result = transformNullsToUndefined(incomingArgs, schemaArgs, dmmf) | ||
|
||
expect(result).toMatchSnapshot() | ||
}) | ||
|
||
test('create: converts nulls to undefined when fields are not nullable', async () => { | ||
const datamodel = ` | ||
model User { | ||
id String @default(cuid()) @id | ||
email String? @unique | ||
birthDate DateTime | ||
posts Post[] | ||
} | ||
model Post { | ||
id String @default(cuid()) @id | ||
authors User[] @relation(references: [id]) | ||
} | ||
` | ||
const { dmmf, schemaArgs } = await getSchemaArgsForCrud(datamodel, 'User', 'create') | ||
const incomingArgs = { | ||
data: { | ||
id: null, // not nullable | ||
posts: { | ||
connect: null, // not nullable | ||
create: { | ||
id: 'titi', | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
const result = transformNullsToUndefined(incomingArgs, schemaArgs, dmmf) | ||
|
||
expect(result).toMatchSnapshot() | ||
}) | ||
|
||
test('model filtering: converts nulls to undefined when fields are not nullable', async () => { | ||
const datamodel = ` | ||
model User { | ||
id String @default(cuid()) @id | ||
email String? @unique | ||
birthDate DateTime | ||
posts Post[] | ||
} | ||
model Post { | ||
id String @default(cuid()) @id | ||
authors User[] @relation(references: [id]) | ||
} | ||
` | ||
const dmmf = await getDmmf(datamodel) | ||
const schemaArgs = dmmf.getOutputType('User').fields.find(f => f.name === 'posts')?.args! | ||
const indexedSchemaArgs = indexBy(schemaArgs, 'name') | ||
const incomingArgs = { | ||
where: { | ||
id: null, // not nullable | ||
authors: { | ||
every: { | ||
email: null, | ||
birthDate: null, // not nullable | ||
posts: null, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
const result = transformNullsToUndefined(incomingArgs, indexedSchemaArgs, dmmf) | ||
|
||
expect(result).toMatchSnapshot() | ||
}) |