Skip to content

Commit

Permalink
fix(db-mongodb): ensure relationships are stored in ObjectID (#8932)
Browse files Browse the repository at this point in the history
### What?
Since the join field, we do store relationship fields values in
`ObjectID`. This wasn't true if the field is nested to an array /
blocks.

### Why?
All relationship fields values should be stored in `ObjectID`.

### How?
Fixes arrays / blocks handling in the `traverseFields.ts` function.
Before it didn't run for them.
  • Loading branch information
r1tsuu authored Oct 30, 2024
1 parent c41ef65 commit d64946c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
60 changes: 56 additions & 4 deletions packages/payload/src/utilities/traverseFields.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
import type { Field, TabAsField } from '../fields/config/types.js'
import type { ArrayField, BlocksField, Field, TabAsField } from '../fields/config/types.js'

import { fieldHasSubFields } from '../fields/config/types.js'

const traverseArrayOrBlocksField = ({
callback,
data,
field,
parentRef,
}: {
callback: TraverseFieldsCallback
data: Record<string, unknown>[]
field: ArrayField | BlocksField
parentRef?: unknown
}) => {
for (const ref of data) {
let fields: Field[]
if (field.type === 'blocks' && typeof ref?.blockType === 'string') {
const block = field.blocks.find((block) => block.slug === ref.blockType)
fields = block?.fields
} else if (field.type === 'array') {
fields = field.fields
}

if (fields) {
traverseFields({ callback, fields, parentRef, ref })
}
}
}

export type TraverseFieldsCallback = (args: {
/**
* The current field
Expand Down Expand Up @@ -68,11 +94,11 @@ export const traverseFields = ({
})
return
}
if (field.type !== 'tab' && fieldHasSubFields(field)) {
if (field.type !== 'tab' && (fieldHasSubFields(field) || field.type === 'blocks')) {
const parentRef = ref
if ('name' in field && field.name) {
if (typeof ref[field.name] === 'undefined') {
if (field.type === 'array') {
if (field.type === 'array' || field.type === 'blocks') {
if (field.localized) {
ref[field.name] = {}
} else {
Expand All @@ -85,7 +111,33 @@ export const traverseFields = ({
}
ref = ref[field.name]
}
traverseFields({ callback, fields: field.fields, parentRef, ref })

if (field.type === 'blocks' || field.type === 'array') {
if (field.localized) {
for (const key in (ref ?? {}) as Record<string, unknown>) {
const localeData = ref[key]
if (!Array.isArray(localeData)) {
continue
}

traverseArrayOrBlocksField({
callback,
data: localeData,
field,
parentRef,
})
}
} else if (Array.isArray(ref)) {
traverseArrayOrBlocksField({
callback,
data: ref,
field,
parentRef,
})
}
} else {
traverseFields({ callback, fields: field.fields, parentRef, ref })
}
}
})
}
1 change: 0 additions & 1 deletion test/joins/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,6 @@ describe('Joins Field', () => {
}
}`

expect(true).toBeTruthy()
const response = await restClient
.GRAPHQL_POST({ body: JSON.stringify({ query }) })
.then((res) => res.json())
Expand Down

0 comments on commit d64946c

Please sign in to comment.