sanity
Version:
Sanity is a real-time content infrastructure with a scalable, hosted backend featuring a Graph Oriented Query Language (GROQ), asset pipelines and fast edge caches
25 lines (21 loc) • 624 B
text/typescript
import {type SanityClient} from '@sanity/client'
import {
DEFAULT_STUDIO_CLIENT_OPTIONS,
getDraftId,
getPublishedId,
type SourceClientOptions,
} from 'sanity'
export async function resolveTypeForDocument(
getClient: (options: SourceClientOptions) => SanityClient,
id: string,
): Promise<string | undefined> {
const query = '*[_id in [$documentId, $draftId]]._type'
const documentId = getPublishedId(id)
const draftId = getDraftId(id)
const types = await getClient(DEFAULT_STUDIO_CLIENT_OPTIONS).fetch(
query,
{documentId, draftId},
{tag: 'structure.resolve-type'},
)
return types[0]
}