feat: dynamically populates richtext relationships
* feat: adds relationship field to test searchable input * fix: searching on relationship fields properly fetches results * chore: more dry relationship field * feat: sets default access control to requiring a user to be logged in * feat: dynamically populates richtext relationships * feat: allows depth param in graphql richText field * feat: ensures relationship input is initialized with up to 3 related collections
This commit is contained in:
@@ -3,7 +3,6 @@ import executeAccess from '../auth/executeAccess';
|
||||
import { Field, RelationshipField, fieldSupportsMany } from './config/types';
|
||||
import { Payload } from '..';
|
||||
|
||||
|
||||
type PopulateArgs = {
|
||||
depth: number
|
||||
currentDepth: number
|
||||
|
||||
134
src/fields/richTextRelationshipPromise.ts
Normal file
134
src/fields/richTextRelationshipPromise.ts
Normal file
@@ -0,0 +1,134 @@
|
||||
import { Collection } from '../collections/config/types';
|
||||
import { Payload } from '..';
|
||||
import { RichTextField } from './config/types';
|
||||
import { PayloadRequest } from '../express/types';
|
||||
|
||||
type Arguments = {
|
||||
data: unknown
|
||||
overrideAccess?: boolean
|
||||
depth: number
|
||||
currentDepth?: number
|
||||
payload: Payload
|
||||
field: RichTextField
|
||||
req: PayloadRequest
|
||||
}
|
||||
|
||||
type RecurseRichTextArgs = {
|
||||
children: unknown[]
|
||||
overrideAccess: boolean
|
||||
depth: number
|
||||
currentDepth: number
|
||||
payload: Payload
|
||||
field: RichTextField
|
||||
req: PayloadRequest
|
||||
promises: Promise<void>[]
|
||||
}
|
||||
|
||||
const populate = async ({
|
||||
id,
|
||||
collection,
|
||||
data,
|
||||
overrideAccess,
|
||||
depth,
|
||||
currentDepth,
|
||||
payload,
|
||||
req,
|
||||
}: Arguments & {
|
||||
id: string,
|
||||
collection: Collection
|
||||
}) => {
|
||||
const dataRef = data as Record<string, unknown>;
|
||||
|
||||
const doc = await payload.operations.collections.findByID({
|
||||
req: {
|
||||
...req,
|
||||
payloadAPI: 'local',
|
||||
},
|
||||
collection,
|
||||
id,
|
||||
currentDepth: currentDepth + 1,
|
||||
overrideAccess,
|
||||
disableErrors: true,
|
||||
depth,
|
||||
});
|
||||
|
||||
if (doc) {
|
||||
dataRef.value = doc;
|
||||
} else {
|
||||
dataRef.value = null;
|
||||
}
|
||||
};
|
||||
|
||||
const recurseRichText = ({
|
||||
req,
|
||||
children,
|
||||
payload,
|
||||
overrideAccess = false,
|
||||
depth,
|
||||
currentDepth = 0,
|
||||
field,
|
||||
promises,
|
||||
}: RecurseRichTextArgs) => {
|
||||
if (Array.isArray(children)) {
|
||||
(children as any[]).forEach((element) => {
|
||||
const collection = payload.collections[element?.relationTo];
|
||||
|
||||
if (element.type === 'relationship'
|
||||
&& element?.value?.id
|
||||
&& collection
|
||||
&& (depth && currentDepth <= depth)) {
|
||||
promises.push(populate({
|
||||
req,
|
||||
id: element.value.id,
|
||||
data: element,
|
||||
overrideAccess,
|
||||
depth,
|
||||
currentDepth,
|
||||
payload,
|
||||
field,
|
||||
collection,
|
||||
}));
|
||||
}
|
||||
|
||||
if (element?.children) {
|
||||
recurseRichText({
|
||||
req,
|
||||
children: element.children,
|
||||
payload,
|
||||
overrideAccess,
|
||||
depth,
|
||||
currentDepth,
|
||||
field,
|
||||
promises,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const richTextRelationshipPromise = ({
|
||||
req,
|
||||
data,
|
||||
payload,
|
||||
overrideAccess,
|
||||
depth,
|
||||
currentDepth,
|
||||
field,
|
||||
}: Arguments) => async (): Promise<void> => {
|
||||
const promises = [];
|
||||
|
||||
recurseRichText({
|
||||
req,
|
||||
children: data[field.name],
|
||||
payload,
|
||||
overrideAccess,
|
||||
depth,
|
||||
currentDepth,
|
||||
field,
|
||||
promises,
|
||||
});
|
||||
|
||||
await Promise.all(promises);
|
||||
};
|
||||
|
||||
export default richTextRelationshipPromise;
|
||||
@@ -5,6 +5,7 @@ import { Field, fieldHasSubFields, fieldIsArrayType, fieldIsBlockType, HookName
|
||||
import { Operation } from '../types';
|
||||
import { PayloadRequest } from '../express/types';
|
||||
import { Payload } from '..';
|
||||
import richTextRelationshipPromise from './richTextRelationshipPromise';
|
||||
|
||||
type Arguments = {
|
||||
fields: Field[]
|
||||
@@ -91,8 +92,22 @@ const traverseFields = (args: Arguments): void => {
|
||||
if (data[field.name] === '') dataCopy[field.name] = false;
|
||||
}
|
||||
|
||||
if (field.type === 'richText' && typeof data[field.name] === 'string') {
|
||||
dataCopy[field.name] = JSON.parse(data[field.name] as string);
|
||||
if (field.type === 'richText') {
|
||||
if (typeof data[field.name] === 'string') {
|
||||
dataCopy[field.name] = JSON.parse(data[field.name] as string);
|
||||
}
|
||||
|
||||
if ((field.admin?.elements?.includes('relationship') || !field?.admin?.elements) && hook === 'afterRead') {
|
||||
relationshipPopulations.push(richTextRelationshipPromise({
|
||||
req,
|
||||
data,
|
||||
payload,
|
||||
overrideAccess,
|
||||
depth,
|
||||
field,
|
||||
currentDepth,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
const hasLocalizedValue = (typeof data?.[field.name] === 'object' && data?.[field.name] !== null)
|
||||
|
||||
Reference in New Issue
Block a user