Skip to content

Commit c1080f4

Browse files
authoredMar 18, 2025··
fix(core): update resolveTypeForDocument query (#8969)
1 parent 7f07747 commit c1080f4

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed
 

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test('should return passed document type if already resolved', async () => {
3131

3232
test('should resolve document type from API on undefined type (with loading state)', async () => {
3333
const client = createMockSanityClient()
34-
const response = defer(() => of(['book']).pipe(observeOn(asyncScheduler)))
34+
const response = defer(() => of('book').pipe(observeOn(asyncScheduler)))
3535

3636
client.observable.fetch = vi.fn(() => response)
3737

@@ -75,8 +75,8 @@ test('should return correct document type on document type transition', async ()
7575
test('should return correct document type on document ID transition', async () => {
7676
const client = createMockSanityClient()
7777

78-
const responseGrrm = defer(() => of(['author']).pipe(observeOn(asyncScheduler)))
79-
const responseGot = defer(() => of(['book']).pipe(observeOn(asyncScheduler)))
78+
const responseGrrm = defer(() => of('author').pipe(observeOn(asyncScheduler)))
79+
const responseGot = defer(() => of('book').pipe(observeOn(asyncScheduler)))
8080

8181
client.observable.fetch = (_query, params) =>
8282
params.publishedId === 'grrm' ? responseGrrm : responseGot
@@ -102,7 +102,7 @@ test('should return correct document type on document ID transition', async () =
102102
test('should return correct document type when transitioning from undefined type to specified type', async () => {
103103
const client = createMockSanityClient()
104104

105-
const responseGrrm = defer(() => of(['author']).pipe(observeOn(asyncScheduler)))
105+
const responseGrrm = defer(() => of('author').pipe(observeOn(asyncScheduler)))
106106

107107
client.observable.fetch = vi.fn<typeof client.observable.fetch>().mockReturnValue(responseGrrm)
108108

@@ -132,7 +132,7 @@ test('should return correct document type when transitioning from undefined type
132132
test('should return correct document type when transitioning from specified to undefined type', async () => {
133133
const client = createMockSanityClient()
134134

135-
const responseGrrm = defer(() => of(['person']).pipe(observeOn(asyncScheduler)))
135+
const responseGrrm = defer(() => of('person').pipe(observeOn(asyncScheduler)))
136136

137137
client.observable.fetch = vi.fn<typeof client.observable.fetch>().mockReturnValue(responseGrrm)
138138

@@ -166,7 +166,7 @@ test('should cancel ongoing requests when transitioning document ID', async () =
166166
const responseDelayedGrrm = defer(() =>
167167
of(['person']).pipe(observeOn(asyncScheduler), delay(5000), tap(hasResolvedFirst)),
168168
)
169-
const responseGot = defer(() => of(['book']).pipe(observeOn(asyncScheduler)))
169+
const responseGot = defer(() => of('book').pipe(observeOn(asyncScheduler)))
170170

171171
client.observable.fetch = vi
172172
.fn<typeof client.observable.fetch>()
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {type SanityClient} from '@sanity/client'
22
import {type Observable, of} from 'rxjs'
3-
import {map} from 'rxjs/operators'
43

54
import {getPublishedId} from '../../../util'
65

@@ -14,15 +13,13 @@ export function resolveTypeForDocument(
1413
return of(specifiedType)
1514
}
1615

17-
const query = '*[sanity::versionOf($publishedId)]._type'
16+
const query = '*[sanity::versionOf($publishedId)][0]._type'
1817

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]))
18+
return client.observable.fetch(
19+
query,
20+
{publishedId: getPublishedId(id)},
21+
{
22+
tag: 'document.resolve-type',
23+
},
24+
)
2825
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ export async function resolveTypeForDocument(
55
getClient: (options: SourceClientOptions) => SanityClient,
66
id: string,
77
): Promise<string | undefined> {
8-
const query = '*[sanity::versionOf($publishedId)]._type'
8+
const query = '*[sanity::versionOf($publishedId)][0]._type'
99

10-
const types = await getClient(DEFAULT_STUDIO_CLIENT_OPTIONS).fetch(
10+
const type = await getClient(DEFAULT_STUDIO_CLIENT_OPTIONS).fetch(
1111
query,
1212
{publishedId: getPublishedId(id)},
1313
{tag: 'structure.resolve-type'},
1414
)
1515

16-
return types[0]
16+
return type
1717
}

0 commit comments

Comments
 (0)
Please sign in to comment.