Skip to content

Commit 7613d72

Browse files
authoredMar 17, 2025··
fix(core): update resolveTypeForDocument to work with version docs (#8962)
1 parent 8b68490 commit 7613d72

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed
 

‎packages/sanity/src/core/store/_legacy/document/hooks/__tests__/useDocumentType.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ test('should return correct document type on document ID transition', async () =
7979
const responseGot = defer(() => of(['book']).pipe(observeOn(asyncScheduler)))
8080

8181
client.observable.fetch = (_query, params) =>
82-
params.documentId === 'grrm' ? responseGrrm : responseGot
82+
params.publishedId === 'grrm' ? responseGrrm : responseGot
8383

8484
let documentId = 'grrm'
8585
const {result, rerender} = renderHook(() => useDocumentType(documentId, undefined), {

‎packages/sanity/src/core/store/_legacy/document/resolveTypeForDocument.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {type SanityClient} from '@sanity/client'
22
import {type Observable, of} from 'rxjs'
33
import {map} from 'rxjs/operators'
44

5-
import {getDraftId, getPublishedId} from '../../../util'
5+
import {getPublishedId} from '../../../util'
66

77
export function resolveTypeForDocument(
88
client: SanityClient,
@@ -14,9 +14,15 @@ export function resolveTypeForDocument(
1414
return of(specifiedType)
1515
}
1616

17-
const query = '*[_id in [$documentId, $draftId]]._type'
18-
const documentId = getPublishedId(id)
19-
const draftId = getDraftId(documentId)
17+
const query = '*[sanity::versionOf($publishedId)]._type'
2018

21-
return client.observable.fetch(query, {documentId, draftId}).pipe(map((types) => types[0]))
19+
return client.observable
20+
.fetch(
21+
query,
22+
{publishedId: getPublishedId(id)},
23+
{
24+
tag: 'document.resolve-type',
25+
},
26+
)
27+
.pipe(map((types) => types[0]))
2228
}

‎packages/sanity/src/structure/structureBuilder/util/resolveTypeForDocument.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
import {type SanityClient} from '@sanity/client'
2-
import {
3-
DEFAULT_STUDIO_CLIENT_OPTIONS,
4-
getDraftId,
5-
getPublishedId,
6-
type SourceClientOptions,
7-
} from 'sanity'
2+
import {DEFAULT_STUDIO_CLIENT_OPTIONS, getPublishedId, type SourceClientOptions} from 'sanity'
83

94
export async function resolveTypeForDocument(
105
getClient: (options: SourceClientOptions) => SanityClient,
116
id: string,
127
): Promise<string | undefined> {
13-
const query = '*[_id in [$documentId, $draftId]]._type'
14-
const documentId = getPublishedId(id)
15-
const draftId = getDraftId(id)
8+
const query = '*[sanity::versionOf($publishedId)]._type'
169

1710
const types = await getClient(DEFAULT_STUDIO_CLIENT_OPTIONS).fetch(
1811
query,
19-
{documentId, draftId},
12+
{publishedId: getPublishedId(id)},
2013
{tag: 'structure.resolve-type'},
2114
)
2215

0 commit comments

Comments
 (0)
Please sign in to comment.