UNPKG

groq-builder

Version:

A **schema-aware**, strongly-typed GROQ query builder. It enables you to build GROQ queries using **auto-completion**, **type-checking**, and **runtime validation**.

25 lines (24 loc) 1.32 kB
import { ResultItem } from "../types/result-types"; import { Simplify } from "../types/utils"; import { ExtractSelectByTypeResult, SelectByTypeProjections } from "./select-types"; import { IGroqBuilder, InferResultType } from "../types/public-types"; declare module "../groq-builder" { interface GroqBuilder<TResult, TQueryConfig> { /** * Applies GROQ's `select` function, for conditional logic, * based on the `_type` field. * * This is similar to `.select`, * but provides stronger types and auto-completion. * * @example * const qContent = q.star.filterByType("movie", "actor").project(sub => ({ * name: sub.selectByType({ * movie: sub => sub.field("title"), * actor: sub => sub.field("name"), * }) * })); */ selectByType<TSelectByTypeProjections extends SelectByTypeProjections<ResultItem.Infer<TResult>, TQueryConfig>, TDefaultSelection extends IGroqBuilder | null = null>(typeQueries: TSelectByTypeProjections, defaultSelection?: TDefaultSelection): GroqBuilder<Simplify<ExtractSelectByTypeResult<TSelectByTypeProjections>> | (TDefaultSelection extends null | undefined ? null : InferResultType<NonNullable<TDefaultSelection>>), TQueryConfig>; } }