Type alias ModelPath<FlatModel, Depth, RecursionLoop, Field>

ModelPath<FlatModel, Depth, RecursionLoop, Field>: {
    done: Field extends string
        ? `${Field}.*`
        : never;
    recur: Field extends string
        ? NonNullable<UnwrapArray<FlatModel[Field]>> extends Record<string, unknown>
            ? `${Field}.${ModelPath<NonNullable<UnwrapArray<FlatModel[Field]>>, RecursionLoop[Depth]>}` | `${Field}.*`
            : `${Field}`
        : never;
}[Depth extends -1
    ? "done"
    : "recur"]

Generates custom selection set type with up to 6 levels of nested fields

Type Parameters

  • FlatModel extends Record<string, unknown>

  • Depth extends number = 5

  • RecursionLoop extends number[] = [-1, 0, 1, 2, 3, 4]

  • Field = keyof FlatModel

Returns

string[] where each string is a field in the model recurses over nested objects - such as relationships and custom types - generating a field.* type value to select all fields in that nested type, as well as a dot-delimited set of fields for fine-grained selection of particular fields in the nested type (see example below)

Example

FlatModel = {
id: string
title: string
comments: {
id:: string
content: string
}[]
}

Result

'id' | 'title' | 'comments.*' | 'comments.id' | 'comments.content'