Skip to content

Commit a22dff3

Browse files
authored
fix(next): version._status field queried/returned for entities without drafts (#14423)
### What? Fixes an issue where the `_status` field was being queried or returned for entities (collections/globals) that have versions enabled but drafts disabled. ### Why? The `_status` field only exists when `drafts` are enabled (not just `versions`). So the following issue occurred: **Collections with versions (no drafts)**: When viewing version comparison pages in the admin UI, the app threw `QueryError: The following path cannot be queried: version._status` because the view code was attempting to filter versions by `_status` for collections that don't have drafts enabled. ### How? - Check if drafts are enabled before querying `version._status` in version comparison views
1 parent eba6cfc commit a22dff3

File tree

4 files changed

+80
-45
lines changed

4 files changed

+80
-45
lines changed

packages/next/src/views/Version/fetchVersions.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,26 @@ export const fetchLatestVersion = async <TVersionData extends object = object>({
163163
user?: TypedUser
164164
where?: Where
165165
}): Promise<null | TypeWithVersion<TVersionData>> => {
166+
// Get the entity config to check if drafts are enabled
167+
const entityConfig = collectionSlug
168+
? req.payload.collections[collectionSlug]?.config
169+
: globalSlug
170+
? req.payload.globals[globalSlug]?.config
171+
: undefined
172+
173+
// Only query by _status if drafts are enabled (since _status field only exists with drafts)
174+
const draftsEnabled = entityConfig?.versions?.drafts
175+
166176
const and: Where[] = [
167-
{
168-
'version._status': {
169-
equals: status,
170-
},
171-
},
177+
...(draftsEnabled
178+
? [
179+
{
180+
'version._status': {
181+
equals: status,
182+
},
183+
},
184+
]
185+
: []),
172186
...(where ? [where] : []),
173187
]
174188

packages/next/src/views/Version/index.tsx

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -143,33 +143,36 @@ export async function VersionView(props: DocumentViewServerProps) {
143143
})
144144
: Promise.resolve(null),
145145
// Previous published version
146-
fetchVersions({
147-
collectionSlug,
148-
depth: 0,
149-
draft: true,
150-
globalSlug,
151-
limit: 1,
152-
locale: 'all',
153-
overrideAccess: false,
154-
parentID: id,
155-
req,
156-
sort: '-updatedAt',
157-
user,
158-
where: {
159-
and: [
160-
{
161-
updatedAt: {
162-
less_than: versionTo.updatedAt,
163-
},
164-
},
165-
{
166-
'version._status': {
167-
equals: 'published',
168-
},
146+
// Only query for published versions if drafts are enabled (since _status field only exists with drafts)
147+
draftsEnabled
148+
? fetchVersions({
149+
collectionSlug,
150+
depth: 0,
151+
draft: true,
152+
globalSlug,
153+
limit: 1,
154+
locale: 'all',
155+
overrideAccess: false,
156+
parentID: id,
157+
req,
158+
sort: '-updatedAt',
159+
user,
160+
where: {
161+
and: [
162+
{
163+
updatedAt: {
164+
less_than: versionTo.updatedAt,
165+
},
166+
},
167+
{
168+
'version._status': {
169+
equals: 'published',
170+
},
171+
},
172+
],
169173
},
170-
],
171-
},
172-
}),
174+
})
175+
: Promise.resolve(null),
173176
])
174177

175178
const previousVersion: null | TypeWithVersion<object> = previousVersionResult?.docs?.[0] ?? null

test/versions/collections/Versions.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,7 @@ const VersionPosts: CollectionConfig = {
1010
return true
1111
}
1212

13-
return {
14-
or: [
15-
{
16-
_status: {
17-
equals: 'published',
18-
},
19-
},
20-
{
21-
_status: {
22-
exists: false,
23-
},
24-
},
25-
],
26-
}
13+
return false
2714
},
2815
readVersions: ({ req: { user } }) => Boolean(user),
2916
},

test/versions/payload-types.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export interface Config {
8585
text: Text;
8686
media: Media;
8787
media2: Media2;
88+
'payload-kv': PayloadKv;
8889
users: User;
8990
'payload-jobs': PayloadJob;
9091
'payload-locked-documents': PayloadLockedDocument;
@@ -111,6 +112,7 @@ export interface Config {
111112
text: TextSelect<false> | TextSelect<true>;
112113
media: MediaSelect<false> | MediaSelect<true>;
113114
media2: Media2Select<false> | Media2Select<true>;
115+
'payload-kv': PayloadKvSelect<false> | PayloadKvSelect<true>;
114116
users: UsersSelect<false> | UsersSelect<true>;
115117
'payload-jobs': PayloadJobsSelect<false> | PayloadJobsSelect<true>;
116118
'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
@@ -605,6 +607,23 @@ export interface Media2 {
605607
focalX?: number | null;
606608
focalY?: number | null;
607609
}
610+
/**
611+
* This interface was referenced by `Config`'s JSON-Schema
612+
* via the `definition` "payload-kv".
613+
*/
614+
export interface PayloadKv {
615+
id: string;
616+
key: string;
617+
data:
618+
| {
619+
[k: string]: unknown;
620+
}
621+
| unknown[]
622+
| string
623+
| number
624+
| boolean
625+
| null;
626+
}
608627
/**
609628
* This interface was referenced by `Config`'s JSON-Schema
610629
* via the `definition` "users".
@@ -800,6 +819,10 @@ export interface PayloadLockedDocument {
800819
relationTo: 'media2';
801820
value: string | Media2;
802821
} | null)
822+
| ({
823+
relationTo: 'payload-kv';
824+
value: string | PayloadKv;
825+
} | null)
803826
| ({
804827
relationTo: 'users';
805828
value: string | User;
@@ -1191,6 +1214,14 @@ export interface Media2Select<T extends boolean = true> {
11911214
focalX?: T;
11921215
focalY?: T;
11931216
}
1217+
/**
1218+
* This interface was referenced by `Config`'s JSON-Schema
1219+
* via the `definition` "payload-kv_select".
1220+
*/
1221+
export interface PayloadKvSelect<T extends boolean = true> {
1222+
key?: T;
1223+
data?: T;
1224+
}
11941225
/**
11951226
* This interface was referenced by `Config`'s JSON-Schema
11961227
* via the `definition` "users_select".

0 commit comments

Comments
 (0)