CLI Typegen expand references

Open
#29 20 comments 21 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
typescript
Domain
cli, tooling

Research direction

Start by locating the CLI typegen implementation and its typegen configuration handling. Review how schema references and GROQ query types currently produce internalGroqTypeReferenceTo, then determine how configuration or schema flags should control expansion and how multi-level references should be represented. Done means generated TypeScript types expose the referenced schema properties without requiring a dummy query.

Written by the indexing model from the issue text.

Description

Is your feature request related to a problem? Please describe.
When generating types you only get an "internalGroqTypeReferenceTo" a related schema.

export type ProgressBar = {
  _id: string;
  _type: "progressBar";
  _createdAt: string;
  _updatedAt: string;
  _rev: string;
  name: string;
  steps: Array<{
    _ref: string;
    _type: "reference";
    _weak?: boolean;
    _key: string;
    [internalGroqTypeReferenceTo]?: "progressBarStep";
  }>;
  locale?: string;
};

This means that you can't access the actual properties on an indiviual "step", which becomes troublesome when you have several references to other schemas or some kind of multi-level-reference structure (e.g. I have a Page with a Progress bar that has Progress bar steps).

Describe the solution you'd like
It would be great if we during the type generation could define if we wanted all/some "internalGroqTypeReferences" expanded. Perhaps either via the typegen config or some kind of flag on the schema itself.

The result after such an expansion could be something like:

export type ProgressBar = {
  _id: string;
  _type: "progressBar";
  _createdAt: string;
  _updatedAt: string;
  _rev: string;
  name: string;
  steps: Array<{
    _ref: string;
    _type: "reference";
    _weak?: boolean;
    _key: string;
    [internalGroqTypeReferenceTo]?: "progressBarStep";
    // Progress bar step properties
    _id: string;
    _type: "progressBarStep";
    _createdAt: string;
    _updatedAt: string;
    _rev: string;
    text: string;
    percentage: number
    locale?: string;
  }>;
  locale?: string;
};

Describe alternatives you've considered
I've considered creating a "dummy query" that explicitly expands the type but isn't used anywhere, however I don't think that's a very clean solution. I've also tried to alter the main query that contains multi-level-references to try and get the query-typegen to expand the "deeper levels of nesting" but with no success.

Additional context
Typegen is a super helpful feature, and the current state is a very promising! I do however think that there is room for some improvement, an this would bring it a big step forward!

Dominant language
TypeScript
Stars
1
Forks
2
Avg merge
3d 11h
Merged PRs (30d)
5

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sanity-io/codegen

All issues in sanity-io/codegen

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.