Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(codegen): expose referenced type as hidden symbol #6008

Merged

Conversation

sgulseth
Copy link
Member

@sgulseth sgulseth commented Mar 15, 2024

Description

Adds a symbol so that we can lookup which type a reference is referencing to.

Types generating ends up like:

{
      _ref: string;
      _type: "reference";
      _weak?: boolean;
      [internalGroqTypeReferenceTo]?: "sanity.imageAsset";
}

...

export declare const internalGroqTypeReferenceTo: unique symbol;

What to review

Testing

No specific tests added for this as the feature is marked as "internal". We should build out general integration testing for codegen, but that can be done separately.

Notes for release

N/A - no notes needed.

Copy link

vercel bot commented Mar 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
performance-studio 🛑 Canceled (Inspect) Mar 19, 2024 3:21pm
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 19, 2024 3:21pm
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Visit Preview Mar 19, 2024 3:21pm

Copy link
Contributor

github-actions bot commented Mar 15, 2024

No changes to documentation

Copy link
Contributor

github-actions bot commented Mar 15, 2024

Component Testing Report Updated Mar 19, 2024 3:34 PM (UTC)

File Status Duration Passed Skipped Failed
comments/CommentInput.spec.tsx ✅ Passed (Inspect) 31s 15 0 0
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 6s 3 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ✅ Passed (Inspect) 11s 3 0 0
formBuilder/inputs/PortableText/copyPaste/CopyPaste.spec.tsx ✅ Passed (Inspect) 12s 4 2 0
formBuilder/inputs/PortableText/Decorators.spec.tsx ✅ Passed (Inspect) 12s 6 0 0
formBuilder/inputs/PortableText/FocusTracking.spec.tsx ✅ Passed (Inspect) 31s 15 0 0
formBuilder/inputs/PortableText/Input.spec.tsx ✅ Passed (Inspect) 1m 0s 15 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ✅ Passed (Inspect) 1m 1s 18 0 0
formBuilder/inputs/PortableText/RangeDecoration.spec.tsx ✅ Passed (Inspect) 12s 6 0 0
formBuilder/inputs/PortableText/Styles.spec.tsx ✅ Passed (Inspect) 13s 6 0 0
formBuilder/inputs/PortableText/Toolbar.spec.tsx ✅ Passed (Inspect) 19s 9 0 0

@sgulseth sgulseth marked this pull request as ready for review March 15, 2024 10:50
@sgulseth sgulseth requested a review from a team as a code owner March 15, 2024 10:50
@sgulseth sgulseth requested review from binoy14, judofyr and ricokahler and removed request for a team and binoy14 March 15, 2024 10:50
@sgulseth sgulseth force-pushed the 03-12-feat_codegen_add_CLI_to_generate_types_given_a_codegen_config branch from 015f4f6 to ad928d6 Compare March 16, 2024 14:51
@sgulseth sgulseth force-pushed the 03-15-feat_codegen_expose_referenced_type_as_hidden_symbol branch from 6b34289 to 9445143 Compare March 16, 2024 14:51
@sgulseth sgulseth force-pushed the 03-12-feat_codegen_add_CLI_to_generate_types_given_a_codegen_config branch from ad928d6 to 26ad046 Compare March 16, 2024 14:52
@sgulseth sgulseth requested a review from a team as a code owner March 16, 2024 14:52
@sgulseth sgulseth force-pushed the 03-15-feat_codegen_expose_referenced_type_as_hidden_symbol branch 2 times, most recently from b9b5ea4 to 28a69e5 Compare March 16, 2024 14:58
@sgulseth sgulseth force-pushed the 03-12-feat_codegen_add_CLI_to_generate_types_given_a_codegen_config branch from 26ad046 to 84eede0 Compare March 16, 2024 14:59
@sgulseth sgulseth force-pushed the 03-15-feat_codegen_expose_referenced_type_as_hidden_symbol branch from 28a69e5 to 9d325f1 Compare March 16, 2024 14:59
Copy link
Contributor

@ricokahler ricokahler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this pattern. It's very akin to the use of Symbol.iterator etc

I wonder if we should declare some kind of object to act as a symbol namespace. e.g.

