fix: filtering by polymorphic relationships inside other fields (#13265)
Previously, filtering by a polymorphic relationship inside an array / group (unless the `name` is `version`) / tab caused `QueryError: The following path cannot be queried:`.
This commit is contained in:
@@ -157,18 +157,11 @@ export function getLocalizedPaths({
|
|||||||
// If this is a polymorphic relation,
|
// If this is a polymorphic relation,
|
||||||
// We only support querying directly (no nested querying)
|
// We only support querying directly (no nested querying)
|
||||||
if (matchedField.type !== 'join' && typeof matchedField.relationTo !== 'string') {
|
if (matchedField.type !== 'join' && typeof matchedField.relationTo !== 'string') {
|
||||||
const lastSegmentIsValid =
|
|
||||||
['relationTo', 'value'].includes(pathSegments[pathSegments.length - 1]!) ||
|
|
||||||
pathSegments.length === 1 ||
|
|
||||||
(pathSegments.length === 2 && pathSegments[0] === 'version')
|
|
||||||
|
|
||||||
lastIncompletePath.path = pathSegments.join('.')
|
lastIncompletePath.path = pathSegments.join('.')
|
||||||
|
if (![matchedField.name, 'relationTo', 'value'].includes(pathSegments.at(-1)!)) {
|
||||||
if (lastSegmentIsValid) {
|
|
||||||
lastIncompletePath.complete = true
|
|
||||||
} else {
|
|
||||||
lastIncompletePath.invalid = true
|
lastIncompletePath.invalid = true
|
||||||
return paths
|
} else {
|
||||||
|
lastIncompletePath.complete = true
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lastIncompletePath.complete = true
|
lastIncompletePath.complete = true
|
||||||
|
|||||||
@@ -250,6 +250,11 @@ export default buildConfigWithDefaults({
|
|||||||
relationTo: 'directors',
|
relationTo: 'directors',
|
||||||
hasMany: true,
|
hasMany: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'polymorphic',
|
||||||
|
type: 'relationship',
|
||||||
|
relationTo: ['directors'],
|
||||||
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -1192,6 +1192,24 @@ describe('Relationships', () => {
|
|||||||
expect(result.docs[0]!.id).toBe(doc.id)
|
expect(result.docs[0]!.id).toBe(doc.id)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should allow querying polymorphic in an array', async () => {
|
||||||
|
const director = await payload.create({
|
||||||
|
collection: 'directors',
|
||||||
|
data: { name: 'direcotr' },
|
||||||
|
})
|
||||||
|
const movie = await payload.create({
|
||||||
|
collection: 'movies',
|
||||||
|
data: { array: [{ polymorphic: { relationTo: 'directors', value: director.id } }] },
|
||||||
|
})
|
||||||
|
|
||||||
|
const res = await payload.find({
|
||||||
|
collection: 'movies',
|
||||||
|
where: { 'array.polymorphic': { equals: { value: director.id, relationTo: 'directors' } } },
|
||||||
|
})
|
||||||
|
expect(res.docs).toHaveLength(1)
|
||||||
|
expect(res.docs[0].id).toBe(movie.id)
|
||||||
|
})
|
||||||
|
|
||||||
it('should allow querying hasMany in array', async () => {
|
it('should allow querying hasMany in array', async () => {
|
||||||
const director = await payload.create({
|
const director = await payload.create({
|
||||||
collection: 'directors',
|
collection: 'directors',
|
||||||
|
|||||||
@@ -271,6 +271,10 @@ export interface Movie {
|
|||||||
array?:
|
array?:
|
||||||
| {
|
| {
|
||||||
director?: (string | Director)[] | null;
|
director?: (string | Director)[] | null;
|
||||||
|
polymorphic?: {
|
||||||
|
relationTo: 'directors';
|
||||||
|
value: string | Director;
|
||||||
|
} | null;
|
||||||
id?: string | null;
|
id?: string | null;
|
||||||
}[]
|
}[]
|
||||||
| null;
|
| null;
|
||||||
@@ -750,6 +754,7 @@ export interface MoviesSelect<T extends boolean = true> {
|
|||||||
| T
|
| T
|
||||||
| {
|
| {
|
||||||
director?: T;
|
director?: T;
|
||||||
|
polymorphic?: T;
|
||||||
id?: T;
|
id?: T;
|
||||||
};
|
};
|
||||||
updatedAt?: T;
|
updatedAt?: T;
|
||||||
|
|||||||
Reference in New Issue
Block a user