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

Enforce the type Query on InstaQLEntity and InstaQLResult #524

Merged
merged 1 commit into from
Nov 22, 2024

Conversation

stopachka
Copy link
Contributor

@stopachka stopachka commented Nov 22, 2024

Context

Given a schema like: messages, users, messages.creator = users.createdMessages

If you made a type in the 'first' level of your expansion:

InstaQLEntity<AppSchema, 'messages', { creatorr: { } }>
InstaQLResult<AppSchema, { messagess: { } }>

TS would warn you. But as you went deeper, you would lose typesafety:

InstaQLEntity<AppSchema, 'messages', { creator: { createdMessagess: { } } }>
InstaQLResult<AppSchema, { messages: { creator: { createdMessagess: { } } }>

This is because we were really lax with how we typed Query.

It used to be:

 Query extends {
    [LinkAttrName in keyof Schema["entities"][EntityName]["links"]]?: any;
  }

I updated this to be:

type InstaQLEntitySubquery<
  Schema extends IContainEntitiesAndLinks<EntitiesDef, any>,
  EntityName extends keyof Schema["entities"],
> = {
  [QueryPropName in keyof Schema["entities"][EntityName]["links"]]?: InstaQLEntitySubquery<
    Schema,
    Schema["entities"][EntityName]["links"][QueryPropName]["entityName"]
  >;
};

(And similar for the Result types)

This ensures that as we expand our tree, we are constantly typechecking.

@dwwoelfel @nezaj

@stopachka stopachka changed the title [do-not-review] Enforce the type Query on InstaQLEntity and InstaQLResult Enforce the type Query on InstaQLEntity and InstaQLResult Nov 22, 2024
Copy link

View Vercel preview at instant-www-js-strongquer-jsv.vercel.app.

Copy link
Contributor

@dwwoelfel dwwoelfel left a comment

Choose a reason for hiding this comment

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

Type magic!

@stopachka stopachka merged commit 1be3683 into main Nov 22, 2024
39 checks passed
@stopachka stopachka deleted the strongquer branch November 22, 2024 17:35
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.

2 participants