fix(db-mongodb): ensure relationships are stored in ObjectID (#8932)

### 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.
This commit is contained in:
Sasha
2024-10-30 19:42:07 +02:00
committed by GitHub
parent c41ef65a2b
commit d64946c2e2
2 changed files with 56 additions and 5 deletions

View File

@@ -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
@@ -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 {
@@ -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 })
}
}
})
}

View File

@@ -643,7 +643,6 @@ describe('Joins Field', () => {
}
}`
expect(true).toBeTruthy()
const response = await restClient
.GRAPHQL_POST({ body: JSON.stringify({ query }) })
.then((res) => res.json())