Skip to content

Commit

Permalink
fix: 🐛 disconnect equals true for one to one relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
dennemark committed Sep 23, 2024
1 parent 7bd7083 commit 43d884d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/applyDataQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,18 @@ export function applyDataQuery(
const isConnection = nestedAction === 'connect' || nestedAction === 'disconnect'

tree.children[field] = { action: mutationAction, model: relationModel.type as Prisma.ModelName, children: {} }
const dataQuery = applyDataQuery(abilities, nestedArgs, mutationAction, relationModel.type, tree.children[field])
mutation[field][nestedAction] = dataQuery.args
// connection works like a where query, so we apply it
if (isConnection) {
const accessibleQuery = accessibleBy(abilities, mutationAction)[relationModel.type as Prisma.ModelName]
if (nestedAction !== 'disconnect' && nestedArgs !== true) {
const dataQuery = applyDataQuery(abilities, nestedArgs, mutationAction, relationModel.type, tree.children[field])
mutation[field][nestedAction] = dataQuery.args
// connection works like a where query, so we apply it
if (isConnection) {
const accessibleQuery = accessibleBy(abilities, mutationAction)[relationModel.type as Prisma.ModelName]

if (Array.isArray(mutation[field][nestedAction])) {
mutation[field][nestedAction] = mutation[field][nestedAction].map((q) => applyAccessibleQuery(q, accessibleQuery))
} else {
mutation[field][nestedAction] = applyAccessibleQuery(mutation[field][nestedAction], accessibleQuery)
if (Array.isArray(mutation[field][nestedAction])) {
mutation[field][nestedAction] = mutation[field][nestedAction].map((q) => applyAccessibleQuery(q, accessibleQuery))
} else {
mutation[field][nestedAction] = applyAccessibleQuery(mutation[field][nestedAction], accessibleQuery)
}
}
}
} else {
Expand Down
14 changes: 14 additions & 0 deletions test/applyDataQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ describe('apply data query', () => {
})

})
describe('disconnect', () => {
it('accepts disconnect: true', () => {
const { can, build } = abilityBuilder()
can('update', 'User', {
id: 0
})
can('update', 'Post', {
id: 1
})
const result = applyDataQuery(build(), { data: { id: 1, posts: { disconnect: true } }, where: { id: 0 } }, 'update', 'User')
expect(result.args).toEqual({ data: { id: 1, posts: { disconnect: true } }, where: { id: 0, AND: [{ OR: [{ id: 0 }] }] } })
expect(result.creationTree).toEqual({ children: { posts: { children: {}, action: 'update', model: 'Post', } }, model: 'User', action: "update" })
})
})
describe('connectOrCreate', () => {
it('adds where and connection clause in nested connection update', () => {
const { can, build } = abilityBuilder()
Expand Down

0 comments on commit 43d884d

Please sign in to comment.