Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: deep querying, handle getLocalizedPaths for blocks #10187

Merged
merged 3 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions packages/payload/src/database/getLocalizedPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,32 @@ export function getLocalizedPaths({

let fieldsToSearch: FlattenedField[]

if (lastIncompletePath?.field && 'flattenedFields' in lastIncompletePath.field) {
fieldsToSearch = lastIncompletePath.field.flattenedFields
let matchedField: FlattenedField

if (lastIncompletePath?.field?.type === 'blocks') {
if (segment === 'blockType') {
matchedField = {
name: 'blockType',
type: 'text',
}
} else {
for (const block of lastIncompletePath.field.blocks) {
matchedField = block.flattenedFields.find((field) => field.name === segment)
if (matchedField) {
break
}
}
}
} else {
fieldsToSearch = lastIncompletePath.fields
if (lastIncompletePath?.field && 'flattenedFields' in lastIncompletePath.field) {
fieldsToSearch = lastIncompletePath.field.flattenedFields
} else {
fieldsToSearch = lastIncompletePath.fields
}

matchedField = fieldsToSearch.find((field) => field.name === segment)
}

const matchedField = fieldsToSearch.find((field) => field.name === segment)
lastIncompletePath.field = matchedField

if (currentPath === 'globalType' && globalSlug) {
Expand Down Expand Up @@ -94,7 +113,6 @@ export function getLocalizedPaths({
}

switch (matchedField.type) {
case 'blocks':
case 'json':
case 'richText': {
const upcomingSegments = pathSegments.slice(i + 1).join('.')
Expand Down
41 changes: 41 additions & 0 deletions test/relationships/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,47 @@ export default buildConfigWithDefaults({
},
],
},
{
slug: 'deep-nested',
fields: [
{
type: 'tabs',
tabs: [
{
name: 'content',
fields: [
{
type: 'blocks',
name: 'blocks',
blocks: [
{
slug: 'testBlock',
fields: [
{
type: 'tabs',
tabs: [
{
name: 'meta',
fields: [
{
type: 'relationship',
relationTo: 'movies',
name: 'movie',
},
],
},
],
},
],
},
],
},
],
},
],
},
],
},
],
onInit: async (payload) => {
await payload.create({
Expand Down
32 changes: 32 additions & 0 deletions test/relationships/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,38 @@ describe('Relationships', () => {
expect(docs[0].id).toBe(doc.id)
})

it('should allow querying within tabs-blocks-tabs', async () => {
const movie = await payload.create({ collection: 'movies', data: { name: 'Pulp Fiction' } })

const { id } = await payload.create({
collection: 'deep-nested',
data: {
content: {
blocks: [
{
blockType: 'testBlock',
meta: {
movie: movie.id,
},
},
],
},
},
})

const result = await payload.find({
collection: 'deep-nested',
where: {
'content.blocks.meta.movie': {
equals: movie.id,
},
},
})

expect(result.totalDocs).toBe(1)
expect(result.docs[0].id).toBe(id)
})

describe('Custom ID', () => {
it('should query a custom id relation', async () => {
const { customIdRelation } = await restClient
Expand Down
54 changes: 54 additions & 0 deletions test/relationships/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface Config {
'rels-to-pages': RelsToPage;
'rels-to-pages-and-custom-text-ids': RelsToPagesAndCustomTextId;
'object-writes': ObjectWrite;
'deep-nested': DeepNested;
users: User;
'payload-locked-documents': PayloadLockedDocument;
'payload-preferences': PayloadPreference;
Expand All @@ -52,6 +53,7 @@ export interface Config {
'rels-to-pages': RelsToPagesSelect<false> | RelsToPagesSelect<true>;
'rels-to-pages-and-custom-text-ids': RelsToPagesAndCustomTextIdsSelect<false> | RelsToPagesAndCustomTextIdsSelect<true>;
'object-writes': ObjectWritesSelect<false> | ObjectWritesSelect<true>;
'deep-nested': DeepNestedSelect<false> | DeepNestedSelect<true>;
users: UsersSelect<false> | UsersSelect<true>;
'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
'payload-preferences': PayloadPreferencesSelect<false> | PayloadPreferencesSelect<true>;
Expand Down Expand Up @@ -350,6 +352,27 @@ export interface ObjectWrite {
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "deep-nested".
*/
export interface DeepNested {
id: string;
content?: {
blocks?:
| {
meta?: {
movie?: (string | null) | Movie;
};
id?: string | null;
blockName?: string | null;
blockType: 'testBlock';
}[]
| null;
};
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-locked-documents".
Expand Down Expand Up @@ -425,6 +448,10 @@ export interface PayloadLockedDocument {
relationTo: 'object-writes';
value: string | ObjectWrite;
} | null)
| ({
relationTo: 'deep-nested';
value: string | DeepNested;
} | null)
| ({
relationTo: 'users';
value: string | User;
Expand Down Expand Up @@ -667,6 +694,33 @@ export interface ObjectWritesSelect<T extends boolean = true> {
updatedAt?: T;
createdAt?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "deep-nested_select".
*/
export interface DeepNestedSelect<T extends boolean = true> {
content?:
| T
| {
blocks?:
| T
| {
testBlock?:
| T
| {
meta?:
| T
| {
movie?: T;
};
id?: T;
blockName?: T;
};
};
};
updatedAt?: T;
createdAt?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "users_select".
Expand Down
Loading