declare const CodegenSymbol = {
  referenceTo: unique symbol
}

Then usage could be like:

interface SomeType {
  _type: 'ref';
  [CodegenSymbol.referenceTo]?: 'sanity.imageAsset';
  // ...
}

But not a blocker. Just an idea. It may not even work because the object implies a runtime value.

@ricokahler
Copy link
Contributor

ricokahler commented Mar 17, 2024

Alternative idea: you could export a type helper from the @sanity/codegen package that would abstract away this as well.

Instead of:

type myQueryResult = {
    asset?: {
      _ref: string;
      _type: "reference";
      _weak?: boolean;
      [internalGroqTypeReferenceTo]?: "sanity.imageAsset";
    };
    // ...
}

it could be:

import {type SanityReference} from '@sanity/codegen'

type myQueryResult = {
    asset?: SanityReference<'sanity.imageAsset'>;
    // ...
}
type SanityReference<TReferenceType extends string> = {
      _ref: string;
      _type: "reference";
      _weak?: boolean;
      [__internalGroqTypeReferenceTo]?: TReferenceType;
}

@sgulseth sgulseth force-pushed the 03-12-feat_codegen_add_CLI_to_generate_types_given_a_codegen_config branch from 84eede0 to 4ce884e Compare March 18, 2024 09:44
@sgulseth sgulseth force-pushed the 03-15-feat_codegen_expose_referenced_type_as_hidden_symbol branch from 8844996 to 01fc82d Compare March 18, 2024 15:16
@sgulseth sgulseth force-pushed the 03-12-feat_codegen_add_CLI_to_generate_types_given_a_codegen_config branch from 4ce884e to 0a18f59 Compare March 18, 2024 21:46
@sgulseth sgulseth force-pushed the 03-15-feat_codegen_expose_referenced_type_as_hidden_symbol branch from 01fc82d to 53dde4e Compare March 18, 2024 21:46
@sgulseth sgulseth force-pushed the 03-12-feat_codegen_add_CLI_to_generate_types_given_a_codegen_config branch from 0a18f59 to 037d8b8 Compare March 19, 2024 14:08
@sgulseth sgulseth force-pushed the 03-15-feat_codegen_expose_referenced_type_as_hidden_symbol branch from 53dde4e to 59fd242 Compare March 19, 2024 14:08
@sgulseth sgulseth force-pushed the 03-12-feat_codegen_add_CLI_to_generate_types_given_a_codegen_config branch from 037d8b8 to f1da568 Compare March 19, 2024 15:01
@sgulseth sgulseth force-pushed the 03-15-feat_codegen_expose_referenced_type_as_hidden_symbol branch from 59fd242 to beeca7e Compare March 19, 2024 15:01
@sgulseth sgulseth force-pushed the 03-12-feat_codegen_add_CLI_to_generate_types_given_a_codegen_config branch from f1da568 to 4d9d54c Compare March 19, 2024 15:05
@sgulseth sgulseth force-pushed the 03-15-feat_codegen_expose_referenced_type_as_hidden_symbol branch from beeca7e to 6975a93 Compare March 19, 2024 15:06
@sgulseth sgulseth force-pushed the 03-12-feat_codegen_add_CLI_to_generate_types_given_a_codegen_config branch from 4d9d54c to f451330 Compare March 19, 2024 15:11
@sgulseth sgulseth force-pushed the 03-15-feat_codegen_expose_referenced_type_as_hidden_symbol branch from 6975a93 to dca8116 Compare March 19, 2024 15:11
Base automatically changed from 03-12-feat_codegen_add_CLI_to_generate_types_given_a_codegen_config to next March 19, 2024 15:13
@sgulseth sgulseth force-pushed the 03-15-feat_codegen_expose_referenced_type_as_hidden_symbol branch from dca8116 to e60bdf9 Compare March 19, 2024 15:14
@sgulseth sgulseth added this pull request to the merge queue Mar 19, 2024
Merged via the queue into next with commit 2826c46 Mar 19, 2024
23 of 27 checks passed
@sgulseth sgulseth deleted the 03-15-feat_codegen_expose_referenced_type_as_hidden_symbol branch March 19, 2024 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants