Skip to content

Commit

Permalink
feat(sanity): add release layering support to groq2024 search strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 committed Jan 13, 2025
1 parent c6dfca6 commit 1bf9f56
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
43 changes: 43 additions & 0 deletions packages/sanity/src/core/search/groq2024/createSearchQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,49 @@ describe('createSearchQuery', () => {
expect(options.perspective).toBe('published')
})

it('should give `perspective` precedence over `includeDrafts`', () => {
const {options: optionsIncludeDrafts} = createSearchQuery(
{
query: 'term0',
types: [testType],
},
'',
{
includeDrafts: true,
perspective: 'published',
},
)

expect(optionsIncludeDrafts.perspective).toBe('published')

const {options: optionsExcludeDrafts} = createSearchQuery(
{
query: 'term0',
types: [testType],
},
'',
{
includeDrafts: false,
perspective: 'drafts',
},
)

expect(optionsExcludeDrafts.perspective).toBe('drafts')
})

it('should add no perspective parameter when `raw` perspective provided', () => {
const {options} = createSearchQuery(
{
query: 'term0',
types: [testType],
},
'',
{perspective: 'raw'},
)

expect(options.perspective).toBeUndefined()
})

it('should use provided limit (plus one to determine existence of next page)', () => {
const {params} = createSearchQuery(
{
Expand Down
25 changes: 22 additions & 3 deletions packages/sanity/src/core/search/groq2024/createSearchQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {type CrossDatasetType, type SchemaType} from '@sanity/types'
import {groupBy} from 'lodash'

import {deriveSearchWeightsFromType2024} from '../common/deriveSearchWeightsFromType2024'
import {isPerspectiveRaw} from '../common/isPerspectiveRaw'
import {prefixLast} from '../common/token'
import {
type SearchFactoryOptions,
Expand Down Expand Up @@ -54,7 +55,7 @@ function toOrderClause(orderBy: SearchSort[]): string {
export function createSearchQuery(
searchTerms: SearchTerms<SchemaType | CrossDatasetType>,
searchParams: string | SearchTerms<SchemaType>,
{includeDrafts = true, ...options}: SearchOptions & SearchFactoryOptions = {},
{includeDrafts = true, perspective, ...options}: SearchOptions & SearchFactoryOptions = {},
): SearchQuery {
const specs = searchTerms.types
.map((schemaType) =>
Expand All @@ -67,6 +68,8 @@ export function createSearchQuery(
)
.filter(({paths}) => paths.length !== 0)

const isRaw = isPerspectiveRaw(perspective)

// Note: Computing this is unnecessary when `!isScored`.
const flattenedSpecs = specs
.map(({typeName, paths}) => paths.map((path) => ({...path, typeName})))
Expand Down Expand Up @@ -96,7 +99,7 @@ export function createSearchQuery(
isScored ? [] : baseMatch,
options.filter ? `(${options.filter})` : [],
searchTerms.filter ? `(${searchTerms.filter})` : [],
'!(_id in path("versions.**"))',
isRaw ? [] : '!(_id in path("versions.**"))',
options.cursor ?? [],
].flat()

Expand Down Expand Up @@ -127,11 +130,27 @@ export function createSearchQuery(
.map((s) => `// ${s}`)
.join('\n')

let activePerspective: string | string[] | undefined

switch (true) {
// Raw perspective provided.
case isRaw:
activePerspective = undefined
break
// Any other perspective provided.
case typeof perspective !== 'undefined':
activePerspective = perspective
break
// No perspective provided.
default:
activePerspective = includeDrafts ? 'previewDrafts' : 'published'
}

return {
query: [pragma, query].join('\n'),
options: {
tag: options.tag,
perspective: includeDrafts ? 'previewDrafts' : 'published',
perspective: activePerspective,
},
params,
sortOrder,
Expand Down

0 comments on commit 1bf9f56

Please sign in to comment.